[英]How to Get values inside Arrow function
如何在Arrow function
获取值,它位于对象内部。
我想访问该对象的其他属性。
let obj = { name:'Ashu', age:123, show: ()=>{ // I want to print Name and Age how to do that // Is there any other way to do get Name and Age Property // I don't want to replace the Arrow function console.log(obj.name, obj.age); } } obj.show()
这应该有效:
let obj = { name: 'Ashu', age: 123, show: () => { console.log(obj.name, obj.age); } } obj.show();
箭头函数不会捕获当前的this
,为了捕获this
您需要编写一个常规函数。
let obj = { name:'Ashu', age:123, show: function() { console.log(this.name, this.age); } }; obj.show();
class Obj { constructor(){ this.name ='Ansh'; this.age = 123; this.show = () => { console.log(this.name, this.age); } } } let obj = new Obj(); obj.show();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.