TypeScript 函数-Lambads 和 this 关键字的使用

let people = {
name:["a","b","c","d"],
/*
getName:function(){
    return function(){
        var i=Math.floor(Math.random()*4);
        return {
            //this 指代的是getName 不是people 引入lamdads即可
            n:this.name[i]
        }
    }
}*/
getName:function(){
    return ()=>{
        var i=Math.floor(Math.random()*4);
        return {
            //this 指代的是getName 不是people 引入lamdads即可
            n:this.name[i]
        }
    }
}
}

var myname = people.getName();
alert(myname().n);

  

原文地址:https://www.cnblogs.com/allyh/p/10692788.html