<分区>
据我了解,箭头函数没有 this 关键字,它采用父级 this 值。那么这是如何工作的呢? func 属性中 this 的值不是指向 window 对象吗?它如何指向类并返回名称?
class app {
constructor(name){
this.name=name
}
func() => {return this.name}
}
let a = new app('william')
a.func()
// william
标签 javascript
<分区>
据我了解,箭头函数没有 this 关键字,它采用父级 this 值。那么这是如何工作的呢? func 属性中 this 的值不是指向 window 对象吗?它如何指向类并返回名称?
class app {
constructor(name){
this.name=name
}
func() => {return this.name}
}
let a = new app('william')
a.func()
// william
最佳答案
您发布的代码将 func
初始化为实例变量,而不是类方法。因此就好像你的类声明看起来像:
class app {
constructor(name) {
this.func = () => {return this.name};
this.name=name;
}
}
类声明中的 name = value
语法尚未得到普遍支持(据我所知)。
关于javascript - 类中的箭头函数方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56622004/
相关文章:
javascript - 如何修复 jQuery 1.8 “unsupported pseudo: all” 错误?
javascript - React 将函数组件转换为类组件
javascript - 在 Firefox 中检测对 "input"事件的删除操作
javascript - 单选按钮焦点/使用 TAB 键盘选择
javascript - iframe youtube api 视频事件设置
javascript - HTML 和 CSS : How to lay out a div tag with width:100% and left:300px
javascript - 显示显示长度 ="none";使用 JavaScript