I think the misunderstanding arises when you read that someone write "every object has a prototype property".
Functions In Javascript are objects. Like every objects, functions have some properties and methods and some of them are defined by default: the length property, the prototype property, the call() apply() and bind() methods and so on. So, a function has a prototype property, by default
function myfunc(){
return "yes I'm a function";
}
//I said that function are objects having properties and methods
//this means you can call them, for example
myfunc.prototype
and you will see that the value you have in return from calling this property is an object. Ok, stop here about functions for now.
Now, you can use any function as a constructor, so you can create a new object calling the function together with the new
keyword
var myobj = new myfunc()
and you will have a new object. This object has not a prototype property by default
myobj.proptotype //=> unefined
we can't speak of a prototype property for an object: functions have a prototype property, objects don't.
But, each object it's associated with a prototype object from which properties and methods are inherited. Do you want to see what's the value of the prototype object? One of the solutions is
myobj.__proto__
and you will see the value of the prototype object of your custom object.
Now, take a look at the value of myobj.__proto__
and the value of myfunc.prototype
: they are the same. This simply means that the prototype property of the constructor (in other words, the prototype property of the function you use with the new
keyword in order to create a new object) is used as the prototype of the new object.
Keep that in mind.
Functions are objects, so functions have properties and methods by default.
Functions have a prototype property.
You use functions as constructors, to create new object.
Objects have not a prototype property.
Objects have a prototype object.
The prototype object value it's the same as the prototype property of the constructor.