`

JS1.8.5新增特性之Object.create

阅读更多

Object.create

Object.create(proto [, propertiesObject ])

通过指定的原型对象和属性创建一个新的对象.

proto:原型对象

propertiesObject:可选属性,也是可配置的,如下:

var obj = Object.create({}, {'a':{'value': 1, 'writable': false, 'enumerable': false, 'configurable': false}});

其中,第二个参数中, 是一个较为特殊的为obj添加属性的方式;其中四个值所代表的含义分别为:

 

value: 表示a 的属性值;

writable: 表示a 的属性值是否可写;[默认为: false]

enumerable: 表示属性a 是否可以被枚举;[默认为: false]

configurable: 表示属性a 是否可以被配置,例如 对obj.a做 delete操作是否允许;[默认为: false]

 

Classical inheritance with Object.create

//Shape - superclass
function Shape() {
  this.x = 0;
  this.y = 0;
}

//superclass method
Shape.prototype.move = function(x, y) {
    this.x += x;
    this.y += y;
    console.info("Shape moved.");
};

// Rectangle - subclass
function Rectangle() {
  Shape.call(this); //call super constructor.
}

//subclass extends superclass
Rectangle.prototype = Object.create(Shape.prototype);
Rectangle.prototype.constructor = Rectangle;

var rect = new Rectangle();

rect instanceof Rectangle //true.
rect instanceof Shape //true.

rect.move(1, 1); //Outputs, "Shape moved."

 

 If you wish to inherit from multiple objects, then mixins are a possibility.

function MyClass() {
     SuperClass.call(this);
     OtherSuperClass.call(this);
}
 
MyClass.prototype = Object.create(SuperClass.prototype); //inherit
mixin(MyClass.prototype, OtherSuperClass.prototype); //mixin
 
MyClass.prototype.myMethod = function() {
     // do a thing
};

Using <propertiesObject> argument with Object.create

 

var o;
 
// create an object with null as prototype
o = Object.create(null);
 
 
o = {};
// is equivalent to:
o = Object.create(Object.prototype);
 
 
// Example where we create an object with a couple of sample properties.
// (Note that the second parameter maps keys to *property descriptors*.)
o = Object.create(Object.prototype, {
  // foo is a regular "value property"
  foo: { writable:true, configurable:true, value: "hello" },
  // bar is a getter-and-setter (accessor) property
  bar: {
    configurable: false,
    get: function() { return 10 },
    set: function(value) { console.log("Setting `o.bar` to", value) }
}})
 
 
function Constructor(){}
o = new Constructor();
// is equivalent to:
o = Object.create(Constructor.prototype);
// Of course, if there is actual initialization code in the Constructor function, the Object.create cannot reflect it
 
 
// create a new object whose prototype is a new, empty object
// and a adding single property 'p', with value 42
o = Object.create({}, { p: { value: 42 } })
 
// by default properties ARE NOT writable, enumerable or configurable:
o.p = 24
o.p
//42
 
o.q = 12
for (var prop in o) {
   console.log(prop)
}
//"q"
 
delete o.p
//false
 
//to specify an ES3 property
o2 = Object.create({}, { p: { value: 42, writable: true, enumerable: true, configurable: true } });

 针对不支持Object.create的浏览器,可使用以下函数:

if (!Object.create) {
    Object.create = function (o) {
        if (arguments.length > 1) {
            throw new Error('Object.create implementation only accepts the first parameter.');
        }
        function F() {}
        F.prototype = o;
        return new F();
    };
}

 

 

 

 

分享到:
评论
发表评论

文章已被作者锁定,不允许评论。

相关推荐

Global site tag (gtag.js) - Google Analytics