开发者

Best JavaScript inheritance implementation [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 11 years ago.

In JavaScript what sort of inheritance do you favour? The ECMA standard is to use prototype chaining. So I just set the prototype of the object that's doing the inheriting (monkey) to invoke a new 开发者_C百科instance of the object I want to inherit from (animal):

monkey.prototype = new animal();

I am aware there are other ways to achieve inheritance. i.e inheriting only the prototype, parasitic inheritance, deep copy, shallow copy.

Can anyone enlighten me and tell me if there is one in particular I should use? One that has benefits that outweigh the other methods.


Theres a lot better article on it here than I could ever write. http://www.crockford.com/javascript/inheritance.html

In general it depends on what you want to achieve. In general though I think duck typing is what is considered best for javascript.


I posted an article that talks about the in and outs of JS inheritance. It's relevant if you're trying to emulate class based inheritance. I don't like Douglas Crockford's approach that tries to stay away from the new keyword. I learned class based inheritance with C++ and Java and find it much easier to think about http://js-bits.blogspot.com/2010/08/javascript-inheritance-done-right.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜