I am trying to build(or return) javascript object using two different patterns 1. factory pattern 2. constructor pattern
Here is the code using factory pattern
function employee(){
var emp = {
firstName : 'mihir',
lastName : 'hapaliya',
that : this
}
return emp;
}
console.log(employee().that);
Here is the code using constructor pattern
function employee(){
this.firstName = '';
this.lastName = '';
this.that = this;
}
console.log(new employee().that);
Both pattern return employee object. I have variable 'that' to which i am assigning the scope of this variable
scope of this variable is employee object in constructor pattern(expected behavior).
Why scope of this variable is window in factory pattern thought it is property of emp of object?
So both pattern building the same javascript(employee) object but the way of creating object is different then why the scope of this variable is different?