繁体   English   中英

如何获取箭头函数内的值

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM