JavaScript 基于原型的继承的好例子
我使用 OOP 语言进行编程已经有 10 多年了,但我现在正在学习 JavaScript,这是我第一次遇到基于原型的继承。我倾向于通过研究好的代码来学得最快。正确使用原型继承的 JavaScript 应用程序(或库)的编写良好的示例是什么?您能否(简要地)描述如何/在何处使用原型继承,以便我知道从哪里开始阅读?
I have been programming with OOP languages for over 10 years but I'm learning JavaScript now and it's the first time I've encountered prototype-based inheritance. I tend to learn fastest by studying good code. What's a well-written example of a JavaScript application (or library) that properly uses prototypal inheritance? And can you describe (briefly) how/where prototypal inheritance is used, so I know where to start reading?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
如前所述,道格拉斯·克罗克福德 (Douglas Crockford) 的电影很好地解释了原因和方法。但用几行 JavaScript 来说:
然而,这种方法的问题是,每次创建对象时它都会重新创建对象。另一种方法是在原型堆栈上声明对象,如下所示:
在内省时有一个轻微的缺点。转储 testOne 将导致有用信息减少。此外,“testOne”中的私有属性“privateVariable”在所有实例中共享,shesek 的回复中也提到了这一点。
As mentioned, the movies by Douglas Crockford give a good explanation about the why and it covers the how. But to put it in a couple of lines of JavaScript:
The problem with this approach however, is that it will re-create the object every time you create one. Another approach is to declare your objects on the prototype stack, like so:
There is a slight downside when it comes to introspection. Dumping testOne, will result in less useful information. Also the private property "privateVariable" in "testOne" is shared in all instances, als helpfully mentioned in the replies by shesek.
Douglas Crockford 在 JavaScript 原型继承 上有一个不错的页面:
Dean Edward 的 Base.js、Mootools 类 或 John Resig 类简单继承工作是在JavaScript中执行经典继承的方法。
Douglas Crockford has a nice page on JavaScript Prototypal Inheritance:
Dean Edward's Base.js, Mootools's Class or John Resig's Simple Inheritance works are ways to do classical inheritance in JavaScript.
我会看一下 YUI 和 Dean Edward 的
Base
库: http://dean.edwards.name/weblog/2006/03/base/对于 YUI,您可以快速查看 lang 模块,尤其是。 YAHOO.lang.extend方法。然后,您可以浏览一些小部件或实用程序的源代码并查看它们如何使用该方法。
I would take a look at YUI, and at Dean Edward's
Base
library: http://dean.edwards.name/weblog/2006/03/base/For YUI you can take a quick look at the lang module, esp. the YAHOO.lang.extend method. And then, you can browse the source of some widgets or utilities and see how they use that method.
还有 Microsoft 的 ASP.NET Ajax 库,http://www.asp.net/ajax/。
还有很多不错的 MSDN 文章,包括创建高级采用面向对象技术的 Web 应用程序。
There's also Microsoft's ASP.NET Ajax library, http://www.asp.net/ajax/.
There are a lot of good MSDN articles around as well, including Create Advanced Web Applications With Object-Oriented Techniques.
这是我发现的最清晰的例子,来自 Mixu 的 Node 书籍 (http://book.mixu. net/node/ch6.html):
This is the clearest example I've found, from Mixu's Node book (http://book.mixu.net/node/ch6.html):
ES6
class
和extends
ES6
class
和extends
只是以前可能的语法糖原型链操作,因此可以说是最规范的设置。首先了解有关原型链和
.
属性查找的更多信息:https://stackoverflow.com/a/ 23877420/895245现在让我们来解构发生的情况:
没有所有预定义对象的简化图:
ES6
class
andextends
ES6
class
andextends
are just syntax sugar for previously possible prototype chain manipulation, and so arguably the most canonical setup.First learn more about the prototype chain and
.
property lookup at: https://stackoverflow.com/a/23877420/895245Now let's deconstruct what happens:
Simplified diagram without all predefined objects:
我建议查看 PrototypeJS 的 Class.create:
第 83 行 @ http://prototypejs.org/assets/2009/8/31 /prototype.js
I suggest looking at PrototypeJS' Class.create:
Line 83 @ http://prototypejs.org/assets/2009/8/31/prototype.js
我见过的最好的例子是 Douglas Crockford 的 JavaScript:好的部分。它绝对值得购买,可以帮助您对语言有一个平衡的看法。
Douglas Crockford 负责 JSON 格式,在雅虎担任 JavaScript 专家。
The best examples I've seen are in Douglas Crockford's JavaScript: The Good Parts. It's definitely worth buying to help you get a balanced view on the language.
Douglas Crockford is responsible for the JSON format and works at Yahoo as a JavaScript guru.
有一个片段 JavaScript Prototype-based Inheritance 与 ECMAScript 版本特定实施。它会根据当前运行时自动选择使用 ES6、ES5 和 ES3 实现。
There is a snippet JavaScript Prototype-based Inheritance with ECMAScript version specific implementations. It will automatically choose which to use between ES6, ES5 and ES3 implementations according to current runtime.
在 JavaScript 中添加基于原型的继承的示例。
ES6 使用更简单的继承实现构造函数和 super 关键字。
Adding an example of prototype-based inheritance in JavaScript.
ES6 uses a far easier implementation of inheritance with the use of constructor and super keywords.