对象字面量中带有空格的属性名称 [英] Property names with spaces in object literals

查看:53
本文介绍了对象字面量中带有空格的属性名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想知道,一"、二"、三"的东西,可以有空格吗?所以不是一个",它可以是一个喵"?

I'm just wondering, with the "one", "two", "three" stuff, could there be a space? So instead of "one" it could be "one meow"?

var meow    = { one:        function (t) { return "a"; },
                two:        function (t) { return "b"; },
                three:      function (t) { return "c"; }
              };

推荐答案

当然,属性名称中可以有空格,但是你必须将它们括在 " 中:

Sure, there can be spaces in property names, but then you have to enclose them in ":

var meow    = {
            "one meow": function (t) { return "a"; },
            two:        function (t) { return "b"; },
            three:      function (t) { return "c"; }
            };

当您想稍后访问该属性时,请使用括号语法:

When you want to access that property later, use the bracket syntax:

console.log( meow["one meow"]() );

这篇关于对象字面量中带有空格的属性名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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