[英]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:那么您对第一个示例的执行顺序是:
this.errors
is {}
because thats how you initialized it with errors: {},
. this.errors
是{}
因为这就是你用errors: {},
初始化它的方式errors: {},
。let errors = this.get('errors');
let errors = this.get('errors');
. errors
variable is the same object as this.errors
.errors
变量与this.errors
是同一个对象。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.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
变量仍然指向原始对象{}
。Ember.set(errors, 'permalink', "Test");
Ember.set(errors, 'permalink', "Test");
. 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:但是,第二个示例的执行顺序:
this.errors
is {}
because thats how you initialized it with errors: {},
. this.errors
是{}
因为这就是你用errors: {},
初始化它的方式errors: {},
。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.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
变量,所以原始对象{}
可能会被垃圾收集。let errors = this.get('errors');
let errors = this.get('errors');
. errors
is the same as object as this.errors
and looks like { debug: "second error object" }
.errors
与this.errors
对象相同,看起来像{ debug: "second error object" }
。errors
with Ember.set(errors, 'permalink', "Test");
Ember.set(errors, 'permalink', "Test");
修改errors
Ember.set(errors, 'permalink', "Test");
. errors
and this.errors
are both references to the same object this will modify this.errors
.errors
和this.errors
都是对同一个对象的引用,所以这将修改this.errors
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.