目录
定义(声明)变量
1. var
var应用的作用域有全局作用域和局部作用域,在if、for、while等块级作用域中是不能成为判断和循环独有的变量,因此if、for、while在全局中时,if、for、while中定义的是全局变量,if、for、while在局部中时,if、for、while中定义的是局部变量。
// 例:
window.onload = function() {
for(var i=0; i<10; i++) {
alert(i); // 0,1,2,3,4,5,6,7,8,9
}
alert(i); // 10
}
function() {
If(true) {
var i = 10;
}
alert(i); //10
}
var存在变量提升的问题,在作用域中定义的变量会先在作用域开始时定义,在赋值行赋值。
// 例:
function f1() {
alert(a); // 显示undefined
var a = 5;
}
var在同一作用域可以重复定义变量,只不过后定义的会覆盖之前定义的。
// 例:
var a = 10;
var a = 9;
alert(a); // a=9
2. let 相当于var,但是不属于window
let应用于块级作用域{...},在if、for、while等块级作用域中是能成为判断和循环独有的变量,不会污染块级作用域之外的区域。
// 例:
window.onload = function() {
for(let i = 0; i<10; i++) {
alert(i); // 0,1,2,3,4,5,6,7,8,9
}
alert(i); // 报错
}
function() {
If(true) {
let i=10;
}
alert(i); //报错
}
不存在变量提升的情况。只能先定义再使用。
// 例:
function f1() {
alert(a); // 显示undefined
var a = 5;
}
function f2() {
alert(a); // 报错
let a = 5;
}
let在同一作用域中不能重复定义。
// 例:
let a = 10;
let a = 9;
alert(a); // 报错
let在不同作用域可以重复定义,最典型的是for循环。
// 例:
for(let i = 0; i<5; i++) {
let i = 8;
console.log(i); // i=8
}
// 第一个i在(...)中相当于父级作用域,而第二个i在{...}中相当于子级作用域。
let和var都可以被子作用域继承。
// 例:
function f1( ) {
let i = 9;
for(let j = 0; j<2; j++) {
console.log(i);
}
}
f1(); // 9
3. const常量
const是常量(在同一作用域内不可更改),但是对象除外。对象被修改不是因为常量可以修改,而是因为对象的特性允许被修改。取消修改可以用Object.freeze(对象) 冻结。
// 例1:
const a = 2;
const a = 90; // 报错
// 例2:
const arr= ["one"];
arr.push("two");
alert(arr) // ["one","two"]
// 冻结
const arr= Object.freeze(["one"]);
arr.push("two");
alert(arr) // 报错
特性和let一样
例:
const a = 2;
function show() {
alert(a); // 报错
const a = 5;
}
.......
解构赋值
1. 在数据交互(Ajax)用处较大
// 例:
let json = {
name: "jack",
age: 18
};
let {name, age} = json;
let {name: a, age: g} = json; // 可以起别名
console.log(name, age) // jack 18
console.log(a, g) // jack 18
2. 定义:let/var [自定义变量名1, 自定义变量名2, ....] = [arr1,arr2,......]
// 例:
let [a, b, c] = [12, "html", 7];
alert(a, b, c) //12 "html" 7
let [a, [b, c]]=[12, ["html", 7]];
alert(a, b, c) //12 "html" 7
// 注意: 左右两边,结构格式要保持一致。
3. 解构时候可以给默认值:
// 例:
let [a, b, c = "默认值"] = ["aa", "bb"];
alert(a, b, c) //aa bb 默认值
// PS:如果c传过来的数据是null,则无法用这样的格式赋默认值。
4. 解构赋值可以实现两个数交换
// 例:
let a = 5;
let b = 6;
[a, b]=[b, a]
alert(a, b); //6 5
5. 解构赋值可以实现函数返回值的解构
// 例:
function getPos() {
......
return {
left: 10,
top: 2
}
}
let {left, top: t} = getPos();
console.log(left, t); // 10, 2
// 例:
function show({a = "默认1", b = "默认"} = {}) {
console. log(a,b);
}
show({}); // "默认1", "默认"
字符串
1. 模板: ` ....${变量} ....`
// 例:
let name = 'jack';
let age = 18;
let str = `这个人叫${name},年龄是${age}岁`;
// Ps: 可以随意换行
2. 字符串的方法
include(“查找内容”) // 字符串查找
// 例:
let str = "const是常量(在同一作用域内不可更改), 但是对象除外"
console.log(str.includes(“对象”)); // 返回true
// PS:相似方法indexof(“查找内容”); // 返回查找内容的位置标号
// 常用功能: 判断浏览器
If (navigator.userAgent.includes(“Chrome”)) {
alert(“这是谷歌浏览器”)
}
startsWith(“字符串”) // 字符串以 ? 开头
// 例:
let str1 = "E:\专业学习笔记\ES6";
let str2 = "https://study.163.com/course/courseLearn.htm";
console.log(str1.startsWith("http")); // false
console.log(str2.startsWith("http")); // true
// PS:常用功能:检测网址
endsWith("字符串") // 字符串以 ? 结尾
// 例:
let str1 = "ES…….docx";
let str2 = "1525439897.zip";
console.log(str1.startsWith("zip")); // false
console.log(str2.startsWith("zip")); // true
// PS:常用功能:检测文件类型
repeat(次数)
// 例:
let str = "jack";
console.log(str.repeat(3)); // jackjackjack
padStart(整个字符串长度,填充内容) 往前填充
// 例:
let str1 = "jack";
let str2 = "第一"
console.log(str1.padStart(str1.length + str2.length, str2)) //第一jack
padEnd(整个字符串长度,填充内容) 往后填充
// 例:
let str1 = "jack";
let str2 = "第一"
console.log(str1.padEnd(str1.length+str2.length, str2)) // jack第一
函数
1. 设置默认值
// 例:
function show(a, b = 'jack') {
console.log(a + b);
}
show('欢迎'); // '欢迎jack'
2. 函数参数声明之后,不能用let和const在函数体内重复声明同名变量。但是var可以,会覆盖函数的变量,不推荐用var。
// 例:
function show(b = '马督') {
let b = '新用户';
console.log(b); // 错误
}
show('欢迎');
3. 扩展运算符/reset运算符 (...) 。可以将多个参数合成为一个数组,也可以将一个数组的每一项都拆成独立个体。
// 例:
// 1,2,5,8,9 ---> [...a ]---> [1,2,5,8,9]
// [1,2,5,8,9] ---> ...[1,2,5,8,9] ---> 1,2,5,8,9
function sort(...a) {
a.sort(); // 将传入的参数合成一个数组,再进行排序
console.log(a) // [1,2,5,8,9]
}
show(1,8,2,5,9);
// 例:
function sort(a,b,c,d,e) {
console.log(a,b,c,d,e) // 1,8,2,5,9
}
show(...[1,8,2,5,9]);
// 例1:
function show (a,b,...c) { // 将剩余的参数合为一个数组
console.log(a, b); // 1, 2
console.log(c); // [3, 4, 5]
}
show(1,2,3,4,5);
// PS:作剩余预算符时,只能放在最后
4. 箭头函数
(参数1,参数2...)=>{...语句... return ...} = function(参数1,参数2...){...语句... return ...},如果只有一个参数就无须写(),直接 参数=>{...语句... return ...}
// 例:
let show= (a, b) => {
console. log(a, b); // a=1 b=2
return a+b; // 3
}
show(1, 2);
PS:(1). 原生js中this指向调用的对象,谁调用就是谁,箭头函数中this指向定义this的那个对象(特别针对定时)。
// 例:
var id = 10;
let json = {
id: 1,
show: function() {
setTimeout(function() {
alert(this.id); // 这里的this指向window,因此打印出来的是10.
}, 2000);
}
};
json.show();
// 例:
var id = 10;
let json = {
id: 1,
show: function() {
setTimeout(() => {
alert(this.id); // 这里的this指向json,因此打印出来的是1.
},2000);
}
};
json.show();
PS:(2). 箭头函数里没有arguments,用扩展运算符...代替arguments。
// 例:
let show = () => {
console.log(arguments); //报错
}
show(1,2,3,4,5);
let show = (...a) => {
console.log(a); //[1,2,3,4,5]
}
show(1,2,3,4,5);
PS:(3). 箭头函数不能当做构造函数。
let show = () => {
this.name = 'jack';
}
let name = new show();
alert(name.name); // 报错
数组
1. 循环
arr.forEach(function(val, index, arr){...}), this指向的对象)
//代替普通for循环例:
// 例:
let arr = ['南京','北京','东京','西京'];
arr.forEach(function(val, index, arr) {
console.log(index + ':' + val) // 1:南京 2:北京 3:东京 4:西京
});
arr.map(function(val,index,arr){...}), this指向的对象)
// ”映射”,正常情况下map()要有return,返回一个新的数组,没有return,map()和forEach()没区别。map()可以有效防止对原数据的变动。
// 例:
let arr = ['南京','北京','东京','西京'];
let newArr = arr.map(function(val, index, arr) {
let city =[];
city.push(val);
return city;
});
console.log(newArr ); // ['南京','北京','东京','西京'];
arr.filter(function(val,index,arr){...}), this指向的对象)
// 过滤,filter()也有返回值,但是返回的是Boolean值,如果是true,返回的数组就push,如果是false,返回的数组就pop。
// 例:
let arr = [{name: "南京", id:1},{name: "北京", id: 2},{name: "东京", id: 3}];
let newArr = arr.filter(function(item, index, arr) {
return item.id >= 2;
});
console.log(newArr); // {name: "北京",id: 2},{name: "东京",id: 3}
arr.some(function(val,index,arr){...}), this指向的对象) 和 arr.every(function(val, index, arr){...}), this指向的对象)
// 都类似于查找功能,some在查找到数组中某一项符合条件之后就返回true,every在数组所有项都符合条件之后才返回true。
// 例:
let arr = ['南京','北京','东京','西京'];
let a = arr.some((val, index, arr) => {
return value == "南京"; // true
});
console.log(a);
let b = arr.every((val,index,arr) => {
return value == "南京"; // false
// return val.charAt(1)=="京";
});
console.log(b);
arr.reduce(function(prev,cur,index,arr){...})
// reduce()方法接收一个函数作为累加器,数组中的每个值(从左到右)开始缩减,最终计算为一个值。常用于数组求和、阶乘,二维数组转一维数组,数组去重。
// 例:
let arr = [1,2,3,4,5,6,7,8,9,10]
let a = arr.reduce((prev, cur, index, arr) => {
return prev + cur;
});
console.log(a); // 55
arr.reduceRight(function(prev, cur, index, arr){...})
// reduceRight()和reduce()的区别是reduce是从左往右缩减,reduceRight是从右往左缩减,其他的一样。
// 例:
let arr = [2,2,3];
let a = arr.reduce((prev, cur, index, arr) => {
return prev ** cur;
});
console.log(a); // 81
for....of...
// for in遍历的是数组的索引(即键名),会将原型这一属性也遍历进去,而for of遍历的是数组元素值。For in适合于对象的遍历,for of 适合数组的遍历。
// 例:
let arr = ['南京','北京','东京','西京'];
for(let val of arr) {
console.log(val); // 值
}
for(let index of arr.keys()) { // arr.keys() 数组下标
console.log(index);
}
for(let item of arr.entries()) { // arr.entries() 数组某一项
console.log(item);
}
Array.from( )
// 作用:把类数组(获取一组元素、 arguments...)对象转成数组
// 例:
let aLi = document.queryselectorAll("ul li");
let arrLi = Array.from(aLi);
console.log(arrLi); //将li集合转换成数组
// PS:具备length条件的类数组才能用Array.from
// 例:
let json = {
0: "apple",
1: "banana",
2: "orange",
length:3
};
let json1 = {
0: "apple",
1: "banana",
2: "orange",
};
let arr= Array.from(json);
let arr1= Array.from(json1);
console.log(arr1); //[ ]
console.log(arr); //["apple","banana","orange"]
Array.of( )
// 作用:将一组值转为数组,类型于扩展运算符功能。
// 例:
let arr= Array.of("apple","banana", "orange");
console.log(arr);
arr.find()
// 作用:查找,找出第一个符合条件的数组成员,如果没有找到,返回undefined
// 例:
let arr = [23, 900, 101, 80, 100];
let res = arr.find((val, index, arr) => {
return val > 100;
});
console.log(res); // 900
arr.findIndex()
// 作用:查找,找出第一个符合条件的数组成员的位置,没找到返回-1
// 例:
let arr=[23, 900, 101, 80, 100];
let res = arr.findIndex((val, index, arr) => {
return val > 100;
});
console.log(res); // 1
arr.fill() 填充
arr.fill(填充的内容, 开始位置, 结束位置)
// 例:
let arr = new Array(10);
arr.fill('默认', 1, 5);
console.log(arr); // [empty, '默认', '默认', '默认', '默认', empty, empty, empty, empty, empty]
arr.include( ) // 包含
// 例:
let arr=[23, 900, 101, 80, 100];
console.log(arr.include(900)); // true
对象
1. Json对象的简写
// 例:
//原json写法
let name = "jack"
let age = 18
let json = {
name:name,
age: age
show:function() {
return this.name + this.age;
}
}
console.log(json.show())
//简写对比
let name = "jack"
let age = 18
let json = {
name,
age,
show(){
return this.name + this.age
}
}
console.log(json.show())
// PS:简写不要使用箭头函数
// 因为在使用箭头函数时,this就不是调用的那个对象,而是定义的对象,又因为json无法构成一个作用域,因此往上一级找到window,因此箭头函数中的this指向的是window,而不是json。
2. 绝对相等Object.is(arg1,arg2)
Object.is( )是将两个值进行绝对比较, 只要一样就返回true, 例如: console.log(Object.is(NaN,NaN));理论上是NaN!=NaN,但是Object.is( )返回true。又比如:console.log(Object.is(+0,-0));理论上是+0 = -0,但是Object.is( )返回false。因为Object.is( )要求两个值一模一样才返回true。
3. 合并对象Object.assign( )
let 新的对象=Object.assign(目标对象, source1, srouce2...) //将source1和source2合并成一个对象,放在目标对象中。
// 例:
let json = { a:1}
let json1 = { b:1,a:2}
let json2 = { c:1}
let newjson = Object.assign({ }, json, json1, json2);
console.log(newjson) // {a:2,b:1,c:1} 这里a=2的原因是后面的对象中a=2覆盖了之前的a=1.
常用举例:Object.assign( )常用在有默认数据的方法中,用户传进来的数据要覆盖方法中定义的默认数据。
function ajax(options) ( //用户传的数据
let defaults = { //默认数据
type: "get",
data: {}
...
};
Object.assign({}, defaults, options); //用户数据覆盖默认数据
}
// PS:也可以用于复制一个对象
4. object和array一样也有keys,value,entries方法,对象中的object.keys()是找到对象中某个属性,object.values()是找到属性的值,object.entries()是找到的是属性+值。
// 例:
let json={
A: 1,
B: 2,
C: 3
}
for(let key of object.keys()) {
console.log(key) // A,B,C
}
for(let value of object.values()) {
console.log(value ) // 1,2,3
}
for(let entries of object.entries()) {
console.log(entries ) // A=1,B=2,C=3
}
5. 解构不仅可以用在数组身上,也可以用在对象身上,只不过运算符要加在{ }里边。
// 例:
let {x, y, ...z} = {x:1, y:2, a:3, b:4};
console.log(x, y, z); // 1,2,{a:3,b:4}
// 例:
let json = {x:1, y:2, a:3, b:4};
let newjson = {...json}
console.log(newjson); // {x:1,y:2,a:3,b:4}