ES6 / ES7有没有这样的简写? [英] Is there a shorthand for this in ES6/ES7?

查看:140
本文介绍了ES6 / ES7有没有这样的简写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一点脑筋在ES6 / ES7中有这样的简写吗?



res.locals.hello = hello



我已经尝试了几种不同的组合,但无法得到任何东西。

解决方案

我不认为有一个较短的方式来将一个新的密钥附加到一个对象上,并且自动分配一个具有相同名称的引用。但是,在构建您的本地人对象期间,您可以简单地提供处理程序:

  let res = {
本地人:{hello}
};

这与以下有效的一样:

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

这个增强功能是在ES6中添加的,并且受到我所知道的所有透明胶片的支持。


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.

解决方案

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.

这篇关于ES6 / ES7有没有这样的简写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆