在 JavaScript 中实例化一个对象真的只是以某种方式继承吗?

发布于 2024-11-16 04:34:50 字数 349 浏览 2 评论 0原文

我正在阅读一本 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一个人的旅程 2024-11-23 04:34:50

不。您会感到困惑,因为完成继承(即原型继承)的方法之一采用给定对象作为原型。获取对象的一种方法是通过构造函数。

换句话说,someObject.prototype = new SomeOtherObject() 没有什么特别的。您也可以这样做

someObject.prototype = { myObjectLiteral: "isJustAsGoodAsYourConstructoredInstance" };

,甚至someObject.prototype = false。从字面上看,这两个操作彼此无关。


编辑为了解决他书中找到的引用OP:

“回忆一下之间的关系
构造函数、原型和
实例:每个构造函数都有一个
指向的原型对象
构造函数和实例都有一个
指向原型的内部指针。”

不幸的是,这充其量是误导性的,最坏的情况是完全错误的。考虑一下“每个构造函数都有一个指向构造函数的原型对象”意味着什么。这意味着,例如,

Array.prototype === Array
Function.prototype === Function

function CustomConstructor() { }
CustomConstructor.prototype === CustomConstructor

但是所有如果您在控制台中输入这些语句,则它们会输出 false

现在考虑“实例有一个指向原型的内部指针。”这是正确的,但与他所使用的术语不符。意思是:

var myProto = {};

function CustomConstructor() { }
CustomConstructor.prototype = myProto; // inherit from the myProto object.

var x = new CustomConstructor(); // x is an "instance"
Object.getPrototypeOf(x) === myProto; // Object.getPrototypeOf fetches the "internal pointer"

但如你所见,我没有使用构造函数继承,正如我上面试图指出的那样,实例化对象无论如何都不是继承,构造函数的 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 do

someObject.prototype = { myObjectLiteral: "isJustAsGoodAsYourConstructoredInstance" };

or 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:

"Recall the relationship between
constructors, prototypes, and
instances: each constructor has a
prototype object that points back to
the constructor, and instances have an
internal pointer to the prototype."

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,

Array.prototype === Array
Function.prototype === Function

function CustomConstructor() { }
CustomConstructor.prototype === CustomConstructor

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:

var myProto = {};

function CustomConstructor() { }
CustomConstructor.prototype = myProto; // inherit from the myProto object.

var x = new CustomConstructor(); // x is an "instance"
Object.getPrototypeOf(x) === myProto; // Object.getPrototypeOf fetches the "internal pointer"

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 placing extends 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."

许一世地老天荒 2024-11-23 04:34:50

在不使用 objectType.inherits() 的情况下,如果我了解了继承的概念,那么如果您只采用正确的部分:

new someOtherObject()

这是 someOtherObject 的实例化。

这里发生的情况是,您正在创建一个 someOtherObject 类型的对象,该对象从一个空对象继承结构(前提是您尚未为此设置原型)。因为prototype的默认值是一个空对象{}

您将此对象作为 someObject 的原型:

someObject.prototype = new someOtherObject();

现在,如果您创建 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:

new someOtherObject()

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:

someObject.prototype = new someOtherObject();

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文