D.B*_*een 1 javascript oop constructor
简单的问题.我为什么要这样做......
var Person = function(name) {
this.name = name;
this.sayHello = function(){
console.log('Hello, my name is ' + this.name); // <--
}
};
Run Code Online (Sandbox Code Playgroud)
代替...
var Person = function(name) {
this.name = name;
this.sayHello = function(){
console.log('Hello, my name is ' + name); // <--
}
};
Run Code Online (Sandbox Code Playgroud)
我没有发现他们表现不同的情况.
因此,如果有任何函数重置名称,如果你不使用,事情就不会被重新选择this.name.检查此示例
var Person = function(name) {
this.name = name;
this.sayHello = function() {
console.log('Hello, my name is ' + name);
console.log('Hello, my name is ' + this.name);// <--
}
};
Person.prototype.resetName = function(name) {
this.name = name;
}
var me = new Person('A');
me.sayHello();
console.log('******');
me.resetName('Ayan');
me.sayHello();Run Code Online (Sandbox Code Playgroud)