Multiple key names, same pair value

后端 未结 9 1734
萌比男神i
萌比男神i 2020-12-08 13:33

I\'m trying to setup an object literal in a JavaScript script that has a key with multiple names. referring to the same object value i.e. something like these that I have al

9条回答
  •  情话喂你
    2020-12-08 14:12

    //create some objects(!) you want to have aliases for..like tags
    var {learn,image,programming} = 
     ["learn", "image", "programming"].map(tag=>({toString:()=>tag }));
    
    
    //create arbitrary many aliases using a Map
    var alias = new Map();
    alias.set("photo", image);
    alias.set("pic", image);
    alias.set("learning", learn);
    alias.set("coding", programming);
    
    //best put the original tagNames in here too.. 
    //pretty easy huh? 
    
    // returns the image object 
    alias.get("pic");
    
    // ;)
    

提交回复
热议问题