目录
js的变量
-
变量提升
1.用var定义的变量会变量提升
2.定义的函数会变量提升
3.在函数体内,直接定义变量 num=100,在全局作用域下也可以访问
// 变量提升:用var定义变量,变量会提升到顶部,值为undefined console.log(num) var num = 100; /**********************/ //var 变量提升 var num; console.log(num) num = 100; console.log(num) num = 100; /*********************************/ // var num=100; //③全局变量,在任何位置可以访问 function Demo() { // var num = 100; //①局部变量 num = 100; //②定义的全局变量,在函数外部可以访问 } Demo() console.log(num) /****************************/ // 函数也会提升 Demo() //100 function Demo() { num = 100; console.log(num) }
命名规范
1.1 大驼峰
/**********大驼峰************/ class PersonAddress { } new PersonAddress() /*****构造函数******/ function Person(){ }
2.2小驼峰
function getBookCount() { } function setName(){ //设置 } function isEnglish(){ //判断用is开头 } function createCar(){ } function canRead(){ //能不能 } function hasBook(){ //判断有没有 }
js作用域(scope)
-
全局作用
-
变量的提升
-
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <script> // 变量提升:用var定义变量,变量会提升到顶部,值为undefined // console.log(num) // var num = 100; /**********************/ //var 变量提升 var num; console.log(num) num = 100; console.log(num) num = 100; /*********************************/ // var num=100; //③全局变量,在任何位置可以访问 function Demo() { // var num = 100; //①局部变量 num = 100; //②定义的全局变量,在函数外部可以访问 } Demo() console.log(num) /****************************/ // 函数也会提升 Demo() //100 function Demo() { num = 100; console.log(num) } </script> </head> <body> </body> </html>
-
函数作用域
-
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <script> // let没有变量提升 // console.log(num) // let num = 100; // const没有变量提升 // console.log(NUM) // const NUM=100; // console.log(NUM) /********小驼峰:自定义函数************/ function getBookCount() { } function setName(){ //设置 } function isEnglish(){ //判断用is开头 } function createCar(){ } function canRead(){ //能不能 } function hasBook(){ //判断有没有 } /**********大驼峰************/ class PersonAddress { } new PersonAddress() /*****构造函数******/ // function Person(){ // } function demo() { console.log(this) } // demo() //普通函数:this指向window let p = new demo() //构造函数(实例化):this指向实例化对象 </script> </head> <body> </body> </html>
-
块作用域
-
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <script> /************改变this指向* * call 立即执行 * apply 立即执行,参数放到一个数组里 * bind 不会立即执行 * *************/ function Demo(uname, age) { // console.log(this) console.log(uname, age) } //call bind apply let obj = {} // Demo.call(obj, "张三", 21) //改变this指向,并立即执行 // let f = Demo.bind(obj, "张三", 21) //改变this指向,不立即执行 // f() //执行新的函数 Demo.apply(obj, ["张三", 21]) //改变this指向,并立即执行;参数放到数组里 </script> </head> <body> </body> </html>
for循环
white循环
if嵌套
数据类型
- 基础数据类型:
- 复杂数据类型
类型检测和类型转换
- 类型检测常用方法
- 显式类型转换