在JavaScript中解构对象时如何绑定方法? [英] How to bind methods when destructuring an object in JavaScript?
本文介绍了在JavaScript中解构对象时如何绑定方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在JavaScript中解构对象时如何绑定方法?
How to bind methods when destructuring an object in JavaScript?
const person = {
getName: function() {
console.log(this);
}
};
var a = person.getName;
var b = person.getName.bind(person);
var {getName: c} = person;
person.getName(); //=> {getName: [Function]}
a(); //=> window or global
b(); //=> {getName: [Function]}
c(); //=> window or global
我希望c
在控制台中登录其父"对象{getName:
[Function]}
.
I want c
to log in the console its "parent" object {getName:
[Function]}
.
在 destructuring 行中解构对象时,是否有任何方法可以绑定所有方法?
Is there any way to bind all methods when destructuring an object in one destructuring line?
推荐答案
不,没有办法.与对象分离的功能将失去原始上下文.而且JavaScript中的销毁没有语法可以即时处理提取的值.
No, there is no way. Functions detached from objects lose the original context. And destructing in JavaScript has no syntax to do something with extracted values on the fly.
这篇关于在JavaScript中解构对象时如何绑定方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文