admin管理员组文章数量:1313364
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" property is accessible in for...in loop
"fullName" is not visible in getOwnPropertyNames() method.That means it is
not a own property
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
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" property is accessible in for...in loop
"fullName" is not visible in getOwnPropertyNames() method.That means it is
not a own property
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
Share
Improve this question
asked Apr 6, 2016 at 15:59
AL-zamiAL-zami
9,07617 gold badges77 silver badges136 bronze badges
5
- 2 "what type of property it is" Some would say it's an "inherited" property. – Felix Kling Commented Apr 6, 2016 at 16:03
- 1 MDN: Enumerability and ownership of properties – Bergi Commented Apr 6, 2016 at 16:07
- @FelixKling: Who would not? – Bergi Commented Apr 6, 2016 at 16:12
- @Bergi: Dunno. Wasn't sure how official the term is, but it seems to be official. – Felix Kling Commented Apr 6, 2016 at 16:13
- FWIW, its the same difference between local and free variables, if that helps... – Felix Kling Commented Apr 6, 2016 at 16:15
2 Answers
Reset to default 9hasOwnProperty
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.
"Own properties" are defined in section 4.3.30 of the spec to be:
property that is directly contained by its object
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.
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.
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.
Object.getOwnPropertyNames returns the property names that are directly on the object. For instance if you add a new property to the object using obj.foo = 'bar';
then "foo"
will be included in the object's own properties. fullName
isn't a property directly on obj
, but it is a property of this object:
{
get fullName() {
return this.first + " " + this.last;
},
set fullName(name) {
var names = name.split(" ");
this.first = names[0];
this.last = names[1];
}
}
which happens to be the internal prototype of obj
:
Object.getOwnPropertyNames( Object.getPrototypeOf( obj ) ); // ["fullName"]
The for ...in loop iterates through all enumerable properties in the objects prototype chain, so it will include those properties.
本文标签: oopwhat is an quotOWNquot property in javascriptStack Overflow
版权声明:本文标题:oop - what is an "OWN" property in javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741934336a2405761.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论