有什么区别
var A = function () {
this.x = function () {
//do something
};
};
Run Code Online (Sandbox Code Playgroud)
和
var A = function () { };
A.prototype.x = function () {
//do something
};
Run Code Online (Sandbox Code Playgroud) 有人可以帮我澄清在javascript中创建构造函数的两种方法之间的区别吗?这是代码:
var globarcheck= this;
var Car = function(color){
this.color= color;
/*this.prototype.Go = function()
{
console.log(this.color+" "+"Car going...");
};*/
};
Car.prototype.Go= function(){
console.log(this.color+" "+"Car going...");
};
var aCar = new Car("green");
var bCar = new Car("yellow");
aCar.Go();
bCar.Go();
Run Code Online (Sandbox Code Playgroud)
上面的代码有效,但为什么我不能使用注释掉的代码来分配Go方法呢?