JS ES5

严格模式 use strict

  • 必须使用var声明变量
  • 禁止自定义函数this指向window
'use strict'
funcion Person(name){
    this.name = name;
}
Person("Tom"); //error
new Person("Tom"); //right
  • 为eval创建作用域
  • 对象属性名不能重复

JSON对象

  • JSON.stringify(obj/arr) js对象(数组)转json对象(数组)
  • JSON.parse(json) json对象转(数组)js对象(数组)

Object扩展

  • Object.create(prototype, [descriptors]) 以指定对象为原型创建新的对象
var man = {sex:'mail'};
var person = Object.create(man, {
    name: {
        value: '',
        writable: true, //可写
        configurable: true, //可配置(可删除)
        enumerable: true //可枚举
    }
})
person.name = 'Tom';
for (let value in person) {
    console.log(value); //name sex
}
  • Object.defineProperties(object, descriptors) 为指定对象定义扩展多个属性
var obj1 = {
    firstName: 'Kevin',
    lastName: 'Tseng'
};
Object.defineProperties(obj1, {
    fullName: {
        //获取
        get: function(){
            return this.firstName + '-' + this.lastName;
        },
        //监听
        set: function(data){
            var nameArr = data.split("-");
            this.firstName = nameArr[0];
            this.lastName = nameArr[1];
        }
    }
})
console.log(obj1.fullName); //Kevin-Tseng
obj1.fullName = "Tom-Smith";
console.log(obj1.fullName, obj1.firstName); //Tom-Smith Tom

Array扩展

  • indexOf()
  • lastIndexOf()
  • forEach(function(item, index){})
  • map(function(item, index){})
  • filter(function(item, index){})

Function扩展

  • call() Function.call(obj, arguments) 将函数送给某对象,立即执行
  • apply() Function.apply(obj, [arguments]) 将函数送给某对象,立即执行
  • bind() Function.call(obj, arguments) 将函数送给某对象,调用执行
var obj = {name: 'Kevin'};
function foo(data){
    console.log(this, data);
}
foo.call(obj, 33);//{name: "Kevin"} 33
foo.apply(obj, [28]);//{name: "Kevin"} 28
foo.bind(obj)(22); //{name: "Kevin"} 22

setTimeout(function(data){
    console.log(this, data); //{name: "Kevin"} 18
}.bind(obj, 18),1000);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值