0
で未定義の関数の奇妙なエラー私は両方とも「getYearメソッド」と呼ばれる関数定義し、ベースと派生クラスを定義した:最後の文はJavascriptを
Cannot read property 'getYear' of undefined
を言って、実行時エラーがトリガされます
function Base() {}
Base.prototype.getYear = function() {
return 2015;
}
function Derived() {
year = 2016;
}
Derived.prototype = new Base();
Derived.prototype.getYear = function() {
return 2017;
}
var ins = new Derived();
console.log(ins.getYear());
console.log(ins.prototype.getYear());
を
理由を説明するのを助けてくれますか? 私はこの関数を基底/派生関数の両方で定義したと思います。