// what is this "Object.prototype" anyway...? var prototypeType = typeof Object.prototype; console.log(prototypeType); // now let's examine it! var hasOwn = Object.prototype.hasOwnProperty("hasOwnProperty"); console.log(hasOwn);
Kindly explain the concept of 'Object.prototype' and 'hasOwnProperty' .I did not understand the code below?
A prototype is an early sample, model, or release of a product
built to test a concept or process or
to act as a thing to be replicated or learned from.
The word prototype derives from the Greek πρωτότυπον prototypon,
"primitive form", neutral of πρωτότυπος prototypos,
"original, primitive", from πρῶτος protos, "first" and τύπος typos, "impression".
Inheritance and the Prototype Chain
Prototypal Inheritance is Dynamic
One of the most important advantages of prototypal inheritance is
that you can add new properties to prototypes after they are created.
This allows you to add new methods to a prototype
which will be automatically made available
to all the objects which delegate to that prototype.
This is not possible in classical inheritance
because once a class is created you can't modify it at runtime.
This is probably the single
biggest advantage of
over classical inheritance,
and it should have been at the top.
Prototypal inheritance matters.
on why to abandon the constructor pattern of prototypal inheritance
in favor of the prototypal pattern of prototypal inheritance.
and that means showing new programmers how to write code
using the prototypal pattern instead of the constructor pattern.
Not only will it be it easier to explain
prototypal inheritance using the prototypal pattern,
but it will also make better programmers
++++++++ a String type explained ++++++
why it should be Object.prototype.hasOwnProperty....
The hasOwnProperty Method is the only method
which will be fetched from the Object.prototype WITHOUT traversing
through the prototype Chain.
In the following link:
Under the Header:
The lookup time for properties that are high up on the prototype chain can have a negative impact on performance, and this may be significant in code where performance is critical. Additionally, trying to access nonexistent properties will always traverse the full prototype chain.
Also, when iterating over the properties of an object, every property that is on the prototype chain will be enumerated.
To check whether an object has a property defined on itself and not somewhere on its prototype chain, it is necessary to use the hasOwnProperty method which all objects inherit from Object.prototype.
now to your case:
var prototypeType = typeof Object.prototype;
will leave you with a variable which has a VALUE of the string-type
As the string is created, you will get a present...the length Property
prototypeType.hasOwnProperty("length") ==> true.
From the String.prototype you will inherit the methods:
from the Object.prototype you inherit:
'hasOwnProperty' \<== this is a special one !!
wtf...you're way to smart homeboy.
very good explanation, almost too good.
I'm still a little fuzzy on what we're supposed to get from this lesson on "what is this Object.prototype", but maybe that will come in time.
Thank you leonhard
@leonhard.wettengmx.n, while I appreciate your explanation, I think I am more confused now. What confuses me the most is that nothing in this lesson prints to the console. Maybe my thought process is wrong, but...
var prototypeType = typeof Object.prototype; console.log(prototypeType);
This would print "object", right? Or so I thought, but you mentioned it is a string?? I thought Object.prototype was an 'object', and so prototypeType would hold the value 'object'... How is it a 'string', instead?
var hasOwn = Object.prototype.hasOwnProperty("hasOwnProperty"); console.log(hasOwn);
And Object.prototype.hasOwnProperty('hasOwnProperty'); would return a boolean based on whether Object.prototype has the property hasOwnProperty, which would make hasOwn = true.
Is the above correct? Where is the inheritance? Like @richr759, Im not sure what 'lesson' in this lesson is?
You could have used
var prototypeType = typeof Object.prototype; console.log(prototypeType); console.log(typeof prototypeType);
and have seen that the variable prototypeType has an associated string Value