在 JavaScript 中实例化一个对象真的只是以某种方式继承吗?
我正在阅读一本 JavaScript 书,当我阅读有关继承的章节时,我想知道每次创建对象实例时是否真的会发生继承,特别是因为它们很相似。
示例:原型链接 - someObject.prototype = new someOtherObject();
实例化对象 var myArray = new Array()
类似吧?
另外,这本书(JavaScript for Web Developers,作者:Nicholas Zakas)指出,实例和构造函数的原型之间存在链接。(该链接通常称为 __proto__ )
因此可以认为实例化一个对象就像继承?
I was reading a JavaScript book and as I was reading a chapter about inheritance, I wondered if every time you create a instance of a object is it really inheritance that happens, especially since they are similar.
Examples: Prototype Chaining - someObject.prototype = new someOtherObject();
Instantiating an object var myArray = new Array()
Similar right?
Additionally the book(JavaScript for web developers, By:Nicholas Zakas) stated, a link exists between an instance and a constructor's prototype.(The link is usually known as __proto__
)
So can one argue that instantiating a object is like inheritance?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不。您会感到困惑,因为完成继承(即原型继承)的方法之一采用给定对象作为原型。获取对象的一种方法是通过构造函数。
换句话说,
someObject.prototype = new SomeOtherObject()
没有什么特别的。您也可以这样做,甚至
someObject.prototype = false
。从字面上看,这两个操作彼此无关。编辑为了解决他书中找到的引用OP:
不幸的是,这充其量是误导性的,最坏的情况是完全错误的。考虑一下“每个构造函数都有一个指向构造函数的原型对象”意味着什么。这意味着,例如,
但是所有如果您在控制台中输入这些语句,则它们会输出
false
现在考虑“实例有一个指向原型的内部指针。”这是正确的,但与他所使用的术语不符。意思是:
但如你所见,我没有使用构造函数继承,正如我上面试图指出的那样,实例化对象无论如何都不是继承,构造函数的
prototype
属性是完成原型继承的方式,就像经典继承一样。是通过在 Java 中的类定义之后放置extends BaseClass
来实现的。反过来,存在这种差异是因为原型继承允许您继承原型对象实例,而不是继承一个对象实例。根据类。JavaScript Garden 有一些不错的部分,如果您想了解更多信息,这些部分可能会有所帮助。相关的是“原型”、“构造函数”和“实例操作符”。
No. You are confused because one of the methods of accomplishing inheritance (viz. prototypal inheritance) takes, as the prototype, a given object. And one method of acquiring objects is via constructors.
In other words, there is nothing special about
someObject.prototype = new SomeOtherObject()
. You could just as well door even
someObject.prototype = false
. The two operations, very literally, have nothing to do with each other.EDIT To address the quote OP found in his book:
Unfortunately, this is misleading at best, and plain wrong at worst. Consider what "each constructor has a prototype object that points back to the constructor" would mean. It would mean that, for example,
But all of these statements output
false
if you type them in your console.Now consider "instances have an internal pointer to the prototype." This is true, but does not fit with the terminology he is using. What this means is:
But as you can see, I didn't inherit using a constructor, as I tried to point out above. Instantiating an object is not inheritance in any way. It is simply that the
prototype
property of constructors is how prototypal inheritance is accomplished, just like classical inheritance is accomplished by placingextends BaseClass
after a class definition in Java. In turn, this difference is present because prototypal inheritance lets you inherit a prototype object instance, not a base class.The JavaScript Garden has a few good sections that might help if you want more information. Relevant are "The Prototype," "Constructors," and "The instanceof Operator."
在不使用 objectType.inherits() 的情况下,如果我了解了继承的概念,那么如果您只采用正确的部分:
这是 someOtherObject 的实例化。
这里发生的情况是,您正在创建一个 someOtherObject 类型的对象,该对象从一个空对象继承结构(前提是您尚未为此设置原型)。因为prototype的默认值是一个空对象
{}
。您将此对象作为
someObject
的原型:现在,如果您创建 someObject 的实例,将会发生以下情况:
您创建的 someObject 继承了该时间点 someOtherObject 的结构,该对象继承了 (空)对象已被实例化。
也许更好的解释是,实例在该时间点以其原型的结构进行扩展,并将所有继承措辞放在一边,因为仅使用它可能会产生误导。由于经典继承使用基类(类型),而原型使用这些基类的实例。现在这一切都取决于你如何看待继承,而争论在 javascript 中根本不存在(对象类型在运行时是可变的),正如卡尔·马克思认为真正的共产主义在概念上根本不可能。如果您同意最后的陈述,那么比较和询问它并不重要,因为这两种情况都不是。
Without using objectType.inherits() if I got your concept of inheritance then it is if you take just the right part:
This is instantiation of someOtherObject.
What happens here is you are creating an object of type someOtherObject that inherits structure from an empty object (provided you have not set a prototype on this). Because the default value of prototype is an empty object
{}
.You are putting this object as prototype of
someObject
:Now if you create an instance of someObject this will happen:
You create someObject that inherits the structure of someOtherObject in that point in time, which inherits the structure of (empty)object when it has been instanced.
Maybe a better explanation is saying that the instance gets extended with the structure of its prototype at that point in time, and leave all of the inheriting wording aside, since just using that can mislead. Since classic inheritance uses base classes (types) and proto uses instances of those. Now it all depends on how you look at inheritance, which to argue about does not exists at all in javascript (object types are mutable at runtime) as by Karl Marx argues that real communism is not possible at all in the concept. If you agree with this last statement then comparing and asking about it does not matter, because neithercase is.