var test = function () { num = 0 ;};
test.prototype.show = function () { alert(this.num) ; }
test.prototype.add = function () { this.num += 1 ; alert(this.num);}
var testit = new test() ;
testit.add(); //NaN
testit.show(); //NaN
为什么 testit.add(); 和 testit.show(); 显示结果不是 1 呢? 而是 NaN? 我哪里错了? 为什么
var test = function () { this.num = 0 ;}; 就可以呢?
//asker:www.yuanshi88.com
test.prototype.show = function () { alert(this.num) ; }
test.prototype.add = function () { this.num += 1 ; alert(this.num);}
var testit = new test() ;
testit.add(); //NaN
testit.show(); //NaN
为什么 testit.add(); 和 testit.show(); 显示结果不是 1 呢? 而是 NaN? 我哪里错了? 为什么
var test = function () { this.num = 0 ;}; 就可以呢?
//asker:www.yuanshi88.com
this.num使得num和函数变量test绑定
var test = function () {this.num = 0 ;};
test.prototype.show = function () { alert(this.num) ; }
test.prototype.add = function () { this.num += 1 ; alert(this.num);}
var testit = new test();
testit.add();
testit.show();
如果你想写在函数外面,可以这样
var testit = new test();
testit.num = 0;
...