高程3总结#第5章引用类型

引用类型

Object类型

  • 两种创建Object实例的对象

    • 使用new操作符后跟Object构造函数

      var person=new Object();
      person.name="Nicholas";
      person.age=29;
    • 对象字面量表示法

      var person={
        name:"Nicholas",
        age:29
      }

Array类型

  • 创建数组的基本方式有两种

    • 使用Array构造函数,new操作符可以省略

      var colors=new Array();
      var colors=Array(3);
    • 数组字面量表示法

      var colors=["red","blue","green"];//创建一个包含3个字符串的数组
      var names=[];//创建一个空数组
      var values=[1,2,];//创建一个包含2或3项的数组,谨慎操作
      var options=[,,,,,];//创建一个包含5或6项的数组,谨慎操作
    • 数组的length不是只读的,可以通过设置这个属性,可以从数组的末尾移除项或向数组或向数组中添加新项

      var colors=["red","blue","green"];//创建一个包含3个字符串的数组
      colors.length=2;
      alert(colors[2]);//undefined
      var colors=["red","blue","green"];
      colors.length=4;
      alert(colors[3]);//undefined
    • 可以使用instanceof确定某个对象是否是数组,但是如果存在两个以上不同的全局执行环境,从而存在两个以上不同版本的Array构造函数,从其中一个框架向另外一个框架传入数组,那么传入的数组与在第二框架中原生创建的数组分别具有各自不同的构造函数。
    • Array.isArray()方法解决instanceof方法的问题

      if(Array.isArray(value)){
        //对数组执行默写操作
      }
    • push()接收任意数量的参数,把它们逐个添加到数组末尾,并返回修改后数组的长度。
    • pop()从数组末尾移除最后一项,减少数组的length值,然后返回移除的项。
    • shift()从数组开头移除一项,减少数组的length值,然后返回移除的项。
    • unshift()在数组前端添加人一个项,并返回新数组的长度。
    • reverse()反转数组项的顺序。返回值是经过排序之后的值。
    • sort()排序,可以接收一个比较函数作为参数。返回值是经过排序之后的值。
    • concat()将一个或多个数组添加到结果数组中,如果传递的值不是数组,将会被简单地添加到结果数组的末尾。

      var colors=["red","green","blue"];
      var colors2=colors.concat("yellow",["black","brown"]);
      alert(colors);//red,green,blue
      alert(colors2);//red,green,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()向数组中部插入项。指定两个参数,删除项。指定三个参数,插入项。指定三个参数,替换项。

      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开始插入两项
      alert(colors);//green,yellow,orange,blue
      alert(removed);//返回的是一个空数组
      
      removed=colors.splice(1,1,"red","purple");//插入两项,删除一项
      alert(colors);//green,red,purple,orange,blue
      alert(removed);//yellow,返回的数组中只包含一项
    • indexOf()从数组的开头向后查找,lastIndexOf()从数组的末尾开始向前查找。

      var numbers=[1,2,3,4,5,4,3,2,1];
      alert(number.indexOf(4));//3
      alert(number.lastIndexOf(4));//5
      alert(number.indexOf(4,4));//5
      alert(number.lastIndexOf(4,4));//3
      var person={name:"Nicholas"};
      var people=[{name:"Nicholas"}];
      var morePeople=[person];
      alert(people.indexOf(person));//-1
      alert(morePeople.indexOf(person));//0
    • every()对数组中的每一项运行给定函数,如果该函数对每一项都返回true,则返回true
    • filter()对数组中的每一项运行给定函数,返回该函数会返回true的项组成的数组
    • forEach()对数组中的每一项运行给定函数,没有返回值。
    • map()对数组中的每一项运行给定函数,返回每次函数调用的结果组成的数组。
    • some()对数组中的每一项给定函数,如果该函数对任一项返回true,则返回true。
    • reduce()从数组的第一项开始,逐个遍历到最后。
    • reduceRight()从数组的最后一项开始,向前遍历到第一项。

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

Date类型

  • Date类型来自UTC

    • 使用new操作符和Date构造函数

      var now=new Date();
      var someDate=new Date(Date.parse("May 25, 2004"));
      var someDate=new Date("May 25, 2004");
    • Date.UTC()同样返回表示日期的毫秒数,但它与Date.parse()在构建时使用不同的信息。Date.UTC()的参数分别是年份、基于0的月份、月中的哪一天、小时数、分钟、秒以及毫秒

      var y2k=new Date(Date.UTC(2000,0));
      var allFives=new Date(Date.UTC(2005,4,5,17,55,55));
    • Data.now()返回表示调用这个方法时的日期和时间的毫秒数
    • toDateString()以特定于实现的格式显示星期几、月、日和年
    • toTimeString()以特定于实现的格式显示时、分、秒和时区
    • toLocaleDateString()以特定于实现的格式显示时、分、秒
    • toLocaleTimeString()以特定于地区的格式显示星期几、月、日和年
    • toUTCString()以特定于实现的格式完整的UTC日期
    • getTime()返回表示日期的毫秒数,与valueOf()方法返回的值相同
    • setTime(毫秒)以毫秒数设置日期,会改变整个日期
    • getFullYear()取得4位数的年份,如2007而非仅07
    • getUTCFullYear()返回UTC日期的4位数年份
    • setFullYear(年)设置日期的年份。传入的年份值必须是4位数字,如2007而非仅07
    • setUTCFullYear(年)设置UTC日期的年份。传入的年份值必须是4位数字,如2007而非仅07
    • getMonth()返回日期中的月份,其中0表示一月,11表示十二月
    • getUTCMonth()返回UTC日期中的月份,其中0表示一月,11表示十二月
    • setMonth(月)设置日期的月份。传入的月份值必须大于0,超过11则增加年份
    • setUTCMonth(月)设置UTC日期的月份。传入的月份值必须大于0,超过11则增加年份
    • getDate()返回日期月份中的天数,1到31
    • getUTCDate()返回UTC日期月份中的天数,1到31
    • setDate(日)设置日期月份中的天数。如果传入的值超过了该月中应有的天数,则增加月份
    • setUTCDate(日)设置UTC日期月份中的天数。如果传入的值超过了该月中应有的天数,则增加月份
    • getDay()返回日期中星期的星期几,其中0表示星期日,6表示星期六
    • getUTCDay()返回UTC日期中星期的星期几,其中0表示星期日,6表示星期六
    • getHours()返回日期中的小时数,0到23
    • getUTCHours()返回UTC日期中的小时数,0到23
    • setHours(时)设置日期中的小时数。传入的值超过了23则增加月份中的天数
    • setUTCHours(时)设置UTC日期中的小时数。传入的值超过了23则增加月份中的天数
    • getMinutes()返回日期中的分钟数,0到59
    • getUTCMinutes()返回UTC日期中的分钟数,0到59
    • setMinutes(分)设置日期中的分钟数。传入的值超过59则增加小时数
    • setUTCMinutes(分)设置UTC日期中的分钟数。传入的值超过59则增加小时数
    • getSeconds()返回日期中的秒数,0到59
    • getUTCSeconds()返回UTC日期中的秒数,0到59
    • setSeconds(秒)设置日期中的秒数。传入的值超过了59会增加分钟数
    • setUTCSeconds(秒)设置UTC日期中的秒数。传入的值超过了59会增加分钟数
    • getMilliseconds()返回日期中的毫秒数
    • getUTCMilliseconds()返回UTC日期中的毫秒数
    • setMilliseconds(毫秒)设置日期中的毫秒数
    • setUTCMilliseconds(毫秒)设置UTC日期中的毫秒数
    • getTimezoneOffset()返回本地时间与UTC时间相差的分钟数。例如,美国东部标准时间返回300。在某地进入夏令时的情况下,这个值会有所变化

RegExp类型

  • RegExp类型,g表示全局,i表示不区分大小写,m表示多行模式

    var text = "mom and dad and baby";
    var pattern = /mom( and dad( and baby)?)?/gi;
    
    var matches = pattern.exec(text);
    alert(matches.index); // 0
    alert(matches.input); // "mom and dad and baby"
    alert(matches[0]); // "mom and dad and baby"
    alert(matches[1]); // " and dad and baby"
    alert(matches[2]); // " and baby"
    
    var text = "cat, bat, sat, fat";
    var pattern1 = /.at/;
    
    var matches = pattern1.exec(text);
    alert(matches.index); //0
    alert(matches[0]); //cat
    alert(pattern1.lastIndex); //0
    
    matches = pattern1.exec(text);
    alert(matches.index); //0
    alert(matches[0]); //cat
    alert(pattern1.lastIndex); //0
    
    var pattern2 = /.at/g;
    
    var matches = pattern2.exec(text);
    alert(matches.index); //0
    alert(matches[0]); //cat
    alert(pattern2.lastIndex); //3
    matches = pattern2.exec(text);
    alert(matches.index); //5
    alert(matches[0]); //bat
    alert(pattern2.lastIndex); //8

Function类型

  • Function类型

    • 每个函数都是Function类型的实例,而且与其他引用类型一样具有属性和方法。函数时对象,因此函数名实际上也是一个纸箱函数对象的指针,不会与某个函数绑定。函数通常是使用函数声明语法定义的。

      function sum(num1,num2){
        return num1+num2;
      }
      
      var sum=function(num1,num2){
        return num1+num2;
      }
    • 在代码开始执行之前,解析器就已经通过一个名为函数声明提升的过程,读取并将函数声明添加到执行环境中。对代码求值时,JavaScript引擎在第一遍会声明函数并将它们放到源代码树的顶部。所以即使声明函数的diamante在调用它的代码后面,JavaScript引擎也能把函数声明提升到顶部。

      alert(sum(10,10));
      function sum(num1,num2){
        return num+num2
      }          
      alert(sum(10,10));
      var sum=function(num1,num2){
        return num1+num2
      }

Boolean类型

  • Boolean类型,与布尔值对应的引用类型。要创建Boolean对象,可以调用Boolean构造函数并传入true或false值

    var booleanObject=new Boolean(true)

Number类型

  • Number类型

    • toFixed()方法按照指定的小数位返回数值的字符串表示,toExponential()方法返回以指数表示法表示的数值的字符串形式,toPrecision()方法返回固定大小fixed格式或者exponential格式

      var num=10;
      alert(num.toFixed(2));//"10.00"
      alert(num.toExponential(1));//"1.0e+1"
      
      var num=99;
      alert(num.toPrecision(1));//"1e+2"
      alert(num.toPrecision(2));//"99"
      alert(num.toPrecision(3));//"99.0"

String类型

  • String类型,使用String构造函数来创建

    var stringObject=new String("hello world");
    • charAt()和chartCodeAt()接收一个参数,返回这个参数位置上的字符
    • concat()将一个或多个字符串拼接起来
    • slice()、substr()和substring()返回被操作字符串的一个子字符串
    • indexOf()和lastIndexOf()从字符串中搜索给定的子字符串
    • trim()创建一个字符串的副本,删除前置及后缀的所有空格
    • toLowerCase()、toLocaleLowerCase()、toUpperCase()和toLocaleUpperCase()

      var stringValue = "hello world";
      alert(stringValue.charAt(1)); //"e"
      
      var stringValue = "hello world";
      alert(stringValue.charCodeAt(1)); // 输出"101"
      
      var stringValue = "hello ";
      var result = stringValue.concat("world");
      alert(result); //"hello world"
      alert(stringValue); //"hello"
      
      var stringValue = "hello world";
      alert(stringValue.slice(-3)); //"rld"
      alert(stringValue.substring(-3)); //"hello world"
      alert(stringValue.substr(-3)); //"rld"
      alert(stringValue.slice(3, -4)); //"lo w"
      alert(stringValue.substring(3, -4)); //"hel"
      alert(stringValue.substr(3, -4)); //""(空字符串)
      
      var stringValue = "hello world";
      alert(stringValue.indexOf("o")); //4
      alert(stringValue.lastIndexOf("o")); //7
      
      var stringValue = " hello world ";
      var trimmedStringValue = stringValue.trim();
      alert(stringValue); //" hello world "
      alert(trimmedStringValue); //"hello world"
      
      var stringValue = "hello world";
      alert(stringValue.toLocaleUpperCase()); //"HELLO WORLD"
      alert(stringValue.toUpperCase()); //"HELLO WORLD"
      alert(stringValue.toLocaleLowerCase()); //"hello world"
      alert(stringValue.toLowerCase()); //"hello world"
      
      var text = "cat, bat, sat, fat";
      var pattern = /.at/;
      //与 pattern.exec(text)相同
      var matches = text.match(pattern);
      alert(matches.index); //0
      alert(matches[0]); //"cat"
      alert(pattern.lastIndex); //0
      
      var text = "cat, bat, sat, fat";
      var pos = text.search(/at/);
      alert(pos); //1
      
      var text = "cat, bat, sat, fat";
      var result = text.replace("at", "ond");
      alert(result); //"cond, bat, sat, fat"
      result = text.replace(/at/g, "ond");
      alert(result); //"cond, bond, sond, fond"
      
      var stringValue = "yellow";
      alert(stringValue.localeCompare("brick")); //1
      alert(stringValue.localeCompare("yellow")); //0
      alert(stringValue.localeCompare("zoo")); //-1
  • 内置对象

    • Global对象,eval方法就像是一个完整的ECMAScript解析器,只接收一个参数,即要执行的ECMAScript字符串。eval{"alert('hi')"};相当于alert("hi");
    • 对象属性

      • undefined:特殊值undefined
      • Date:构造函数Date
      • NaN:特殊值NaN
      • RegExp:构造函数RegExp
      • Infinity:特殊值Infinity
      • Error:构造函数Error
      • Object:构造函数Object
      • EvalError:构造函数EvalError
      • Array:构造函数Array
      • RangeError:构造函数RangeError
      • Function:构造函数Function
      • ReferenceError:构造函数ReferenceError
      • Boolean:构造函数Boolean
      • SyntaxError:构造函数SyntaxError
      • String:构造函数String
      • TypeError:构造函数TypeError
      • Number:构造函数Number
      • URIError:构造函数URIError
    • Math对象

      • min()和max()方法确定最大值和最小值
      • Math.ceil()、Math.floor和()Math.round()四舍五入相关方法
      • Math.random()获取随机值

        var max = Math.max(3, 54, 32, 16);
        alert(max); //54
        var min = Math.min(3, 54, 32, 16);
        alert(min); //3
        
        alert(Math.ceil(25.9)); //26
        alert(Math.ceil(25.5)); //26
        alert(Math.ceil(25.1)); //26
        alert(Math.round(25.9)); //26
        alert(Math.round(25.5)); //26
        alert(Math.round(25.1)); //25
        alert(Math.floor(25.9)); //25
        alert(Math.floor(25.5)); //25
        alert(Math.floor(25.1)); //25
        
        var num = Math.floor(Math.random() * 10 + 1);
      • 其他方法

        • Math.abs(num)返回num的绝对值
        • Math.asin(x)返回x的反正弦值
        • Math.exp(num)返回Math.E的num次幂
        • Math.atan(x)返回x的反正切值
        • Math.log(num)返回num的自然对数
        • Math.atan2(y,x)返回y/x的反正切值
        • Math.pow(num,power)返回num的power次幂
        • Math.cos(x)返回x的余弦值
        • Math.sqrt(num)返回num的平方根
        • Math.sin(x)返回x的正弦值
        • Math.acos(x)返回x的反余弦值
        • Math.tan(x)返回x的正切值
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值