简体   繁体   English

方法销毁无效

[英]Method destructuring not working

Trying to determine why this wont function properly. 试图确定为什么此功能无法正常运行。

// assuming this works
const { emitter } = window.project;
emitter.emit('foo');

// why doesnt this
const { emitter: { emit } } = window.project;
emit('foo');

Presumably because the emit method depends on this referring to a specific value. 大概是因为emit方法依赖this引用一个特定值。 The value of this depends on how a function is called (if it's an unbound, non-arrow function), and you are calling the function in two different ways: this取决于函数是如何被调用(如果它是一个未绑定的,非箭头功能),你在呼唤以两种不同方式的功能:

  • emitter.emit() will cause this inside emit to refer to emitter emitter.emit()会导致this内部emit引用emitter
  • emit() will cause this to refer to undefined (strict mode) or the global object (non-strict mode) emit()将导致this引用undefined (严格模式)或全局对象(非严格模式)

That has nothing to do with ES6 specifically. 这与ES6无关。

More information about this : You Don't Know JS: this & Object Prototypes . 关于this更多信息: 您不知道JS:this和Object Prototypes

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

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