Dojo 1.9: What Annotation Does Declare.safemixin Add?
Solution 1:
Using safeMixin
allows you to mix functions into an instance that can take advantage of this.inherited
the same way that prototype methods defined using declare
can.
For example, the following will log 2 messages:
require([
"dojo/_base/lang",
"dojo/_base/declare"
], function(lang, declare){
var A = declare(null, {
method: function () {
console.log('method in prototype');
}
});
var a = newA();
declare.safeMixin(a, {
method: function () {
this.inherited(arguments);
console.log('method in instance');
}
});
a.method();
});
Without safeMixin
, you wouldn't be able to call this.inherited(arguments)
from the overriding method (at least, not without additional parameters) - you'd end up getting an error:
Error:declare: can't deduce a name to call inherited()
Solution 2:
safeMixin
adds the nom
property to functions that are mixed in to the target. This property is set to the key from the source object that the function was assigned to. e.g. if you call declare.safeMixin(target, { foo: function() {} })
, the nom
property for that function is "foo". This is necessary for this.inherited(arguments)
to automatically figure out that it should call the parent "foo". The alternative to using safeMixin
would be to explicitly specify the name of the parent function: this.inherited('foo', arguments);
.
Post a Comment for "Dojo 1.9: What Annotation Does Declare.safemixin Add?"