"use strict";
function Greet(name) {
this.name = name;
this.sayHello = function () {
console.log("Hello " + this.name);
};
}
var greet = new Greet("tom");
greet.sayHello();
输出:Hello tom
"use strict";
function Greet(name) {
this.name = name;
function sayHello() {
console.log("Hello " + this.name);
}
return {
name: "cat",
sayHi: sayHello
};
}
var greet = new Greet("tom");
greet.sayHi();
输出:Hello cat
"use strict";
function Greet(name) {
var that = this;
this.name = name;
function sayHello() {
console.log("Hello " + that.name);
}
return {
name: "cat",
sayHi: sayHello
};
}
var greet = new Greet("tom");
greet.sayHi();
输出:Hello tom