为什么 ?函数的对象参数 returns undefined JS
Why ? Object params of function returns undefined JS
In this query, i've have small doubt i need to know WHY?
我们正处于#lockdown 意味着我的一个兄弟 padu 问了这个问题。下面我有示例对象,因为在直接调用其 return 1 时有函数,但在分配为 const 和访问时作为函数 returns 未定义。哪位大神解释一下,对我的知识增长很有帮助。
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
您正在搜索 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())
In this query, i've have small doubt i need to know WHY?
我们正处于#lockdown 意味着我的一个兄弟 padu 问了这个问题。下面我有示例对象,因为在直接调用其 return 1 时有函数,但在分配为 const 和访问时作为函数 returns 未定义。哪位大神解释一下,对我的知识增长很有帮助。
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
您正在搜索 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())