- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有这个原型(prototype)继承链:
function Class1() {};
Class1.prototype.name = 'Main Class';
function Class2() {};
Class2.prototype = Object.create(Class1.prototype);
Class2.prototype.constructor = Class2;
function Class3() {};
Class3.prototype = Object.create(Class1.prototype);
// Note missing reassignment of constructor for Class3
var class2Ins = new Class2();
var class3Ins = new Class3();
console.log(Object.getPrototypeOf(class2Ins)); // Class2 { ... }
console.log(Object.getPrototypeOf(class3Ins)); // Object { ... }
我很困惑,为什么 Object.getPrototypeOf(class3Ins)
是一个对象,尽管它的原型(prototype)是 Class3。
最佳答案
我认为你有点不清楚原型(prototype)委托(delegate)在 JS 中是如何工作的。
首先,当我在浏览器中运行您的代码时,我得到以下输出:
Object { constructor: Class2() }
Object { }
您可能想检查输出是否相同或不同。
<小时/>现在,概念。
When a function is created, the JS engine creates an anonymous object and binds the 2 as follows:
function foo(){} // user created a function
/* The JS engine binds an anonymous object */
// foo.prototype = {};
// foo.prototype.constructor = foo;
另外,
When an object is created by a constructor function using
new
,
the[[Prototype]]
(or.__proto__
) of the created object is
set to the anonymous object referenced (pointed to) by thefunction.prototype
和
<小时/>The
Object.create()
method creates a new object with the specified prototype object and properties.
好吧,这一切意味着什么?
让我们将您的代码分解为多个 block ,就像我们是引擎一样。
function Class1() {};
/*
bind anonymous object:
Class1.prototype = {};
Class1.prototype.constructor = Class1;
*/
Class1.prototype.name = 'Main Class';
function Class2() {};
/*
bind anonymous object:
Class2.prototype = {};
Class2.prototype.constructor = Class2;
*/
Class2.prototype = Object.create(Class1.prototype);
/*
create new object with prototype as the specified object:
Class2.prototype = {};
Object.setPrototypeOf(Class2.prototype, Class1.prototype);
*/
Class2.prototype.constructor = Class2;
function Class3() {};
/*
bind anonymous object:
Class3.prototype = {};
Class3.prototype.constructor = Class3;
*/
Class3.prototype = Object.create(Class1.prototype);
/*
create new object with prototype as the specified object:
Class3.prototype = {};
Object.setPrototypeOf(Class3.prototype, Class1.prototype);
*/
// Note missing reassignment of constructor for Class3
var class2Ins = new Class2();
/*
set [[Prototype]] of the created object to object referenced by the function.prototype:
Object.setPrototypeOf(class2Ins, class2.prototype);
*/
var class3Ins = new Class3();
/*
set [[Prototype]] of the created object to object referenced by the function.prototype:
Object.setPrototypeOf(class3Ins, class3.prototype);
*/
Object.getPrototypeOf(class3Ins)
(简单来说,class2Ins.__proto__
)与Class2.prototype
相同Class2.prototype
是由 Object.create(Class1.prototype)
创建的对象。Class2.prototype.__proto__
与 Class1.prototype
相同。class3Ins.__proto__
与 Class3.prototype
Class3.prototype
是由 Object.create(Class1.prototype)
创建的对象。Class3.prototype.__proto__
与 Class1.prototype
相同。Object.getPrototypeOf(class3Ins)
之所以为空,是因为 Object.create
创建了一个空对象。
如果您还有困惑,请访问:http://www.javascripttutorial.net/javascript-prototype/
这是理解 JS 原型(prototype)概念的绝佳资源。
关于javascript - Object.getPrototypeOf(classInstance) 返回 Object {},我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35517858/
对于下面的代码,我想知道为什么 Object.getPrototypeOf(person) 可以工作,但 person.getPrototypeOf(person) 不起作用?我认为规则是:如果对象没
在this video (大约 31 分钟),Crockford 说他们(代表 ECMAScript 委员会发言)建议不要使用Object.getPrototypeOf。他的解释是,它并不是真正适用于
我正在阅读你不懂JS,并且混淆了 getPrototypeOf 和 isProtytypeOfs 的结果。代码如下: function Foo(name) { this.n
我正在尝试学习 javascript,并且正在从 Mozilla 文档中学习 Object。一切都很好,直到我看到这个例子。 It is the first example with the head
我有以下 Javascript 片段:` var alien = { kind: "alien" }; var person =
我正在阅读this post 。它定义了一个简单的数组: var myArray = [1, 2]; myArray.push(3); myArray.reverse(); myArray.pop()
非常简单: 我有代码使用Object.getPrototypeOf(...)来获取Dojo Widget的继承类(只是一个JS对象)。 IE8 不支持 Object.getPrototypeOf(..
Object.getPrototypeOf(obj) 是如何工作的? 根据定义,Object.getPrototypeOf(obj) 应该返回对象的原型(prototype)属性,或者以其他方式返回与
非常简单: 我有代码使用 Object.getPrototypeOf(...) 获取 Dojo Widget 的继承类(只是一个 JS 对象)。 IE8 不支持 Object.getPrototype
我正在学习一些 JS,我希望有人能用简单的术语向我解释 Object.getPrototypeOf() 与 .prototype 之间的区别 function ParentClass() {} fun
查看这个类的定义: function MyClass (property) { // Private var privateVariable; var privateMetho
所以我知道“null”是一种“对象” 但是,当我执行 Object.getProtoTypeOf(null) 时,我收到错误。 instanceof 也是如此,这有点相同。 如果 null 是一种 O
我很难理解 Object.getPrototypeOf(o)。我正在下面的代码中使用该方法。 var obj0 = {x: 10}; var obj1 = Object.create(null, {
我正在尝试设置一个对象的原型(prototype)。 但是,有时我想设置/获取字符串的原型(prototype)。然而,令人惊讶的是,当我调用时出现错误: var foo = 'baz'; Objec
我有一个 Angular 7 应用程序,当我在 IE11 中运行它时,我收到错误消息“Object.getPrototypeOf: 'this' is not an Object”。只有当我使用 --
Javascript 代理对象的一个应用是通过将数据作为数组的数组以及列出字段名称和每个字段索引的对象(即字段映射)通过网络发送来减少网络流量。 (而不是在每个对象中重复属性名称的对象数组)。 乍
我有这个原型(prototype)继承链: function Class1() {}; Class1.prototype.name = 'Main Class'; function Class2()
我知道后者是非标准的。但是 Object.getPrototypeOf 和 __proto__ 之间有区别吗?我正在研究 javascript 中的原型(prototype)链是如何工作的,并希望弄清
var protoRabbit = {size: "small"}; var fastRabbit = Object.create(protoRabbit); console.log(Object.g
我只是想知道替换 __proto__ 的主要原因是什么功能 getPrototypeOf/setPrototypeOf ? 提前致谢 最佳答案 MDN explains it pretty well.
我是一名优秀的程序员,十分优秀!