In this query, i've have small doubt i need to know WHY?
我们正处于#lockdown状态,而我的一个兄弟padu问了这个问题。下面我有示例对象,其中有函数,同时直接调用其返回1,但分配为const并作为函数访问返回未定义。有人有任何解释将真正有助于提高我的知识。
const apple = 3;
const data = {
apple: 2,
bag: {
apple: 1,
plastic: function(){
return this.apple;
}
}
}
const plastic = data.bag.plastic;
console.log(plastic()) **//Result - undefined**
console.log(data.bag.plastic()) **//Result - 1**
谢谢
戈帕尔.R
最佳答案
您正在搜索bind()
const apple = 3;
const data = {
apple: 2,
bag: {
apple: 1,
plastic: function(){
return this.apple;
}
}
}
const plastic = data.bag.plastic.bind(data.bag);
console.log(plastic())
console.log(data.bag.plastic())
关于javascript - 为什么 ?函数的对象参数返回未定义的 JS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61025173/