我是 Javascript 的初学者,很难理解构造函数和原型属性之间的关系。
我知道 Prototype 对象有一个constructor
指向构造函数的属性。并且构造函数有一个prototype
指向原型对象的属性。
这是我试图理解的代码(我的问题在代码中注释):
function Car(){};
var myCar = new Car();
console.log(Object.getPrototypeOf(myCar)); //why this prints "Car" Object ? isn't it the constructor not the prototype object ? why the prototype object is not printed ?
var Vehicle = {
getName : function(){
return "hello";
}
};
Car.prototype = Vehicle ; //I'm trying to change the prototype property in the constructor to "Vehicle" Object is that done right ?
console.log(Object.getPrototypeOf(myCar).getName()); //Why am i getting getName() function does not exist ?