It seems to me that there are four different ways I can determine whether a given object (e.g. foo
) has a given property (e.g. bar
) defined:
if (foo.hasOwnProperty(bar)) {
if ('bar' in foo) {
if (typeof foo.bar !== 'undefined') {
if (foo.bar === undefined) {
To determine if there is a property named "bar
" in the object foo
, are all three of those statements equivalent? Are there any sublte semantics I don't know that makes any of these three statements different?
Member operators allow access to the properties and methods of an object. Under the hood properties and methods are the same thing. A method is a property with a function for it's value. We can use dot or bracket notation to access the members of an object.
A JavaScript property is a characteristic of an object, often describing attributes associated with a data structure. There are two kinds of properties: Instance properties hold data that are specific to a given object instance. Static properties hold data that are shared among all object instances.
What is this? In JavaScript, the this keyword refers to an object. Which object depends on how this is being invoked (used or called). The this keyword refers to different objects depending on how it is used: In an object method, this refers to the object.
A variable that has not been assigned a value is of type undefined . A method or statement also returns undefined if the variable that is being evaluated does not have an assigned value. A function returns undefined if a value was not returned .
No they are totally different. Example:
foo = {bar: undefined};
Object.prototype.baz = undefined;
Object.prototype.bing = "hello";
Then:
(typeof foo.bar != "undefined") === false
('bar' in foo) === true
(foo.hasOwnProperty('bar')) === true
(typeof foo.baz != "undefined") === false
('baz' in foo) === true
(foo.hasOwnProperty('baz')) === false
(typeof foo.bing != "undefined") === true
('bing' in foo) === true
(foo.hasOwnProperty('bing')) === false
Logic-wise:
foo.hasOwnProperty('bar')
implies 'bar' in foo
typeof foo.bar != "undefined"
implies 'bar' in foo
These are all different:
foo.hasOwnProperty('bar')
tells you whether foo
has the property and does not perform lookup along the prototype chain.
'bar' in foo
checks the prototype chain and returns true when it finds property bar
in any object along the chain.
typeof foo.bar != 'undefined'
returns true if foo
or any object along its prototype chain has property bar
and it's value is not undefined
.
Here is an example that demonstrates these differences:
var foo1 = { 'bar1': 10, 'bar2': undefined };
function ctor() {}
ctor.prototype = foo1;
var foo2 = new ctor();
foo2.bar3 = 20;
console.log(foo2.hasOwnProperty('bar1')); // false
console.log(foo2.hasOwnProperty('bar2')); // false
console.log(foo2.hasOwnProperty('bar3')); // true
console.log(foo2.hasOwnProperty('bar4')); // false
console.log('bar1' in foo2); // true
console.log('bar2' in foo2); // true
console.log('bar3' in foo2); // true
console.log('bar4' in foo2); // false
console.log(typeof foo2.bar1 != 'undefined'); // true
console.log(typeof foo2.bar2 != 'undefined'); // false
console.log(typeof foo2.bar3 != 'undefined'); // true
console.log(typeof foo2.bar4 != 'undefined'); // false
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