我正在阅读 JavaScript,我想出了这个简单的代码片段var student = function(){ this._name = ""; this._funct = function(){ console.log(this._name); }}student._name="alax";console.log(student._name);student._funct(); //Fails - error: Uncaught TypeError: student._funct is not a function我的最后一句话失败了。谁能告诉我为什么它失败了?
2 回答
宝慕林4294392
TA贡献2021条经验 获得超8个赞
您尚未实例化 student 的实例。您可以将其更改为:
var Student = function(){
this._name = "";
this._funct = function(){
console.log(this._name);
}
}
var student = new Student()
student._name="alax";
console.log(student._name); // alax
student._funct(); // alax
阅读有关原型继承的更多信息
添加回答
举报
0/150
提交
取消