Why does having [].map with curly brackets change the way it works?

后端 未结 4 1077
轮回少年
轮回少年 2021-01-23 02:15

So, basically I have this:

Array.prototype.toString = function() {
    return (\"[\" + this.map(thing => thing = \'\"\' + thing + \'\"\').join(\', \') + \"]\"         


        
4条回答
  •  伪装坚强ぢ
    2021-01-23 02:54

    You do not return something in the block statement of the arrow function

    () => {
        // code
        return 42; // return is required in block statment
    }
    
    
    () => 42 // return is implicit
    

    Array.prototype.toString = function() {
        return ("[" + this.map(thing => { return thing = '"' + thing + '"'}).join(', ') + "]")
    };
    
    console.log(['foo', 'bar'].toString())

    Shorter, without assignment to thing

    Array.prototype.toString = function() {
        return ("[" + this.map(thing => '"' + thing + '"').join(', ') + "]")
    };
    
    console.log(['foo', 'bar'].toString())

提交回复
热议问题