优步和原型之间的Javascript差异
我在javaScript中比较新,我正在做一些继承。我以为我知道原型是什么,但后来我遇到了超级方法。现在我不知道这两者之间的区别。
我知道优步就像java中的super一样。但是原型是令我烦恼的东西。
如果你能给我一些使用这两个的简单例子,我会非常感激。
I'm relatively new in javaScript and I was doing some inheritance. I thought I know what prototype is but then i met with uber method. Now I don't know the difference between those two. I know that uber is like super in java and that's all. But then prototype is something that bothers me. If you can give me some simple example of using those two I would appreciate it very.
uber
只是道格拉斯·克罗克福德在他的JavaScript继承示例中创建的 sugar 方法,它可以帮助devoloper处理JavaScripts原型非常灵活的特性继承。
uber
is just a sugar method Douglas Crockford created in his examples of inheritance in JavaScript which should help the devoloper when working with the very, very flexible nature of JavaScripts prototypal inheritance.
本机JavaScript中不存在此方法。
他他详细解释了他使用的 sugar 方法此处 。
He explains the sugar methods he uses in detail here.
在他的例子中,他将 uber
方法定义为帮助方法访问方法的父实现。
In his examples he defines the uber
method as a helper method to access the parent implementation of a method.
假设你有一个类(我使用这个术语来缓解这个例子;严格说来没有JavaScript中的类) Human
,其中 walk
方法。如果您现在在 Infant
类中扩展此类,则可以以婴儿的方式覆盖 walk
因为它无法行走所以只能爬行。
Let's assume that you have a "class" (I use this term to ease the example; strictly spoken there are no classes in JavaScript) Human
which has a walk
method. If you now "extend" this class in an Infant
class, you could overwrite walk
in such a way that the infant only crawls since it can't walk.
这显然不是一个很好的例子,但我希望你明白这一点。
在这种情况下,您可以使用Douglas Crockfords uber
方法来访问 Human
在婴儿
类中实施步行
。
In such a case you could use Douglas Crockfords uber
method to access the Human
implementation of walk
in the Infant
"class".
比较JavaScripts原生原型
对象和Douglas Crockfords uber
方法没有任何意义,因为两者都有完全不同的用途。
To compare JavaScripts native prototype
object and Douglas Crockfords uber
method would make no sense, since both serve completly different purposes.
如果你想了解有关JavaScripts的更多信息 prototype
你可以看看这个问题。
If you want more information on JavaScripts prototype
you can take a look at this question.