zoukankan      html  css  js  c++  java
  • JavaScript高级程序设计-(4) 引用类型

    1.object
    var person={};//与new Object()相同
    

    一般创建对象

    var person=new Object();
    person.Name="admin";
    

    Json方式创建

    var person={
      Name:"admin",
      Age:23  
    };
    

    object作为参数传递

    function GetPerson(Person p){
     alert(p.Name)
    }
    
    SetPerson({Name:"admin",Age:12});
    
    2.Array

    Array初始化:

    var arr=new Array(1,"a",3,4);
    
    var arr2=[1,"a",3,4];
    
    var arr3=[];
    

    检查是否数组:

    if(value instanceof Array){
     //对数组执行操作
    }
    //或者是否isArray方法
    if(Array.isArray(value)){
     //对数组执行操作
    }
    

    数组转换

    var people={"A",34};
    people.toString();//A,34
    
    var colors=["red","green"];
    colors.join(",");//red,green
    

    栈方法

    栈是一种后进先出的数据结构,最新添加的最早移除

    push()方法可以接收任意数量的参数,添加到数组末尾,返回最新数组长度

    pop()方法则则数组末尾移除最后一项,减少数组的length值

    var colors=new Array();
    var count=colors.push("red","green");  //插入2项
    alert(count);   //2
    
    count =colors.push("brown");  //插入另一项
    alert(count);   //3
    
    var item = colors.pop();  //取得最后一项
    alert(item);// brown
    alert(colors.length);   //2
    

    队列方法

    shift()方法,移除数组中第一个项返回该项,同时将数组长度减1

    unshift()方法,在数组前端添加并返回新数组长度

    例1:

    例2:

    排序方法:

    reverse()和sort()方法,reverse()实现数组数值反转,sort按照顺序排序

    操作方法:

    concat() 将接收的参数添加到副本的末尾,返回新构建的数组,不更改原数组数据

    var colors=["red","green","blue"];
    var colors2=colors.concat("yellow",["black","brown"]);
    
    alert(colors);  //red,green,blue
    alert(colors2); //red,green,blue,yellow,black,brown
    

    slice() 返回从该数组指定为主开始到当前数组末尾的所有项,也不影响原数组

    var colors=["red","green","blue","yellow","purple"];
    var colors2=colors.slice(1);
    var colors3=colors.slice(1,4);
    
    alert(colors2);//green,blue,yellow,purple
    alert(colors3);//green,blue,yellow
    

    splice() 向数组中部插入项

    删除:可以删除任意数量的项,只需指定2个参数:要删除的第一项的位置和要删除的项数。
    插入:可以向知道位置插入任意数量的项,只需提供3个参数,起始位置,0(要删除的项数)和要插入的项
    替换:可以向指定位置插入任意数量的项,且同时删除任意数量的项,只需指定3个参数;起始位置,要删除的项数和要插入的任意数量的项。插入的项数不必与删除的项数相等

    var colors=["red","green","blue"];
    var removed=colors.splice(0,1);
    alert(colors);   //green,blue
    alert(removed);  //red,返回的数组中只包含一项
    
    removed=colors.splice(1,0,"yellow","orange");// 从位置1开始插入2项
    alert(colors);	//green,yellow,orange,blue
    alert(removed);	//返回的是一个空数组
    
    removed=colors.splice(1,1,"red","purple");//插入2项,删除1项
    alert(colors);	//green,red,purple,yellow,orange,blue
    alert(removed);	//yellow,返回数组中只包含一项
    
    位置方法

    IndexOf()和LastInfodexOf()查找索引位置

    迭代方法

    对数组中的每一项运行给定函数,该函数传入的方法接收3个参数:数组项的值,该项的索引和数组对象本身

    every():如果函数对每一项都返回true,则返回true

    filter():返回该函数会返回true的项组成的数组

    forEach:方法没有返回值

    map():返回每次函数调用的结果组成的数组

    some():如果函数对任一项返回true,则返回true

    var numbers=[1,2,3,4,5,4,3,2,1];
    var everyResult =numbers.every(function(item,index,array){
    	return (item)>2;
    });
    
    alert(everyResult);  //false
    
    var someResult = numbers.some(function(item,index,array){
    	return (item>2);
    });
    
    alert(someResult);	//true
    
    var numbers=[1,2,3,4,5,4,3,2,1];
    
    var filterResult=numbers.filter(function(item,index,array){
    	return (item>2);
    });
    alert(flterResult);	//[3,4,5,4,3]
    
    var numbers=[1,2,3,4,5,4,3,2,1];
    
    var mapResult=numbers.map(function(item,index,array){
    	return (item*2);
    });
    alert(mapResult);	//[2,4,6,8,10,8,6,4,2]
    
    var numbers=[1,2,3,4,5,4,3,2,1];
    
    numbers.forEach(function(item,index,array){
    	//执行操作
    });
    

      

    缩小方法

    reduce()和reduceRight(),迭代数组所有项,构建一个最终返回值
    reduce()从第1项开始,逐个遍历
    reduceRight()从数组最后一项开始,遍历到第1项

    var values=[1,2,3,4,5];
    var sum=values.reduce(function(prev,cur,index,array){
    	return prev+cur;
    });
    alert(sum); 	//15
    
    var values=[1,4];
    var sum=values.reduceRight(function(prev,cur,index,array){
    	return prev-cur;
    });
    alert(sum); 	//3
    

    3.RegExp类型

    RegExp类型支持正则表达式
    var expression=/pattern/flags;

    4.基本包装类型

    为了便于操作基本类型的值,提供了Boolean,Number和String,详细的不说了

    var s1=new String("some text");
    s1.color="red";
    alert(s1.color);  //undefined
    
    var s1=new String("some text");
    alert(s1 instanceof string);   //true
    

    5.单体内置对象

    Global全局对象

    1.URI编码方法
    2.eval()方法
    eval()是一个解析器,接收1个参数

    eval("alert('hi')");等同于alert('hi')
    
    eval("var msg='hello world'");
    alert(msg);	//hello world
    

    Math对象

    random()方法

    var num=Math.floor(Math.random()*10+1);产生1到10的数值
    
  • 相关阅读:
    Eclipse遇到Initializing Java Tooling终极解决办法
    Quartz定时任务框架理解
    Quartz定时任务调度
    jQueryEasyUI Messager基本使用
    转一篇关于Log4j的介绍文章,很详细
    android联系人
    手机中预置联系人(姓名,号码,头像)
    android获取硬件信息
    Sumatra pdf更改起始页面的黄色颜色
    Eclipse背景颜色设置(设置成豆沙绿色保护眼睛,码农保护色)
  • 原文地址:https://www.cnblogs.com/licin/p/5850461.html
Copyright © 2011-2022 走看看