javascript 复制原型进行继承?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11488540/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Copy prototype for inheritance?
提问by SpaceFace
I was playing around with JavaScript in particular simulating object oriented programming with classes and whatnot.
我在玩 JavaScript,特别是用类和诸如此类的东西来模拟面向对象的编程。
I knew about this way of achieving inheritance
我知道这种实现继承的方式
MyClass.prototype = new AnotherClass();
But I wasn't satisfied, I didn't like how I needed to call the constructor of AnotherClass
. So I was playing around and I came up with something that seemed to work and basically want a second opinion.
但我并不满意,我不喜欢我需要调用AnotherClass
. 所以我在玩,我想出了一些似乎有效的东西,基本上想要第二个意见。
function clone (obj)
{
function CloneFactory () {}
CloneFactory.prototype = obj;
return new CloneFactory();
}
MyClass.prototype = clone(AnotherClass.prototype);
By cloning the prototype we get a new copy of it and assign that to MyClass
's prototype so that changing the inherited properties will not affect the parent's prototype's properties. Like this would MyClass.prototype = AnotherClass.prototype
.
通过克隆原型,我们获得了它的一个新副本,并将其分配给MyClass
的原型,以便更改继承的属性不会影响父原型的属性。像这样会MyClass.prototype = AnotherClass.prototype
。
I ran stress tests and this is more efficient under certain circumstances, i.e. when there's a lot of code in the parent's constructor, otherwise it's about the same. Another benefit (or at least I find it to be beneficial) is that it allows to some extent information hiding from the subclasses. Any privileged methods and members will NOT be inherited.
我进行了压力测试,这在某些情况下效率更高,即当父的构造函数中有很多代码时,否则大致相同。另一个好处(或者至少我觉得它是有益的)是它允许在某种程度上隐藏子类的信息。任何特权方法和成员都不会被继承。
Is there some major pitfall that I'm overlooking?
我忽略了一些主要的陷阱吗?
I'm not an expert with JavaScript, actually I'm fairly new to JavaScript, so I'd like to have a second opinion on this because I can't seem to find anything through Google. I don't want to implement bad code :)!
我不是 JavaScript 专家,实际上我对 JavaScript 还很陌生,所以我想对此提出第二意见,因为我似乎无法通过 Google 找到任何内容。我不想实现糟糕的代码:)!
回答by Kevin Ennis
This is almostexactly what Object.create
does. The function you've written is a pretty standard "polyfill" for that method.
这几乎就是Object.create
这样做的。您编写的函数是该方法的一个非常标准的“polyfill”。
This is a really common way of abstracting object creation in a way that more closely reflects "true" prototypal inheritance. Definitely a safe way to do things.
这是一种非常常见的抽象对象创建方式,它更接近地反映了“真正的”原型继承。绝对是一种安全的做事方式。
Oh, and here's a link to the MDN entry for Object.create
, if you're interested: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/create/
哦,Object.create
如果您有兴趣,这里是 MDN 条目的链接:https: //developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/create/
You'll notice at the bottom that they actually include the polyfill, which is pretty much identical to your code, save for some safety checks and variable names.
您会在底部注意到它们实际上包含了 polyfill,它与您的代码几乎相同,除了一些安全检查和变量名称。