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:
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