这不是 return 对象 属性(箭头,this)
this does not return object property (arrow, this)
var std_obj = {
activeEffect : 'fade',
name : 'Jane',
displayMe() {
const doSomeEffects = () => {
if (this.activeEffect === 'fade') {
console.log(this.name); // this.name have accesss and logs
// return this.name --> gives undefined ? why can't I return?
}
};
doSomeEffects();
}
};
console.log(std_obj.displayMe());
// console.log(this.name) 有效但我不能 return this.name 我有点沮丧
您不是 return 来自 displayMe
,您 return 来自 doSomeEffects
。 displayMe
的最后一行是 doSomeEffects()
,没有 return 值。你需要 return doSomeEffects()
.
displayMe() {
const doSomeEffects = () => {
if (this.activeEffect === 'fade') {
console.log(this.name); // this.name have accesss and logs
return this.name // return from `doSomeEffect`
}
};
return doSomeEffects(); // return from `displayMe`
}
箭头函数将采用其声明范围的 this
。这就是为什么这里的 this
会指向对象。但是要查看该值,您必须像这样更改代码:
var std_obj = {
activeEffect : 'fade',
name : 'Jane',
displayMe() {
const doSomeEffects = () => {
if (this.activeEffect === 'fade') {
console.log(this.name); // this.name have accesss and logs
return this.name; //uncomment this
}
};
return doSomeEffects(); //if you do not use return statement, then it will by default return undefined
}
};
console.log(std_obj.displayMe());
如果您不 return 来自 displayMe() 或 doSomeEffects() 的任何内容,它将不会显示 undefined
。原因是函数默认 return undefined
。要对此进行测试:只需 运行 console.log("hello");
在开发控制台中。这将显示 hello 和 undefined。
注意:如果使用函数表达式而不是箭头函数,它将return未定义。这就是箭头函数的力量。
我不确定,但我认为
var std_obj = {
activeEffect : 'fade',
name : 'Jane',
displayMe() {
const doSomeEffects = () => {
if (this.activeEffect === 'fade') {
console.log(this.name);
return this.name
}
};
return doSomeEffects();
}
};
console.log(std_obj.displayMe());
这就是你想要做的
doSomeEffects
在 displayMe
returns this.name 中并且您正在调用 displayMe
我说得对吗?
var std_obj = {
activeEffect : 'fade',
name : 'Jane',
displayMe() {
const doSomeEffects = () => {
if (this.activeEffect === 'fade') {
console.log(this.name); // this.name have accesss and logs
// return this.name --> gives undefined ? why can't I return?
}
};
doSomeEffects();
}
};
console.log(std_obj.displayMe());
// console.log(this.name) 有效但我不能 return this.name 我有点沮丧
您不是 return 来自 displayMe
,您 return 来自 doSomeEffects
。 displayMe
的最后一行是 doSomeEffects()
,没有 return 值。你需要 return doSomeEffects()
.
displayMe() {
const doSomeEffects = () => {
if (this.activeEffect === 'fade') {
console.log(this.name); // this.name have accesss and logs
return this.name // return from `doSomeEffect`
}
};
return doSomeEffects(); // return from `displayMe`
}
箭头函数将采用其声明范围的 this
。这就是为什么这里的 this
会指向对象。但是要查看该值,您必须像这样更改代码:
var std_obj = {
activeEffect : 'fade',
name : 'Jane',
displayMe() {
const doSomeEffects = () => {
if (this.activeEffect === 'fade') {
console.log(this.name); // this.name have accesss and logs
return this.name; //uncomment this
}
};
return doSomeEffects(); //if you do not use return statement, then it will by default return undefined
}
};
console.log(std_obj.displayMe());
如果您不 return 来自 displayMe() 或 doSomeEffects() 的任何内容,它将不会显示 undefined
。原因是函数默认 return undefined
。要对此进行测试:只需 运行 console.log("hello");
在开发控制台中。这将显示 hello 和 undefined。
注意:如果使用函数表达式而不是箭头函数,它将return未定义。这就是箭头函数的力量。
我不确定,但我认为
var std_obj = {
activeEffect : 'fade',
name : 'Jane',
displayMe() {
const doSomeEffects = () => {
if (this.activeEffect === 'fade') {
console.log(this.name);
return this.name
}
};
return doSomeEffects();
}
};
console.log(std_obj.displayMe());
这就是你想要做的
doSomeEffects
在 displayMe
returns this.name 中并且您正在调用 displayMe
我说得对吗?