为什么?函数的对象参数返回未定义的JS [重复]

问题描述 投票:0回答:1

在此查询中,我有一个小疑问,我需要知道为什么?

我们处于#lockdown的状态,而我的一个兄弟padu提出了这个问题。在下面的示例对象中,有一些函数在直接调用其返回1时分配,而在分配为const并以函数访问的方式返回undefined。有人有任何解释将有助于提高我的知识。

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**

谢谢,

Gopal.R

javascript function object const return-value
1个回答
1
投票

您正在搜索bind()

bind()
© www.soinside.com 2019 - 2024. All rights reserved.