我正在尝试使用两种不同的模式构建(或返回)javascript对象 工厂模式 2.构造函数模式
以下是使用工厂模式的代码
function employee(){
var emp = {
firstName : 'mihir',
lastName : 'hapaliya',
that : this
}
return emp;
}
console.log(employee().that);
以下是使用构造函数模式的代码
function employee(){
this.firstName = '';
this.lastName = '';
this.that = this;
}
console.log(new employee().that);
两种模式都返回员工对象。我有变量'that',我指定了这个变量的范围
此变量的范围是构造函数模式中的员工对象(预期行为)。
为什么这个变量的范围是工厂模式中的窗口,认为它是对象emp的属性?
因此,两种模式构建相同的javascript(员工)对象,但创建对象的方式是不同的 那么为什么这个变量的范围不同?