I tried to figure out how the Object constructor can have methods like call and apply since its prototype does not have them as properties, AND Object.hasOwnProperty('call') returns false in my current browser ( chromium ).
Please explain me where is this magic from
I edit my question since some people don't seem to understand what I mean :
A function inherits call from its prototype
var a = function(){}
a.hasOwnProperty('call') // false
a.prototype.hasOwnProperty('call') // true
Object has the call method. However, Object.prototype does not have the call method :
Object.hasOwnProperty('call') // false
Object.prototype.hasOwnProperty('call') // false
but 'call' in Object outputs true
2 Answers 2
Object is a function - it's a constructor that can be used to create objects:
var foo = new Object();
Of course, usually you would just use an object literal:
var foo = {};
Note that:
Object.getPrototypeOf(Object) === Function.prototype // true
but:
Object.getPrototypeOf(Object) !== Object.prototype // true
It's not two prototypes - Object is a function, which creates objects that have the prototype Object.prototype. Object itself has the prototype Function.prototype
try this:
function Fn() {};
Fn.prototype.pro = "this_pro";
var object = new Fn();
object.prototype = {};
object.hasOwnProperty("pro"); # false
object.prototype.hasOwnProperty("pro"); # false
"pro" in object; # true
and this:
object.__proto__.hasOwnProperty("pro") #true
and usually, an object should not have the properties of its prototype as it self's propeties. these properties is for the object which is a instance of it(as a function). To make is more clear:
object.__proto__ === Fn.prototype; # true