简体   繁体   English

为什么当我在此JavaScript对象上使用反射时,看不到其原型对象中定义的属性?

[英]Why when I use reflection on this JavaScript object I can't see a property defined in its prototype object?

I am pretty new in JavaScript and I am actually studying the reflection concept on a tutorial. 我是JavaScript的新手,实际上我正在教程中研究反射概念。

So I have the following code: 所以我有以下代码:

/* REFLECTION AND EXTEND:

REFLECTION: An object can look at itself listing and changing its properties and methods. So it means that a JavaScript object have the ability to look at its own properties and methods. We can use this feature of the JavaScript language to implement a very userful pattern called EXTEND.
*/


/* This is a generic person object that have 'firstname' and 'lastname' peropertiest setted on a 'Default' value. This generic person object provide also a getFullName() method that return the concatenation of the firstname and the lastname
*/
var person = {
    firstname: 'Default',
    lastname: 'Default',
    getFullName: function() {
        return this.firstname + ' ' + this.lastname;  
    }
}

/* This john object represent a specific person but do not provide the getFullName() method: */
var john = {
    firstname: 'John',
    lastname: 'Doe'
}

// don't do this EVER! for demo purposes only !!! 
/* This set the 'person' object on the proto variable of the john object. So from this john object I can also use the getFullName() method defined for the generic person object: */
john.__proto__ = person;


// Now, we are going to see an example of the use of reflection in JavaScript:


for (var prop in john) {            // Iterate on all the properties into the john object (prop is the current property during the iteration)
    if (john.hasOwnProperty(prop)) {
        // propo is the current property name and john[prop] is the related value of this property into the john object:
        console.log(prop + ': ' + john[prop]);      
    }
}

The code is pretty neat and the comment explain what it does. 代码非常简洁,注释说明了它的作用。

So I have a generic person object that provide the getFullName() method and I have also the john object that represent a specific person but that have not dfined the getFullName() method. 因此,我有一个提供了getFullName()方法的普通人对象,还有一个代表特定人但没有定义getFullName()方法的john对象。

Then I do: 然后我做:

john.__proto__ = person;

that say that the person object is the prototype of the john object so it means that the john object inherite from the person object. 那就是说person对象是john对象的原型,所以这意味着john对象是从person对象继承的。 So I will expect that now the john object have access alsto at the getFullName() method. 因此,我希望现在john对象也可以通过getFullName()方法访问。

Then I use reflection to iterate on the john object and I print all the properties of this objects. 然后,我使用反射来迭代john对象,并打印该对象的所有属性。

I obtain this result: 我得到这个结果:

firstname: John
lastname: Doe

This seems to me pretty strange because, as I can see also in the tutorial, I would expect to see also this property: 在我看来这很奇怪,因为正如我在本教程中也可以看到的那样,我希望也能看到该属性:

getFullName: function() {
    return this.firstname + ' ' + this.lastname;  
}

because now I have access to it. 因为现在我可以使用它。

Why I can't see it? 为什么我看不到? What am I missing? 我想念什么? What is the problem? 问题是什么?

As one can see Juhana has already answered it in the comments but i would like to add my explanations. 可以看到Juhana已经在评论中回答了,但我想补充一下我的解释。

When you do: 当您这样做时:

john.__proto__ = person;

I would say it connects two objects as Object john has prototype of person . 我可以说它连接两个对象,因为Object john具有person原型。 So john has the access to all the properties available in the person object. 因此,john可以访问person对象中所有可用的属性。

Still this does not adds the person object's properties to object john but they have a connection between them as per __proto__ . 但这仍未将person对象的属性添加到john对象,但是根据__proto__ ,它们之间具有联系。 So, john still has its own props as is and it can access the method in the person object. 因此,john仍然拥有自己的道具,并且可以访问person对象中的方法。

This condition: 这种情况:

if (john.hasOwnProperty(prop)) {

does not let it log properties of it's prototype. 不允许它记录其原型的属性。

hasOwnProperty limits the property access to the current object in the iteration. hasOwnProperty将属性访问限制为迭代中的当前对象。

First of all avoid using proto as its not same in all browsers. 首先,避免在所有浏览器中使用proto,因为proto并非相同。 now proto add things to prototype ie making inheritance. 现在, proto将事物添加到原型中,即进行继承。 Upon using john.hasOwnProperty(prop) will be true only for the own properties and not for inherited properties,thats why getFullName 使用john.hasOwnProperty(prop)仅适用于自己的属性,而不适用于继承的属性,这就是为什么getFullName

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么不能使用原型将属性绑定到对象? - Why can't I bind a property to an object using the prototype? 如何查看 Javascript 对象的原型链? - How can I see a Javascript object's prototype chain? 为什么当我创建对象时,控制台说它的原型是Object,而在JS中它是Object.prototype? - Why when I create an object the console says that its prototype is Object when it is Object.prototype in JS? 为什么在定义对象时不能在(JavaScript)Worker中使用它? - Why can't I use this in (JavaScript) Worker when defining an object? 为什么在创建此 javascript object 文字时不能使用“this”? - Why can't I use 'this' when creating this javascript object literal? 为什么不能枚举定义为不可枚举的对象属性? - Why can't I enumerate object property defined as not enumerable? Javascript原型属性定义为对象 - Javascript prototype property defined as object 为什么不能通过其原型访问类中“ this”上的属性? - Why can’t I access a property on “this” in a class via its prototype? 创建js对象时,为什么不能在另一个已定义函数中使用已定义函数? - Why can't I use a defined function inside another defined function,when creating an js object? 为什么在不使用原型,调用或应用的情况下不能向对象添加属性? - Why can't I add a property to an object without using prototype, call or apply?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM