0

I am reading an application code that written dojo.

define(["dojo/ready", "dojo/_base/declare"], function(ready, declare) {
  return declare("application.main", null, {
    constructor: function(options) {
        var _self = this;
        this.options = {};
        declare.safeMixin(_self.options, options);

    }
    addReportInAppButton: function() {
        var _self = this;
        if (_self.options.appName) {
 }});

I confused about some points.

  1. in constructor function and addReportInAppButton used this keyword and assigned to _self property.
  2. Declare.safeMixin(_self.opt.ons, options)

options is an array like this { "appName":"xyz", "appId":"1141"}

I noticed that assigned this.options is using in addReportInAppButton function like this _self.oprions.appName,

How does this work? and is declare.safeMixin copiying object to this.options?

bayramucuncu
  • 1,014
  • 10
  • 20

1 Answers1

0
  1. In both functions, _self is equal to this and could be safely removed. Most likely this is just a convention, possibly from having to declare _self when wanting to access it in eg. a callback:

    var _self = this; 
    on(someButton, 'click', function() {
        this.foo() // this !== _self
        _self.foo() // used to access properties of the original context
    }); 
    
  2. Yes, safeMixin copies all of options' properties into _self.options. safeMixin also ensures that if you copy a function, you will be able to call this.inherited on it and it will work as expected.

You could use the mixin() function in dojo/_base/lang, but that would throw an error if you tried to call this.inherited on a function.

The docs on dojo/_base/declare have some more elaborate examples for safeMixin

Kryptic
  • 1,922
  • 2
  • 21
  • 29