JavaScript 命名规则

来源 :http://www.codelifter.com/main/tips/tip_020.shtml


The following are the rules for naming JavaScript variables:

1.
A variable name cannot start with a numeral. For instance, 3x or
2goats or
76trombones would all be
illegal variable names.

You can, however, have numbers within a JavaScript variable name; for instance
up2me or
go4it would both be perfectly valid variable names.

不能以数字开头

2.
You cannot have a mathematical or logical operator in a variable name. For instance,
2*something or
this+that would both be illegal... because the * and the + are arithmetic operators. The same holds true for ^, /, \, !, etc.

变量名中不能含有操作符

3.
You must not use any punctuation marks of any kind in a JavaScript variable name, other than the
underscore; for example... some:thing or
big# or
do‘to would all be
illegal.

The underscore is the exception, and it can be used at the beginning, within, or at the end of JavaScript variable names. You can use names like
_pounds or
some_thing or
gallons_ as variable names and they are perfectly legal.

变量名中不能有标点符号,除了_下划线以外的任何标点符号。

4.
JavaScript names must not contain spaces. Ever.

变量名中不能有空格

5.
You cannot use JavaScript keywords (parts of the language, itself) for variable names. Thus
window or
open or
location
or string
would be illegal.  Check a JavaScript reference if in
doubt as to whether something is or is not part of the language --
JavaScript has grown into a fairly fully-fleshed language, so you
may get some occasional surprises.

You can, of course, use what are otherwise keywords as parts of variable names. For instance,
thatWindow or
someString or
theLocation would all be perfectly acceptable.

变量中不能用JavaScript语言的关键词。如 window,string,blooean等等

6.
JavaScript variable names are case-sensitive. Programmers in other languages are often tripped up by this one, as some languages are not sensitive to case in variable names.

For instance, all of the following names would be considered completely different variable names in JavaScript: 
gasbag 
Gasbag  GasBag  gasBag

变量名区分大小写.

总结:

通常情况下变量名只能由大小写的英文字母和数字组成, 即A-Z a-z  0-9构成

只能以字母或者下划线开头。

不能包含特殊字符,不能用关键词作变量名。

-----------欢迎补充

时间: 2024-08-07 01:31:49

JavaScript 命名规则的相关文章

javascript命名规则

<p>1.javascript是一种弱类型语言</p> <p>2.javascript的变量名跟Java一样,是以字母,下划线_,$美元符号开头.只有var这种数学类型:var str="我是一个数据类型":var numbor=12;</p> <p>3.javascript定义方法:funtion --他是一个对象</p> <p> 动态参数:Arguments--</p> <p>

JavaScript标识符--------命名规则及其保留字

JavaScript的两种注释格式: 1. //这里是单行注释 2. /*这里是一段注释*/ 3. /*这里是 *多行注释 */ 标识符命名: 标识符,通俗的讲就是一个名字,可以用来对变量和函数进行命名,例如: 命名变量名: var luckname='weille'; //=>luckname就是这个变量luckname的标识符 命名函数名: function facename() { //=>facename就是这个函数facename的标识符 } 命名规则: 1.必须要以字母.下划线(_

javaScript中的数据类型和命名规则

有7种数据类型: undefined(未定义) null(空), boolean(布尔型) string(字符串) symbol(符号), number(数字) object(对象) 命名规则 Variable (变量)的名字可以由数字.字母.$ 或者 _组成,但是不能包含空格或者以数字为首. 注意: 当 JavaScript 中的变量被声明的时候,程序内部会给它一个初始值 undefined.当你对一个值为 undefined 的变量进行运算操作的时候,算出来的结果将会是 NaN,NaN 的意

JavaScript 变量命名规则

著名的变量命名规则Camel 标记法首字母是小写的,接下来的字母都以大写字符开头.例如: var testValue = 0, secondValue = "hi";Pascal 标记法 首字母是大写的,接下来的字母都以大写字符开头.例如: var TestValue = 0, SecondValue = "hi";匈牙利类型标记法 在以 Pascal 标记法命名的变量前附加一个小写字母(或小写字母序列),说明该变量的类型.例如,i 表示整数,s 表示字符串,如下

CSS书写规范、顺序和命名规则

一.CSS书写顺序 1.位置属性(position, top, right, z-index, display, float等)2.大小(width, height, padding, margin)3.文字系列(font, line-height, letter-spacing, color- text-align等)4.背景(background, border等)5.其他(animation, transition等) 二.CSS书写规范 1.使用CSS缩写属性 CSS有些属性是可以缩写的

CSS 命名规则

随着CSS的发展,使用CSS有语义化的命名约定和CSS层的分离,将有助于它的可扩展性,性能的提高和代码的组织管理. 在我前面的文章中讨论很多关于CSS的问题都可以通过使用一个适当的CSS策略来避免.在这篇文章里,我将着重于讨论使用一种方法或者一个命名规则所带来的好处. 这里有很多可供使用的前端方法和命名规则,每个都有自己的优缺点.在几乎所有的案例中CSS被分割成更易于管理的代码“块”.CSS的这种分割方式定义了每一种方法. 命名规则 一个可靠命名规则的重要性是不可忽视的.就像组织结构带来的好处一

文件命名规则

网站文件命名规则 关于文件的命名,看似无足重轻,但实际上如果没有良好的命名规则进行必要的约束,一味的乱起名称,最终导致的结果就是整个网站或是文件夹无法管理.所以,命名规则在这里同样非常重要. 需要特别注意的时候,网站文件或文件夹命名请尽量避免使用中文字符命名. 文件的命名 以最少的字母达到最容易理解的意义. 索引文件统一使用index.html文件名(小写) index.html文件统一作为"桥页",不制作具体内容,仅仅作为跳转页和meta标签页.主内容页为main.html. 按菜单

计算机语言变量命名规则

计算机变量一般命名规则为: 变量名首字母必须为字母(a-z A-Z),下划线(_),或者美元符号($)开始,php编程中所有变量必须以$开始.(有些编译器已经支持中文变量名了) 变量名只能是字母(a-z A-Z),数字(0-9),下划线(_)的组合,并且之间不能包含空格,数字不能放在变量名首位. 变量名不能使用编程语言的保留字.比如在javascript中不能使用true,false,while,case,break保留字等等. 在每个代码范围内使用足够短和足够长的名称:例如循环计算器用一个字符

BEM命名规则和规范

BEM命名规则:http://segmentfault.com/a/1190000000391762 class命名方案:http://www.w3cplus.com/css/css-class-name.html 常用的CSS命名规范:http://www.html5cn.org/article-7600-1.html 通用CSS笔记.建议与指导:https://github.com/chadluo/CSS-Guidelines/blob/master/README.md 切勿将标记 CSS