0

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

asked Dec 15, 2013 at 6:31

2 Answers 2

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

answered Dec 15, 2013 at 6:43
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks ! I forgot again that Object.getPrototypeOf was the one to use
0

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
answered Dec 15, 2013 at 9:30

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.