Javascript 为什么不';原型函数不能读取原型属性吗?

Javascript 为什么不';原型函数不能读取原型属性吗?,javascript,function,prototype,Javascript,Function,Prototype,我有一段代码,返回一个未捕获的引用错误:未定义年份 function Car(color, drivetrain) { this.color = color; this.drivetrain = drivetrain; this.stats = function(){ console.log(color + " " + drivetrain); }; } Car.prototype.year = 2012; Car.prototype.funcyear = functi

我有一段代码,返回一个未捕获的引用错误:未定义年份

function Car(color, drivetrain) {
    this.color = color;
  this.drivetrain = drivetrain;
  this.stats = function(){
  console.log(color + " " + drivetrain);
  };
}

Car.prototype.year = 2012;
Car.prototype.funcyear = function () { console.log(year);}; //reference error

var toyota = new Car('red', 'fwd');
toyota.stats();
console.log(toyota.year);
toyota.funcyear();

为什么这不起作用呢?
funcyear
函数不应该位于原型中,所以year应该可以访问?

因为只有
year
会引用变量(或函数),而不是属性。
若要引用必须命名对象的特性,此特性将附着到

Car.prototype.funcyear = function() {
    console.log(this.year);
};
注意:

function Car(color, drivetrain) {
    this.color = color;
    this.drivetrain = drivetrain;
    this.stats = function(){
        //this part is "dangerous", because color and drivetrain reference the local variables 
        //in this surrouunding function-call, NOT the properties of this instance
        //so you might change the properties, but you don't see any effect.
        console.log(color + " " + drivetrain);
    };
}

因为只有
year
会引用变量(或函数),而不是属性。
若要引用必须命名对象的特性,此特性将附着到

Car.prototype.funcyear = function() {
    console.log(this.year);
};
注意:

function Car(color, drivetrain) {
    this.color = color;
    this.drivetrain = drivetrain;
    this.stats = function(){
        //this part is "dangerous", because color and drivetrain reference the local variables 
        //in this surrouunding function-call, NOT the properties of this instance
        //so you might change the properties, but you don't see any effect.
        console.log(color + " " + drivetrain);
    };
}

year
属性应该通过context在方法中访问为什么需要
属性?为什么需要此属性?遵循链接属性不会自动成为方法中的局部变量。您仍然必须从当前对象访问它们–
this.year
this此指被调用的方法的所有者…
year
是所有者
汽车的属性
因此应该使用
this
上下文进行访问。
year
属性应该通过上下文为什么
这个
是必需的?为什么这个是必需的?遵循链接属性不会自动成为方法中的局部变量。您仍然必须从当前对象访问它们—
this.year
this
引用被调用方法的所有者…
year
是所有者
汽车的属性
,因此应该使用
this
上下文进行访问。。