什么是“OWN” javascript中的属性 [英] what is an "OWN" property in javascript

查看:90
本文介绍了什么是“OWN” javascript中的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里我有一个使用getter和setter来定义和获取属性值的代码。我使用对象构造函数创建了一个对象。我在for ... in循环中传递了对象。还使用了getOwnPropertyNames()方法object.Here是结果

Here i have a code which uses getter and setter to define and fetch a property value.I created an object using a object constructor.I passed the object in a for...in loop.Also used getOwnPropertyNames() method on the object.Here are the results


fullName属性可以在for ... in循环中访问

"fullName" property is accessible in for...in loop

fullName在getOwnPropertyNames()方法中不可见。这意味着它是

"fullName" is not visible in getOwnPropertyNames() method.That means it is

不是自己的属性

这里我有两个基本问题。什么是自己的属性?如果fullName不是自己的属性那么是什么类型属性是什么?

Here i have two basic question.What is an own property? If "fullName" is not a own property then what type of property it is ?

function Name(first, last) {
    this.first = first;
    this.last = last;
}

Name.prototype = {
    get fullName() {
        return this.first + " " + this.last;
    },

    set fullName(name) {
        var names = name.split(" ");
        this.first = names[0];
        this.last = names[1];
    }
};
var obj=new Name('al','zami');
for(var i in obj){
   console.log(i); // fullName is here
}
console.log(Object.getOwnPropertyNames(obj)); // fullName is not here


推荐答案

hasOwnProperty getOwnPropertyNames 引用直接分配给对象的属性,而不是只能通过对象的原型链访问。 this.foo = bar.foo = 计为自己的属性,因为您要分配给实例。

hasOwnProperty and getOwnPropertyNames refer to properties assigned directly to the object rather than only being accessible through the object's prototype chain. Either this.foo = or bar.foo = count as an own property, because you're assigning to the instance.

自有属性在第4.3.30节


直接包含的属性通过其对象

property that is directly contained by its object

与继承的属性相对应( 4.3.31 ):

vs an "inherited property," defined (4.3.31) as:


对象的属性,它不是自己的属性,而是对象原型的属性(自己的或继承的)

property of an object that is not an own property but is a property (either own or inherited) of the object’s prototype

也就是说,自己的属性在实例上,而不是原型。

That is, an "own" property is on the instance, not the prototype.

最大的影响是原型类(具有某些方法和/或静态属性的构造函数) 。在经典的OO术语中, getOwnPropertyNames 将跳过类方法以及任何具有 static 关键字的内容。

The biggest impact is with prototypical classes (constructors with some methods and/or static properties). In classical OO terms, getOwnPropertyNames will skip class methods and anything that would have a static keyword.

如果你看一下 8.12.1节的规范,它间接排除了原型。在步骤#3中,运行时检查对象自己的属性以获取相应的属性名称。但是,在第8.12.2节(指 getProperty 没有自己的限定符),步骤#3-4描述了如果在对象本身上找不到属性,则检查对象的原型。

If you take a look at section 8.12.1 of the spec, it indirectly excludes the prototype. In step #3, the runtime checks the object's own properties for the appropriate property name. However, in section 8.12.2 (referring to getProperty without the "own" qualifier), steps #3-4 describe checking the object's prototype if the property was not found on the object itself.

这篇关于什么是“OWN” javascript中的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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