Javascript 箭头功能可以';无法从对象获取键和值

Javascript 箭头功能可以';无法从对象获取键和值,javascript,function,object,Javascript,Function,Object,我现在正在学习JS对象,我想了解它为什么会发生。这是我的密码 const User = { name: 'Lily', } const showUser = () => console.log(this.name); const showUser2 = function(){ console.log(this.name); } showUser2.bind(User)(); 如何使用箭头函数从名为User的对象获取键和值? 当然,如果我使用.bind使用普通

我现在正在学习JS对象,我想了解它为什么会发生。这是我的密码

    const User = {
    name: 'Lily',
}

const showUser = () => console.log(this.name);

const showUser2 = function(){
    console.log(this.name);
}

showUser2.bind(User)();
如何使用箭头函数从名为User的对象获取键和值?
当然,如果我使用.bind使用普通函数,它会工作。

您可以将
User
对象作为参数传递给
showUser

const User = {
    name: 'Lily',
}
const showUser = (user) => console.log(user.name);
showUser(User);

您可以将
User
对象作为参数传递给
showUser

const User = {
    name: 'Lily',
}
const showUser = (user) => console.log(user.name);
showUser(User);

要么在用户内部声明showUser,要么将参数传递给函数,因此如果我使用“this”,函数必须在对象中?是的,因为“this”或多或少是“我所说的对象类的这个具体实例”(向纯粹主义者道歉:-P)ups!我错过了你没有使用
class
这个词,而是
const
!那时我写的东西不管用
class User{name='Lily';showUser(){console.log(this.name)}}
will-workSoon我将学习对象中的类:D非常感谢!要么在用户内部声明showUser,要么将参数传递给函数,因此如果我使用“this”,函数必须在对象中?是的,因为“this”或多或少是“我所说的对象类的这个具体实例”(向纯粹主义者道歉:-P)ups!我错过了你没有使用
class
这个词,而是
const
!那时我写的东西不管用
class User{name='Lily';showUser(){console.log(this.name)}}
will-workSoon我将学习对象中的类:D非常感谢!