<分区>
Possible Duplicate:
Javascript - this Vs. prototype
Advantages of using prototype, vs defining methods straight in the constructor?
Prototype or inline, what is the difference?
在 JavaScript 中创建类时,使用 this 在对象内部声明方法与通过访问 prototype 声明方法有何不同?在我看来,目的相同。
window.onload = function() {
tc = new TestClass();
tc.init(); //logs initting
tc.reset(); //logs resetting
tc.doSomething(); //logs doing something
};
var TestClass = function(){
this.init = function(){
console.log("initting");
}
this.reset = function(){
console.log("resetting");
}
this.destroy = function(){
console.log("destroying");
}
}
TestClass.prototype.doSomething = function(){
console.log("doing something");
}