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

你可能感兴趣的:(JS ES5)