var bus = function() {
this.maxSpeed = 100;
this.minSpeed = 10;
this.constructor = function (a, b) {
this.maxSpeed = a;
this.minSpeed = b;
}
this.show = function () {
alert('maxSpeed:' + this.maxSpeed + ' this.minSpeed:' + this.minSpeed);
}
};
var t = new bus(1000,9);
t.show();
document.writeln(t.constructor);
想重写这个方法的构造函数,结果好像没有效果。断点也没有进入constructor 方法内部。
希望提问者可以多多看书,因为这是很基本的问题。
JS的类是很奇怪的类,差不多也是模拟的类,它并不完全,我们也不能完全用类的思想去理解它。
该代码的执行过程是这样的:
1.系统new一个object类出来。
2.把object类传进bus函数,并绑定为它的上下文this,于是我们可以在bus函数中用this来访问它。
3.把修改后的object类赋值给变量t。
所以我们可以把bus函数看作是构造函数,因为它是在构造类时执行的。
但它执行的是bus函数,并没有执行里面的construct函数。自然跟踪不到断点执行了。