【问题标题】:How is the a value of this in ember.js computed property defined? [duplicate]ember.js 计算属性中 this 的 a 值是如何定义的? [复制]
【发布时间】:2016-08-05 10:50:42
【问题描述】:

我试图理解为什么在计算属性中使用 function() 有效,但粗箭头却无效。

这是我创建的一个类,以及两个计算属性的定义,一个传入function() {} (foo),另一个传入() => {} bar。我将两种样式都包含在一个数组 forEach() 中以供参考(foo1 和 bar1)。

有人能解释一下这四种情况的每一种情况吗?

import Ember from 'ember';

const Light = Ember.Object.extend({
    isOn: false,
    color: 'red',

    foo: Ember.computed( 'isOn', 'color',  function() {
      console.log(`this in foo is ${this}`);
    }) , 
    foo1: [1].forEach(function() {
      console.log(`this in foo1 is ${this}`);
    }) ,
    bar: Ember.computed( 'isOn', 'color',  () => {
      console.log(`this in bar is ${this}`);
    }), 
    bar1: [1].forEach( () => {
      console.log(`this in bar1 is ${this}`);
    }) 

});

export default Ember.Controller.extend({
  appName: 'Ember Twiddle',

  init() {
    this._super();
    this.set('bulb', Light.create());
  }

});

这里是引用所有 4 个属性的模板

<h1>Welcome to {{appName}}</h1>
<br>
<br>
{{outlet}}
<br>
<br>
<p> the bulb is {{bulb.isOn}} </p>
<p> the bulb is {{bulb.foo}} </p>
<p> the bulb is {{bulb.foo1}} </p>
<p> the bulb is {{bulb.bar}} </p>
<p> the bulb is {{bulb.bar1}} </p>

这是打印到控制台的结果。

DEBUG: -------------------------------
VM66 ember.debug.js:6395 DEBUG: Ember      : 2.4.4
VM66 ember.debug.js:6395 DEBUG: Ember Data : 2.4.3
VM66 ember.debug.js:6395 DEBUG: jQuery     : 1.11.3
VM66 ember.debug.js:6395 DEBUG: -------------------------------
VM70 about:srcdoc:29 this in foo1 is undefined
VM70 about:srcdoc:35 this in bar1 is undefined
VM70 about:srcdoc:26 this in foo is <(unknown mixin):ember360>
VM70 about:srcdoc:32 this in bar is undefined

这里是 twiddle 的链接 https://ember-twiddle.com/d5905b42eff57e8ad5c99a27a3199429?openFiles=templates.application.hbs%2C

【问题讨论】:

    标签: javascript ember.js


    【解决方案1】:

    表单的功能

    foo1: [1].forEach(function() {
      console.log(`this in foo1 is ${this}`);
    

    不是计算函数,你可能知道;它们在解析时执行。 forEach 返回的值是undefined 因此,foo1 属性的值将是常量undefined。在解析定义时,控制台日志将只发生一次,甚至在 Light 对象被实例化之前。

    你的功能

    bar: Ember.computed( 'isOn', 'color',  () => {
      console.log(`this in bar is ${this}`);
    }), 
    

    不起作用,因为箭头函数有词法this(换句话说,周围的this)。 this 将是在解析定义时生效的this,它可能是undefinedwindow。 Ember 在相关对象的上下文(使用this)中调用作为计算属性的一部分指定的函数,因此显然让this 等于undefined 根本不起作用。

    【讨论】:

    • 我想我明白为什么其他人没有工作。我想我仍然有点挂断的是理解为什么“this”最终不会成为 Ember 的上下文,因为该函数被传递到 Ember.computed?
    • @nPn 您将function() {}.bind(this) 传递给Ember.computed,您传递的上下文是窗口或未定义(取决于严格模式),Light ember 对象尚未创建,请采取看看重复的问题。如果仍然不清楚,请在 ember 社区 slack 的 -help 或 -general 频道中找到我们。
    • 认为我现在明白了。关键信息是:“Ember 在相关对象的上下文(使用 this)中调用指定为计算属性的一部分的函数”。我确实查看了 Ember.computed 源代码,并且在那里看到了一个 apply(),所以我想这就是它的完成方式。这可能是一种常见的做法(框架有助于让“这个”适合你),但对我来说(天真)我无法理解 为什么 它有效。感谢您指向 slack 社区。​​span>
    猜你喜欢
    • 1970-01-01
    • 2019-03-18
    • 2020-12-10
    • 2013-06-29
    • 1970-01-01
    • 2012-11-03
    • 2012-07-27
    • 1970-01-01
    • 2017-12-18
    相关资源
    最近更新 更多