多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
[TOC] ### 1.1 如何声明变量 ~~~ var a; //如果声明一个变量没有赋值,那么结果就是undefined console.log(a); //undefined; ~~~ #### 1.1.1 let ~~~ /* es6之前js没有块级作用域 */ for(let i =0;i<3;i++){ console.log(i); } { let m =10; } console.log(m); ~~~ ~~~ let a =10 ; let a = 20; console.log(a) //报错 ~~~ #### 1.1.2 var * var 定义的全局变量 ~~~ var a = 10; var a =20; console.log(a) //20 ~~~ #### 1.1.3 var和let易错实例`还需谷歌` ~~~ var a = 1; function test(){ console.log(a); var a = 2; } test(); 结果 undefined 原因 a未被赋值明提前 console.log(a); var a = 2; 相当于 var a ; console.log(a); a = 2; ~~~ ~~~ var a = 1; function test(){ console.log(a); let a = 2; } test(); 结果 报错 原因 a 未被定义 ~~~ ### 1.2标识符(变量名,函数名,属性名)的命名规范 * 1 关键字和保留字不能作为命名 | [abstract](https://baike.baidu.com/item/abstract) | [assert](https://baike.baidu.com/item/assert) | [boolean](https://baike.baidu.com/item/boolean) | break | [byte](https://baike.baidu.com/item/byte) | | ----------------------------------------------------- | --------------------------------------------------------- | --------------------------------------------------- | ----------------------------------------------------- | --------------------------------------------- | | case | [catch](https://baike.baidu.com/item/catch) | [char](https://baike.baidu.com/item/char) | [class](https://baike.baidu.com/item/class) | const | | continue | [default](https://baike.baidu.com/item/default) | [do](https://baike.baidu.com/item/do) | [double](https://baike.baidu.com/item/double) | [else](https://baike.baidu.com/item/else) | | [enum](https://baike.baidu.com/item/enum) | [extends](https://baike.baidu.com/item/extends) | [final](https://baike.baidu.com/item/final) | [finally](https://baike.baidu.com/item/finally) | float | | true | false | null | | | | [for](https://baike.baidu.com/item/for) | goto | [if](https://baike.baidu.com/item/if) | [implements](https://baike.baidu.com/item/implements) | [import](https://baike.baidu.com/item/import) | | [instanceof](https://baike.baidu.com/item/instanceof) | [int](https://baike.baidu.com/item/int) | [interface](https://baike.baidu.com/item/interface) | long | native | | new | [package](https://baike.baidu.com/item/package) | [private](https://baike.baidu.com/item/private) | [protected](https://baike.baidu.com/item/protected) | [public](https://baike.baidu.com/item/public) | | [return](https://baike.baidu.com/item/return) | [strictfp](https://baike.baidu.com/item/strictfp) | [short](https://baike.baidu.com/item/short) | [static](https://baike.baidu.com/item/static) | [super](https://baike.baidu.com/item/super) | | [switch](https://baike.baidu.com/item/switch) | [synchronized](https://baike.baidu.com/item/synchronized) | [this](https://baike.baidu.com/item/this) | [throw](https://baike.baidu.com/item/throw) | [throws](https://baike.baidu.com/item/throws) | | [transient](https://baike.baidu.com/item/transient) | try | [void](https://baike.baidu.com/item/void) | [volatile](https://baike.baidu.com/item/volatile) | while | * 2 由字母,数字,下划线或美元符号($)组成 * 3不能以数字开头 ### 1.3声明提前 > 概念:js会将所有声明的变量,放在作用域的顶部集中创建,赋值留在原地 ~~~ console.log(a); //undefined var a = 20; ~~~ ### 1.4严格模式`use strict` ~~~ //js声明一个变量可以不使用var,js执行时自动补全 b=10; console.log(b); ~~~ ~~~ //如果严格模式,声明变量必须使用var "use strict"; a = 20; console.log(a); //报错 ~~~ ### 1.5 常量 ~~~ //=是赋值的意思 /* 常量:创建之后不能修改 */ const a = 3.14;//创建常量 //a = 40;增加修改报错 console.log(a); //3.14 ~~~ ### 1.6 变量范围 `全局变量`:在函数外面声明的就叫全局变量 作用范围:整个window `局部变量`:在函数内部声明的变量就叫局部变量 作用fanwei:在函数的大括号中 ~~~ var g = 20; console.log(g); function b(){ var c = 30 ; //在函数内部,声明的变量如果没有使用var关键字,js将其识别为全局变量 console.log(c); } b(); console.log(c);c不能被输出 ~~~ ### 1.7转义字符`\` ~~~ var a ="hello world\"good\""; var str = "hello\nworld" var str = "hello\\nworld" console.log(a); console.log(str); ~~~