Object.getPrototypeOf()混淆 [英] Object.getPrototypeOf() confusion

查看:94
本文介绍了Object.getPrototypeOf()混淆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Object.getPrototypeOf(obj)如何工作?

How Object.getPrototypeOf(obj) works?

根据定义,Object.getPrototypeOf(obj)应该返回Object的prototype属性,或者以其他方式返回它as obj.constructor.prototype。

As per definition Object.getPrototypeOf(obj) should return prototype property of an Object or in another way it is same as obj.constructor.prototype.

使用new创建的对象使用其构造函数的prototype属性值作为原型。

Objects created with new use the value of the prototype property of their constructor function as their prototype.

让我们举个例子:

>element = document.getElementById("test")

>a = Object.getPrototypeOf(element)
HTMLDivElement

让我们比如HTMLDivElement是元素的原型。

Let's say HTMLDivElement is the prototype of element.

>a.constructor.prototype
HTMLDivElement

所以a.constructor.prototype是HTMLDivElement,因此Object.getPrototypeOf(a)应返回HTMLDivElement,但它返回HTMLElement。我对getPrototypeOf()的定义感到困惑。

so a.constructor.prototype is HTMLDivElement so Object.getPrototypeOf(a) should return HTMLDivElement but it returns HTMLElement. I am totally confused with definition of getPrototypeOf().

>b = Object.getPrototypeOf(a)

HTMLElement ---->为什么? a.constructor.prototype是HTMLDivElement

HTMLElement ----> why? a.constructor.prototype is HTMLDivElement

实际上它正在返回原型的 proto 属性,根据getPrototypeOf()的定义是不是错了?

Actually it's returning proto property of prototype, isn't it wrong as per definition of getPrototypeOf()?

>a.constructor.prototype.__proto__
 HTMLElement


推荐答案

引自 https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Inheritance_Revisited


<对于来自Java或C ++的开发人员而言,JavaScript有点令人困惑,
因为它都是动态的,所有运行时,并且它根本没有类。所有只是实例(对象)的
。即使我们模拟的类也只是
a函数对象。

JavaScript is a bit confusing for developers coming from Java or C++, as it's all dynamic, all runtime, and it has no classes at all. It's all just instances (objects). Even the "classes" we simulate are just a function object.






请注意原型也是一个对象,因此它也可以拥有独特的原型

所以让你混淆的代码看起来像这样

so code that make you confuse look like this

a = Object.getPrototypeOf(element)
b = Object.getPrototypeOf(a)

可以翻译成这个

a = element.__proto__
b = element.__ptoto__.__proto__ 

我认为现在清除 a!= b

I think it's now clear that a != b

1 )JavaScript 中的每个对象都有一个原型,您可以通过 __ proto __ 属性

1) Every object in JavaScript has a prototype, you can access it through the __proto__ property

2)功能在Javascript中也是对象

2) Function is also an object in Javascript

3)功能还有原型属性

4)我们可以通过使用关键字 new

4) We can create objects in JavaScript by calling function with keyword new

4) 原型初始 __ proto __ 用于创建的任何 >

4) Function prototype is the initial __proto__ for any objects created by them

要创建新对象,我们可以写这样的东西

To create new object we can write something like this

//here we define a function
function SomeFunctionThatCreateObject() {
    this.someStringProperty = "blablabla";
} 

var obj = new SomeFunctionThatCreateObject(); //we create new object with function

var p = Object.getPrototypeOf(obj);

此代码等于此

var SomeFunctionThatCreateObject = function(@this) {
    @this.someStringProperty = "blablabla";
    return @this;
};

SomeFunctionThatCreateObject.prototype = {}; //note that prototype is also an object

var obj = {};

obj = SomeFunctionThatCreateObject(obj);

obj.constructor = SomeFunctionThatCreateObject;

obj.__proto__ = SomeFunctionThatCreateObject.prototype;

var p = obj.__proto__;






PS:还读这个
< a href =https://stackoverflow.com/a/9220317/474290> https://stackoverflow.com/a/9220317/474290

https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Inheritance_Revisited

这篇关于Object.getPrototypeOf()混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆