JavaScript
1. 变量
(1) 变量的定义 const 和 let, 其中const 定义的是常量,不可以重复赋值,一般来说数据不可改变, let定义的变量是可以改变的
1 2
| let num = 123; alert(num);
|
(2) JS 中常用的变量类型:
1) String
2) Boolean
3) Numbers
4) object: Array, Undefined, Null
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| const num_1 = 111; const num_2 = 1.1;
const str_test = "abc";
const bool_test = true;
let array_test = [1,2,3];
let undefined_test = undefined;
console.log("typeof num_1", typeof num_1);
console.log("typeof num_2", typeof num_2);
console.log("typeof str_test", typeof str_test);
console.log("typeof bool_test", typeof bool_test);
console.log("typeof array_test", typeof array_test);
console.log("typeof undefined_test", typeof undefined_test);
|
(3) 字符串的相关操作
1) 字符串拼接
1 2 3 4 5 6 7 8 9 10 11 12 13
| console.log("num_1 = " + num_1,'and num_2 = ' + num_2);
console.log(`num_1 = ${num_1}, num_2 = ${num_2}`);
console.log("str_test length = " + str_test.length);
console.log("str_test toLocalUpperCase=",str_test.toLocaleUpperCase())
console.log("str_test substring(0,2)", str_test.substring(0,2))
let new_str = "a b c"
console.log("new_str split",new_str.split(" "))
|
2.数组
(1) 数组的相关操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| const new_array = ['a','b','c']
console.log("new_array[1]",new_array[1])
new_array[2] = "d"
new_array.push("e")
new_array.unshift("1")
new_array.pop()
console.log(new_array)
console.log("Array.isArray(new_array)",Array.isArray(new_array))
console.log("new_array.indexOf('a')",new_array.indexOf('a'))
|
3.对象
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| const person = { firstName: "John", lastName: "Doe", age: 30, hobbies: ['music', 'movies', 'sports'], address :{ street: "50 main st", city: "Boston", state: "NA" } }
console.log('person', person) console.log('persom.firstName', person['firstName']) console.log('person.hobbies[0]', person.hobbies[0]) console.log('person.address.street', person.address.street)
const{ firstName, lastName, address: {city} } = person;
console.log("firstName=",firstName)
console.log("city=",city)
const todos = [ { id: 1, Name: "Jack" }, { id: 2, Name: "Tom" } ];
console.log("todos",todos)
console.log("todos[1].Name=",todos[1].Name)
const todoJson = JSON.stringify(todos)
console.log(todoJson)
|
4. 循环语句
(1) for 相关语句
1 2 3
| for(let i = 0; i < 6; i++){ console.log(`i=${i}`) }
|
(2) while 相关语句
1 2 3 4 5 6
| let num = 0
while(num < 6){ console.log("num = ", num); num++ }
|
(3) for-each的使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| const todos = [ { Id: 1, Name: "Jack" },{ Id: 2, Name: "Tom" },{ Id: 3, Name: "Jerry" }, ]
todos.forEach( function(t){ console.log("t.Name = ", t.Name) } )
|
(4) map的使用
1 2 3 4 5 6 7 8
| const t = todos.map( function(t){ return t.Id; } )
console.log("t = ", t)
|
(5) filter 过滤器,过滤满足条件的数据
1 2 3 4 5
| const tName = todos.filter( function(t){ return t.Name.length > 3; } )
|
5.判断语句
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
|
let num = 1;
if(num == 1){ console.log("num = ", num) }
if(num == '1'){ console.log("num = ", num) }
if(num === 1){ console.log("num = ", num) }
let score = 90
if (score >= 60 && score <= 70){ console.log("成绩及格了") }else if(score > 70){ console.log("优秀") }else{ console.log("不及格") }
|
6. 逻辑运算
注意:
1 2
| false: undefined, 0, '',null, false //这些值默认是false true:除了上述一般来说都是true
|
(1)与 &&
1 2 3 4 5 6 7 8 9
| let score = 60
if (score > 60 && score < 70){ console.log("良") }else if(score >= 70){ console.log("优秀") }else{ console.log("不及格") }
|
(2) 或 ||
1 2 3 4 5
| if(age < 0 || age > 200){ console.log("非法年龄") }else{ console.log("age=", age) }
|
(3) 三元运算符
1 2 3 4 5
| const xxx = 9;
const color = xxx > 10 ? 'red' : 'green';
console.log("color = ",color)
|
(4) switch 语句
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
let color = "red"
switch (color){ case 'red': console.log("红色"); break; case 'blue': console.log("蓝色"); break; default: console.log("未知"); break; }
|
7. 函数
(1)普通函数
注意: 普通函数的首字母是不可以大写的
1 2 3 4 5 6 7 8 9 10
| console.log("function 函数演示")
function add(num_1 = 0, num_2 = 0){ return num_1 + num_2 }
let res = add(1,2)
console.log("res = ", res)
|
(2) 箭头函数(相当于匿名函数)
注意: 匿名函数必须有变量接收
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
const kniferes = (num_1, num_2) =>{ return num_1+num_2 }
addres = kniferes(1,2) console.log(addres)
const singleline = () => console.log("123")
singleline()
const addline = (num_1, num_2) => num_1 + num_2 console.log("addline(1,2) = ", addline(1,2))
const test = num_1 => num_1 + 12;
console.log("test=",test(1))
|
8. 对象
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
const book_1 = { title : "明朝那些事", author: "朱元璋", year: "2013", getSummary: function(){ return `${this.author} 在 ${this.year} 写了 ${this.title}` } }
console.log(book_1.getSummary())
console.log("Object.Keys(book_1)", Object.keys(book_1))
console.log("Object.Values(book_1)", Object.values(book_1))
|
9. 构造函数
(1)构造函数,相当于其他语言中类的概念
存在的问题: 比如构造函数中存在方法,这个时候会在所有的对象中创建该方法。造成了内存的浪费。

因此,JS为了解决上述问题,推出了原型链的概念
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
function Book(title, author, year){ this.title = title; this.author = author; this.year = year; this.getSummary = function(){ return `${this.author} 在 ${this.year} 写了 ${this.title}这篇文章` } }
book_1 = new Book("明朝那些事", "朱元璋","2020")
console.log(book_1.getSummary())
|
(2)原型链
通过原型链创建构造函数的方法,只有对象使用时,才会创建。
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
function Book(title, author, year){ this.title = title; this.author = author; this.year = year }
Book.prototype.getSummary = function(){ return `${this.author} 在 ${this.year} 写了 ${this.title}` }
|