What I have
I was defining some constructor-style functions statically in my code, and everything else was working fine.
As things got bigger, I wanted to define the constructor-style functions dynamically, based in my entities names.
The following two snippets of code show the problem that I've found. I would expect they to work exactly the same, but they don't.
Snippet 1:
var model = {};
// base constructor
model.Create = function(data, entity) {
console.log("Inside Create, 'this' is", this, 'and entity is', entity);
// initialization logic ...
}
// a constructor for each entity
model.constructors = {};
model.constructors.Country = function(data) {
var entity = 'Country';
console.log('Inside constructor', entity);
model.Create.call(this, data, entity);
}
model.constructors.State = function(data) {
var entity = 'State';
console.log('Inside constructor', entity);
model.Create.call(this, data, entity);
}
var br = new model.constructors.Country({name:'Brazil'});
var rs = new model.constructors.State({name:'Rio Grande do Sul'});
console.log(br);
console.log(rs);
The output:
Inside constructor Country
Inside Create, 'this' is model.constructors.Country {} and entity is Country
Inside constructor State
Inside Create, 'this' is model.constructors.State {} and entity is State
model.constructors.Country {}
model.constructors.State {}
Snippet 2:
var model = {};
// base constructor
model.Create = function(data, entity) {
console.log("Inside Create, 'this' is", this, 'and entity is', entity);
// initialization logic ...
}
// a constructor for each entity
model.constructors = {};
var entity;
entity = 'Country';
model.constructors[entity] = function(data) {
console.log('Inside constructor', entity);
model.Create.call(this, data, entity);
}
entity = 'State';
model.constructors[entity] = function(data) {
console.log('Inside constructor', entity);
model.Create.call(this, data, entity);
}
var br = new model.constructors.Country({name:'Brazil'});
var rs = new model.constructors.State({name:'Rio Grande do Sul'});
console.log(br);
console.log(rs);
The output:
Inside constructor State
Inside Create, 'this' is model.constructors.(anonymous function) {} and entity is State
Inside constructor State
Inside Create, 'this' is model.constructors.(anonymous function) {} and entity is State
model.constructors.(anonymous function) {}
model.constructors.(anonymous function) {}
I can't understand why using model.constructors.Country
is producing a different result than using model.constructors['Country']
, or what else can I be doing wrong.
Examining model.constructors in both cases produced the same output:
Object {Country: function, State: function}
What I need
I need the result of snippet 1, but using an approach like snippet 2, since I would be iterating over an array of entity names.
More exactly, I would need to work with it like this:
var model = {};
model.entities = ['Country', 'State'];
// base constructor
model.Create = function(data, entity) {
console.log("Inside Create, 'this' is", this, 'and entity is', entity);
// initialization logic ...
}
// a constructor for each entity
model.constructors = {};
for (var i in model.entities) {
var entity = model.entities[i];
model.constructors[entity] = function(data) {
console.log('Inside constructor', entity);
model.Create.call(this, data, entity);
};
}
var br = new model.constructors.Country({name:'Brazil'});
var rs = new model.constructors.State({name:'Rio Grande do Sul'});
but having the code producing the same result as snippet 1.
So:
- How can I get this to work?
- Why does snippet 2 works differently than snippet 1?
- In snippet 2 output, what was referencing that "State" string that was printed twice in the log "[...] and entity is State"?