JavaScript对象字面值中的单引号 [英] Single quotes in JavaScript object literal

查看:74
本文介绍了JavaScript对象字面值中的单引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 < script type =text我正在查看Google Maps API教程。 / javascript
src =http://www.google.com/jsapi?autoload={'modules':[{name:'maps',version:3,other_params:'sensor = false'}] }>< /脚本>

为什么 modules 包裹在单引号中?

解决方案

尽管不是严格要求,但将键包含在引号中是一种很好的做法,以避免与 JavaScript保留字



试想一下,如果您有 class 而不是 modules - class 碰巧是JavaScript中的一个保留字,即使它在当前规范中并未实际使用。


I'm looking at the Google Maps API tutorial, and I see this:

<script type="text/javascript" 
        src="http://www.google.com/jsapi?autoload={'modules':[{name:'maps', version:3, other_params:'sensor=false'}]}"></script>

Why is modules wrapped in single quotes?

解决方案

It's a good practice to wrap keys in quotes, even though not strictly required, in order to avoid the possibility of conflicts with JavaScript reserved words.

Imagine if you had class instead of modules - class happens to be a reserved word in JavaScript, even though it is not actually used in the current specification.

这篇关于JavaScript对象字面值中的单引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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