Is there a shorthand for this in ES6/ES7?

梦想的初衷 提交于 2019-12-11 01:20:21

问题


I'm having a bit of a brainfart. Is there a shorthand for this in ES6/ES7?

res.locals.hello = hello

I've tried a few different combinations but can't get anything to stick.


回答1:


I don't believe there is a shorter way to arbitrarily attach a new key to an object, and automatically assign a reference with the same name. However, during the construction of your locals object, you can simply provide the handler:

let res = {
    locals: { hello }
};

This is effectively the same as:

let res = {
    locals: {
        hello: hello
    }
};

This enhancement was added in ES6, and is supported by all transpilers to my knowledge.




回答2:


Yes, assuming res already exists, using res.locals = { hello } works just fine.



来源:https://stackoverflow.com/questions/33658893/is-there-a-shorthand-for-this-in-es6-es7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!