<script>
function Person(gender) {
this.gender = gender;
}
Person.prototype.sayGender = function()
{
alert(this.gender);
};
var person1 = new Person('Male');
var genderTeller = person1.sayGender;
genderTeller();
</script>
问题:
显示'undefined'。脚本有什么问题?
答案 0 :(得分:2)
您需要在person1
genderTell.call(person1);
答案 1 :(得分:0)
window
。你需要在这个人身上打电话。当您检索函数时,您只获得函数,而不是范围。
这也适用:
function Person(gender) {
this.gender = gender;
}
Person.prototype.sayGender = function () {
alert(this.gender);
};
var person1 = new Person('Male');
person1.sayGender(); // <-- calling the function ON "person1" directly