我试图在Javascript中创建一个自定义构造函数,但我似乎无法理解为什么控制台不记录"Investigating"的'Letters'属性,该属性是由构造函数“谓词”创建的:
function Verb (tense, transitivity) {
this.tense = tense;
this.transitivity = transitivity;
**this.letter1 = this.charAt(0);
this.letter2 = this.charAt(2);
this.letter3 = this.charAt(4);
this.Letters = this.letter1 + " " + this.letter2 + " " + this.letter3;**
}
var Investigating = new Verb ("present", "transitive");
console.log(Investigating.tense); // present
**console.log(Investigating.Letters); //console doesn't log anything**我在这里做错什么了?会很感激你们的帮助,谢谢。
发布于 2015-03-10 06:26:28
在构造函数中,this指的是正在创建的obj,.so this.charAt(0) is incorrect.since对象在其原型链中没有charAt方法。(字符串和数组有此方法).i认为您正在尝试这样做。
this.letter1 = this.transitivity.charAt(0);
this.letter2 = this.transitivity.charAt(2);
this.letter3 = this.transitivity.charAt(4);
this.Letters = this.letter1 + " " + this.letter2 + " " + this.letter3;`https://stackoverflow.com/questions/28957252
复制相似问题