How to magically get the Object key?

Can I get a key protagonist in the transfer function?

let books = {
  hero: translate([
    'Batman',
    'Superman',
    'Ironman'
  ])
}

function translate(param1) {
  return ...
}
Run codeHide result

Maybe there is some magic, how to get the name of the calling key? :) Thanks!

+4
source share
1 answer

You ask if a function called the part of the property initializer in the object initializer can access the property name, and the return value will be used to initialize it.

No, he can not. Functions do not know how their return value will be used.

+4
source

Source: https://habr.com/ru/post/1690899/


All Articles