生成动态构造函数样式的函数

时间:2014-09-18 11:58:01

标签: javascript dynamic constructor

我有什么

我在我的代码中静态定义了一些构造函数样式的函数,其他一切都运行正常。

随着事情变得越来越大,我想基于我的实体名称动态定义构造函数样式的函数。

以下两段代码显示了我发现的问题。我希望他们能够完全相同,但他们没有。

摘录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);

输出:

  

内部构造函数Country

     

在Create中,'this'是model.constructors.Country {},entity是Country

     

内部构造函数State

     

在Create中,'this'是model.constructors.State {},entity是State

     

model.constructors.Country {}

     

model.constructors.State {}

摘录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);

输出:

  

内部构造函数State

     

在Create中,'this'是model.constructors。(匿名函数){},entity是State

     

内部构造函数State

     

在Create中,'this'是model.constructors。(匿名函数){},entity是State

     

model.constructors。(匿名函数){}

     

model.constructors。(匿名函数){}

我无法理解为什么使用model.constructors.Country会产生与使用model.constructors['Country']不同的结果,或者我还能做些什么呢。

在两种情况下检查model.constructors都会产生相同的输出:

Object {Country: function, State: function}

我需要什么

我需要片段1的结果,但是使用像片段2这样的方法,因为我将迭代实体名称数组。

更确切地说,我需要像这样使用它:

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'});

但让代码生成与代码段1相同的结果。

所以:

  1. 我怎样才能让它发挥作用?
  2. 为什么代码段2与代码段1的工作方式不同?
  3. 在代码段2输出中,引用在日志“[...]和实体中打印两次的”状态“字符串是什么状态?

0 个答案:

没有答案