Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/408.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript dojo.mixin未按预期工作_Javascript_Dojo - Fatal编程技术网

Javascript dojo.mixin未按预期工作

Javascript dojo.mixin未按预期工作,javascript,dojo,Javascript,Dojo,我想我使用的是dojo.mixin,不是按要求使用的 我有以下代码: dojo.declare("A",null,{ _configuration: {}, constructor: function(configuration) { if(this._configuration.something === 2) { // we should never come here; the constructor is o

我想我使用的是dojo.mixin,不是按要求使用的

我有以下代码:

dojo.declare("A",null,{
    _configuration: {},
    constructor: function(configuration)
    {
        if(this._configuration.something === 2)
        {
            // we should never come here; the constructor is only called on a new instance which should still have an empty _somethingPrivate
            // because the class declaration says so
            console.log("why does this happen?");
        }

        // merge empty configuration 
        dojo.mixin(this._configuration, configuration);
    }
});

var myInstance = new A({ something: 2 });
var myInstance = new A({});
据我所知,您可以使用dojo.mixin来合并对象。我尝试将一个默认配置对象与给定的参数(在对象中)合并,但控制台输出是“为什么会发生这种情况?”因此来自前一个对象的参数将合并到一个新对象中

有人能解释一下吗


顺便说一下:dojo 1.6版(我们还不能升级)

因为您将配置定义为
\u configuration:{},
它在小部件的所有实例中共享。因此,当您初始化第二个实例时,它会看到第一个实例的配置。有关更多详细信息,请参阅

_defaultConfig: {},

_configuration: null,

constructor: function(config) {
    // clone to use a separate object.
    this._configuration = dojo.mixin(dojo.clone(this._defaultConfig), config);
}

因为您将配置定义为
\u configuration:{},
它在小部件的所有实例中共享。因此,当您初始化第二个实例时,它会看到第一个实例的配置。有关更多详细信息,请参阅

_defaultConfig: {},

_configuration: null,

constructor: function(config) {
    // clone to use a separate object.
    this._configuration = dojo.mixin(dojo.clone(this._defaultConfig), config);
}

谢谢你的回复!正如您所说,它听起来像_configuration是一个静态成员,但当我覆盖它(构造函数中的this._configuration=“somethingElse”)时,它只会覆盖当前实例(“this”)。这也是dojo.mixin所做的,这让我怀疑这是否是dojo开发团队故意做的……因为{}是一个对象,javascript将通过引用传递它,而“somethingElse”将通过值传递@Craig的解决方案是我能找到的传递配置的最佳方式。谢谢你的回复!正如您所说,它听起来像_configuration是一个静态成员,但当我覆盖它(构造函数中的this._configuration=“somethingElse”)时,它只会覆盖当前实例(“this”)。这也是dojo.mixin所做的,这让我怀疑这是否是dojo开发团队故意做的……因为{}是一个对象,javascript将通过引用传递它,而“somethingElse”将通过值传递@Craig的解决方案是我能找到的传递配置的最佳方式。