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
Object properties are defined as a simple association between name and value. All properties have a name and value is one of the attributes linked with the property, which defines the access granted to the property. Properties refer to the collection of values which are associated with the JavaScript object.
Both also support ES6 symbols. So what's the difference between the two? The key difference is that in will return true for inherited properties, whereas hasOwnProperty() will return false for inherited properties.
The type property sets or returns the value of the type attribute of a script. The type attribute specifies the MIME type of a script. The type attribute identifies the content between the <script> and </script> tags.
Object properties differentiate objects from other objects. The basic properties of an object are those items identified by its four-part name (name, type, instance, and version) and also include owner, status, platform, and release.
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.
"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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With