简体   繁体   English

Ember 对象的 Javascript 范围

[英]Javascript scoping of Ember objects

I am not sure of the difference between the following:我不确定以下内容之间的区别:

export default Ember.Component.extend({

    errors: {},

    performPermalinkUpdate: function(){

        let errors = this.get('errors');

        this.requestPermalink(this.get('title'), this.endpoint).then((resp) => {    
            // Do success stuff
        }).catch((resp) => {
            Ember.set(errors, 'permalink', "Test");
        });
    }
});

VS VS

export default Ember.Component.extend({

    errors: {},

    performPermalinkUpdate: function(){

        this.requestPermalink(this.get('title'), this.endpoint).then((resp) => {    
            // Do success stuff
        }).catch((resp) => {
            let errors = this.get('errors');
            Ember.set(errors, 'permalink', "Test");
        });
    }
});

The first one does not actually alter the errors property.第一个实际上并没有改变errors属性。 The error never shows up on the template.该错误永远不会出现在模板上。 Whereas the second will and the resulting error shows up on my template.而第二个将和由此产生的错误显示在我的模板上。

I assume I am missing a key scoping issue with the promise.我认为我错过了承诺的关键范围界定问题。

The only difference is the timing.唯一的区别是时间。 The first one is accessing the errors object earlier, before calling requestPermalink .第一个是在调用requestPermalink之前更早地访问errors对象。 The last one access it after.最后一个访问它。 The this context is the same because of the arrow function.由于箭头函数, this上下文是相同的。


I assume your requestPermalink function does something like this:我假设您的requestPermalink函数执行以下操作:

requestPermalink() {
  // 
  this.set('errors', { debug: "second error object" });
},

Then your order of execution for the first example is:那么您对第一个示例的执行顺序是:

  1. this.errors is {} because thats how you initialized it with errors: {}, . this.errors{}因为这就是你用errors: {},初始化它的方式errors: {},
  2. you execute let errors = this.get('errors');你执行let errors = this.get('errors'); . . So now the errors variable is the same object as this.errors .所以现在errors变量与this.errors是同一个对象。
  3. You call this.requestPermalink .你调用this.requestPermalink This will execute the this.set('errors', { debug: "second error object" });这将执行this.set('errors', { debug: "second error object" }); I've posted above.我已经在上面发过了。 Now this.errors is that new object with debug: "second error object" defined, while the errors variable is still pointing to the original object {} .现在this.errors是定义了debug: "second error object"新对象,而errors变量仍然指向原始对象{}
  4. Next you execute Ember.set(errors, 'permalink', "Test");接下来你执行Ember.set(errors, 'permalink', "Test"); . . This will change the errors variable from {} to {permalink: "Test"} .这会将errors变量从{}更改为{permalink: "Test"} However this.errors is still { debug: "second error object" } .然而this.errors仍然是{ debug: "second error object" }

The order of execution for the second example however:但是,第二个示例的执行顺序:

  1. this.errors is {} because thats how you initialized it with errors: {}, . this.errors{}因为这就是你用errors: {},初始化它的方式errors: {},
  2. You call this.requestPermalink .你调用this.requestPermalink This will execute the this.set('errors', { debug: "second error object" });这将执行this.set('errors', { debug: "second error object" }); I've posted above.我已经在上面发过了。 Now this.errors is that new object with debug: "second error object" defined.现在this.errors是带有debug: "second error object"新对象debug: "second error object"定义了debug: "second error object" However there is no errors variable yet, so probably the original object {} can be garbage collected.但是还没有errors变量,所以原始对象{}可能会被垃圾收集。
  3. You execute let errors = this.get('errors');你执行let errors = this.get('errors'); . . Now errors is the same as object as this.errors and looks like { debug: "second error object" } .现在errorsthis.errors对象相同,看起来像{ debug: "second error object" }
  4. You modify errors with Ember.set(errors, 'permalink', "Test");您可以使用Ember.set(errors, 'permalink', "Test");修改errors Ember.set(errors, 'permalink', "Test"); . . Because errors and this.errors are both references to the same object this will modify this.errors .因为errorsthis.errors都是对同一个对象的引用,所以这将修改this.errors

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM