我试图访问该函数作为person对象的方法,为什么它不会访问该方法并执行该函数应该做的事情?
function Person(name, age, location) {
this.name= name;
this.age = age;
this.location = location;
this.test = function() {
alert("TEST");
};
}
var Matt = new Person("Matthew", "21", "South Africa");
Matt.test;
答案 0 :(得分:8)
您需要使用括号调用方法:
Matt.test();
这将执行该功能。否则你用Matt.test
获得的是函数本身,可以传递给另一个函数,存储在变量中等等。然后你可以在以后执行这个函数。
答案 1 :(得分:2)
好吧,因为它的功能使用:
Matt.test();