JS1.8.5新增特性之Object.create

本文介绍了如何使用JavaScript的Object.create方法来创建具有指定原型的新对象,并展示了如何利用此方法实现类的经典继承。此外,还提供了如何设置属性描述符的具体示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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();
    };
}

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值