JavaScript 通过 Object.create() 继承?
如何使用 Object.create() 进行继承?我尝试了这些,但没有一个起作用:
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() 函数?
How do I inherit with the Object.create()? I tried these, but none are working:
var B = function() {};
var A = function() {};
A = Object.create(B);
A.prototype.C = function() {};
and
var B = function() {};
var A = function() {};
A.prototype.C = function() {};
A = Object.create(B);
and
var B = function() {};
A = Object.create(B);
var A = function() {};
A.prototype.C = function() {};
Nothing worked. How am I supposed to use this new Object.create()-function?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
JavaScript 中有多种实现继承的方法
构造继承。如果不需要调用超类型构造函数,则使用:
构造函数窃取。如果需要调用超类型构造函数,则使用:
There are several ways of doing inheritance in JavaScript
Construction Inheritance. Used if you don't need to call supertype constructor:
Constructor Stealing. Used if need to call supertype constructor:
Object.create()
用于继承对象,而不是像您尝试做的那样继承构造函数。它几乎创建了一个新对象,并将旧对象设置为其原型父对象。只是为了确保 b 不只是 a 的克隆,
Object.create()
is used to inherit objects, not constructors like you're trying to do. It pretty much creates a new object with the old object set as its prototypal parent.And just to make sure b is not just a clone of a,
我为此使用的模式是将每种类型包装在模块中,并公开
create
和prototype
属性,如下所示:我可以像这样构建派生类型:
然后 模式,每种类型都有自己的 create() 函数。
The pattern I use for this is to wrap each type in a module, and expose
create
andprototype
properties, like so:Then I can build derived types like so:
with this pattern, each type has its own
create()
function.Douglas 的 Object.create 的原始文档位于此处 http://javascript.crockford.com/prototypal.html 。确保您已包含该方法的定义
The original documentation for Douglas' Object.create is here http://javascript.crockford.com/prototypal.html . Make sure you have included the definition of the the method
您可以自己定义 Object.create,但如果它不是本机的,您将不得不处理在用于对象的每个 for in 循环中枚举它的情况。
到目前为止,只有新的 webkit - Safari5 和 Chrome 本身支持它。
You can define Object.create yourself, but if it is not native you will have to deal with it being enumerated in every for in loop you use for objects.
So far only new webkits- Safari5 and Chrome natively support it.
您可以在 Mozilla 开发中心找到有关 JavaScript 继承的有用信息。
You can find useful information about JavaScript inheritance on Mozilla Development Center.
好吧,已经晚了好几年了,但对于其他偶然发现这一点的人来说。您可以在 FF 和 Chrome 中使用 Object.assign。
在此示例中,当使用 create 创建立方体时。首先 Object.create(this) 创建具有 z 属性的对象,然后使用 Object.assign(obj, Square.create(x,y)) 它将调用 Square.create 并返回并将其附加到存储在 obj 中的 Cube 中。
Well it's years late, but for anyone else stumbling upon this. You can use Object.assign in FF and Chrome.
In this example when the Cube is being made with create. First Object.create(this) creates the object with the z property, then with Object.assign(obj, Square.create(x,y)) it will call the Square.create and return and append that into Cube being stored in obj.