2010-06-20 75 views
14

如何继承Object.create()?我尝试了这些,但都不是工作:使用Object.create()继承JavaScript?

var B = function() {}; 
var A = function() {}; 
A = Object.create(B); 
A.prototype.C = function() {}; 

var B = function() {}; 
var A = function() {}; 
A.prototype.C = function() {}; 
A = Object.create(B); 

var B = function() {}; 
A = Object.create(B); 
var A = function() {}; 
A.prototype.C = function() {}; 

毫无效果。我该如何使用这个新的Object.create()函数?

+0

可能重复(http://stackoverflow.com/questions/2709612/using-object-create-instead-of-new) – Bergi 2013-01-31 11:47:52

回答

21

Object.create()用于继承对象,而不是像您正在尝试的那样构造函数。它几乎创建了一个新对象,其中旧对象设置为其原型父级。

var A = function() { }; 
A.prototype.x = 10; 
A.prototype.say = function() { alert(this.x) }; 

var a = new A(); 
a.say(); //alerts 10 

var b = Object.create(a); 
b.say(); //alerts 10 
b.x = 'hello'; 
b.say(); //alerts 'hello' 

而只是为了确保b不是只是一个克隆,

a.x = 'goodbye'; 
delete b.x; 
b.say(); //alerts 'goodbye' 
+0

该死的,那么它不适合我的情况。我需要定义一个扩展另一个“类”的“类”。 – Tower 2010-06-20 17:35:42

+0

原型继承的关键在于,由于类也是对象,所以“类”和“对象”之间没有很大的区别。 – Kos 2011-12-09 17:12:41

+0

我希望有'Object.clone(obj)'产生与'JSON.parse(JSON.stringify(obj))'相同的结果。 – trusktr 2014-04-05 21:25:55

0

道格拉斯的Object.create的原始文档是http://javascript.crockford.com/prototypal.html。请确认您已经包含了该方法的定义

if (typeof Object.create !== 'function') { 
    Object.create = function (o) { 
     function F() {} 
     F.prototype = o; 
     return new F(); 
    }; 
} 
+0

..不建议使用该实现,但请参阅:http://stackoverflow.com/questions/5199126/javascript-object-create-not-working-in-firefox – zack 2011-06-14 18:47:20

0

您可以定义的Object.create自己,但如果不是本地人,你将不得不对付它在每一个被列举为循环使用的对象。

到目前为止,只有新的webkit - Safari5和Chrome本身支持它。

+3

Object.create'是Object的一个属性,而不是Object.prototype',所以自己定义它不会将它添加到所有对象的枚举属性列表中。 – kpozin 2012-03-13 14:29:31

16

我用于此的图案是包装每种类型的模块中,并暴露createprototype属性,像这样:

var Vehicle = (function(){ 
     var exports = {}; 
     exports.prototype = {}; 
     exports.prototype.init = function() { 
       this.mph = 5; 
     }; 
     exports.prototype.go = function() { 
       console.log("Going " + this.mph.toString() + " mph."); 
     }; 

     exports.create = function() { 
       var ret = Object.create(exports.prototype); 
       ret.init(); 
       return ret; 
     }; 

     return exports; 
})(); 

然后我可以建立派生类型,如下所示:

var Car = (function() { 
     var exports = {}; 
     exports.prototype = Object.create(Vehicle.prototype); 
     exports.prototype.init = function() { 
       Vehicle.prototype.init.apply(this, arguments); 
       this.wheels = 4; 
     }; 

     exports.create = function() { 
       var ret = Object.create(exports.prototype); 
       ret.init(); 
       return ret; 
     }; 

     return exports; 

})(); 

用这种模式,每种类型都有自己的create()函数。

+0

后续工作:如果我觉得有必要制作类似的东西,或者如果我不知道的话,我会使用Coffeescript。 – 2015-01-07 19:00:38

26

有在JavaScript

建设传承做继承的几种方法。使用,如果你不需要调用构造函数父:

function Rectangle(length, width) { 
    this.length = length; 
    this.width = width; 
} 

Rectangle.prototype.getArea = function() { 
    return this.length * this.width; 
}; 

// inherits from Rectangle 
function Square(size) { 
    this.length = size; 
    this.width = size; 
} 

Square.prototype = Object.create(Rectangle.prototype); 

var rect = new Rectangle(6, 8); 
var square = new Square(10); 

console.log(rect.getArea());    // 48 
console.log(square.getArea());    // 100 
console.log(rect instanceof Rectangle);  // true 
console.log(rect instanceof Object);  // true 
console.log(square instanceof Square);  // true 
console.log(square instanceof Rectangle); // true 
console.log(square instanceof Object);  // true 

构造偷窃。使用,如果需要调用父类的构造函数:

function Rectangle(length, width) { 
    this.length = length; 
    this.width = width; 
} 

Rectangle.prototype.getArea = function() { 
    return this.length * this.width; 
}; 

// inherits from Rectangle 
function Square(size) { 
    Rectangle.call(this, size, size); 
} 

Square.prototype = Object.create(Rectangle.prototype); 

var rect = new Rectangle(6, 8); 
var square = new Square(10); 

console.log(rect.getArea());    // 48 
console.log(square.getArea());    // 100 
console.log(rect instanceof Rectangle);  // true 
console.log(rect instanceof Object);  // true 
console.log(square instanceof Square);  // true 
console.log(square instanceof Rectangle); // true 
console.log(square instanceof Object);  // true 
+0

我看到Square.prototype.constructor = Square;在某个地方,它不需要? – c0ming 2017-05-10 01:30:52

0

那么它的晚年,但对于其他人绊倒在此。您可以在FF和Chrome中使用Object.assign。

在本示例中,当使用create创建Cube时。第一个Object.create(this)用z属性创建对象,然后用Object.assign(obj,Square.create(x,y))它将调用Square.create并返回并将它追加到存储在obj中的Cube中。[使用 “的Object.create” 而不是 “新”]的

var Square = { 
     x: 0, 
     y: 0, 

     create: function(x,y) { 
      var obj = Object.create(this); 
      obj.x = x; 
      obj.y = y; 
      return obj; 
     } 
    }; 

var Cube = { 

     z: 0, 

     create:function(x,y,z) { 
      var obj = Object.create(this); 
      Object.assign(obj, Square.create(x,y)); // assign(target,sources...) 
      obj.z = z; 
      return obj; 
     } 
    }; 

// Your code 
var MyCube = Cube.create(20,30,40); 
console.log(MyCube);