es5的特性 有多少你没用过

一:新的对象创建方式

     参数可以是某个对象的原型

function Person(name, age){
    this.name = name;
    this.age = age;
}
Person.prototype.sayHello = function(){
    console.log('hello');
}
Person.prototype.getName = function(){
    console.log(this.name);
}
Person.prototype.getAge = function(){
    console.log(this.age);
}

//创建 jack对象   继承Person的原型
var jack = Object.create(Person.prototype);

这样 jack 就有了 Person的原型属性

二:Object.seal 

让一个对象密封,并返回被密封后的对象。

  1: 不能添加新属性
  2: 不能删除已有属性
  3: 能修改属性的值
  4: 不能修改其可枚举性、可配置性、可写性

Object.defineProperty(foo, 'say',{
    value:"aaa",
    enumerable:false
});

修改其可枚举 失败 enumerable

三:将一个对象改为不可扩展 (不能添加新属性 可删除修改)

Object.preventExtensions(foo);
foo.newName = 'newname';
//添加失败

四:扩展对象的属性 方法

Object.defineProperty(Array, 'test', {
        value: function(){
                return 'ssss';
        },
        configurable: true,
        enumerable: false,
        writable: true
});

调用:Array.test() // ssss
原文地址:https://www.cnblogs.com/heqhbk/p/5120200.html