lang.mixin 和 declare.safeMixin 之间的差异?道场工具包

Difference(s) between lang.mixin and declare.safeMixin? Dojotoolkit

我已经看过documentation,尽管作者已经提到了不同之处,但我还是不明白。

我创建了两个小提琴来向您展示 declare.safeMixinlang.mixin 如何工作的不同情况。

从 Dojo 代码中提取的关于 declare.safeMixin:

的文档

This function is used to mix in properties like lang.mixin does, but it skips a constructor property and decorates functions like declare() does.
It is meant to be used with classes and objects produced with declare.
Functions mixed in with dojo.safeMixin can use this.inherited() like normal methods.
This function is used to implement extend() method of a constructor produced with declare().

从 Dojo 代码中提取的关于 lang.mixin:

的文档

All properties, including functions (sometimes termed "methods"), excluding any non-standard extensions found in Object.prototype, are copied/added from sources to dest. sources are processed left to right.
The Javascript assignment operator is used to copy/add each property; therefore, by default, mixin executes a so-called "shallow copy" and aggregate types are copied/added by reference.

扩充道场 class 创建 declare:

Working example can be found here. You need to open the console to view the logs.

首先我们声明一个 dojo class Person 然后创建这个 class:

的两个实例
var Person = declare(null, {
  name: null,
  message: "foo",
  constructor: function(args) {
    this.name = args.name;
  },
  speak: function() {
    console.log(`   ${this.name} says: '${this.message}'!`);
  }
});


var rob = new Person({ name: "Rob" });
var peter = new Person({ name: "Peter" });

如果我们调用 speak 方法,rob 和 peter 都会说 foo

现在我们修改一个实例 (rob) 为 declare.safeMixin ...

  declare.safeMixin(rob, {
    secondMessage: "bar",
    speak: function() {
        this.inherited(arguments);
        console.log(`   ${this.name} says: '${this.secondMessage}'!`);
    }
  });

...和另一个 (peter) lang.mixin.

  lang.mixin(peter, {
    secondMessage: "bar",
    speak: function() {
        // this.inherited(arguments); // would cause an error
        console.log(`   ${this.name} says: '${this.secondMessage}'!`);
    }
  });

之后Rob会说两个字,因为我们用了declare.safeMixin,说明我们可以在overriden中使用this.inherited(arguments) speak方法。

Peter 不会说 Rob,而是只会说一个词,因为我们使用了 lang.mixin,它完全 overrides 我们之前的 speak 方法。

注意:您可以以 lang.mixin 也允许使用 inherited 的方式编写代码(您需要明确命名该方法你想打电话给 this.inherited):

lang.mixin(peter, {
  secondMessage: "bar",
  speak: function() {
      this.inherited('speak', arguments);
      console.log(`   ${this.name} says: '${this.secondMessage}'!`);
  }
});

See here for a working example.

扩充一个简单的 java 脚本对象:

Working example can be found here. You need to open the console to view the logs.

首先我们声明两个对象,它们具有相同的属性 foo.

  var object= {
    foo: "bar"
  };

  var anotherObject = {
    foo: "bar"
  };

然后我们再次修改两者。 object 实例 declare.safeMixin ...

  declare.safeMixin(object, {
    baz: 123
  }, {
    baz: 1234
  });

...和 ​​anotherObject 实例 lang.mixin.

  lang.mixin(anotherObject, {
    baz: 123
  }, {
    baz: 1234
  });

注意,我们混合了两个实例,两个对象!现在,如果我们尝试访问两个实例的 baz 属性,我们将得到不同的结果。

object实例baz属性会return123,因为declare.safeMixin只支持mixin一个java脚本每个方法调用的对象。

anotherObject 个实例 baz 属性 将 return 1234,因为 lang.mixin 支持 mixin 多个 java 脚本对象每个方法调用。这里计算:最后一个获胜。

希望这能让事情变得更清楚。