逻辑与操作符运算规则
逻辑与操作可以应用于任何类型的操作数,而不仅仅是布尔值。在有一个操作数不是布尔值的情况下,逻辑与操作符就不一定返回布尔值,其规则如下:
- 如果第一个操作数是对象,则返回第二个操作数
- 如果第二个操作数是对象,则只有在第一个操作数的求值结果为true的情况下才会返回该对象
- 如果第两个操作数都是对象,则返回第二个操作数
- 如果第一个操作数是null,则返回null
- 如果第一个操作数是NaN,则返回NaN
- 如果第一个操作数是undefined,则返回undefined
with语句
with语句的作用是将代码的作用域设置到一个特定的对象中。
var qs = location.search.substring(1);
var hostName = location.hostname;
var url = location.href;
with(location) {
var qs = search.substring(1);
var hostName = hostname;
var url = href;
}
严格模式下使用with语句会报语法错误。大量使用with语句会使性能下降。
数组常用方法
- push()和pop()
push()方法可以向原数组末尾添加一项或者多项,返回值为数组的长度,例如:
let arr = ['one', 'two', 'three', 'four'];
let newArr = arr.push('five', 'six');
console.log(arr); // ["one", "two", "three", "four", "five", "six"]
console.log(newArr); // 6
pop()方法删除原数组的最后一项,放回删除项的内容,例如:
let arr = ['one', 'two', 'three', 'four'];
let newArr = arr.pop();
console.log(arr); // ["one", "two", "three"]
console.log(newArr); // four
- shift()和unshift()
shift()删除数组的第一项,返回删除项的内容,例如:
let arr = ['one', 'two', 'three', 'four'];
let newArr = arr.shift();
console.log(arr); // ["two", "three", "four"]
console.log(newArr); // one
unshift()在数组前端添加任意项并返回数组的长度,例如:
let arr = ['one', 'two', 'three', 'four'];
let newArr = arr.unshift('zero', 'zero1');
console.log(arr); // ["zero", "zero1", "one", "two", "three", "four"]
console.log(newArr); // 6
- reverse()和sort()
reverse()将数组顺序反转,返回原数组,例如:
let arr = ['one', 'two', 'three', 'four'];
let newArr = arr.reverse();
console.log(arr); // ["four", "three", "two", "one"]
console.log(newArr); // ["four", "three", "two", "one"]
sort()将数组排序,如果不传参数,将默认升序排序,为了实现排序,sort()方法会调用每个数组项的toString()方法将其转化成字符串,从而比较字符串进行排序,这种情况并不常用。sort()方法可以接收一个函数,相当于指定其排序方式,例如升序排序:
let arr = [1, 5, 15, 10];
let newArr = arr.sort((value1, value2) => {
if (value1 < value2) {
return -1;
} else if (value1 > value2) {
return 1;
} else {
return 0;
}
});
console.log(arr); // [1, 5, 10, 15]
降序排序:
let arr = [1, 5, 15, 10];
let newArr = arr.sort((value1, value2) => {
if (value1 < value2) {
return 1;
} else if (value1 > value2) {
return -1;
} else {
return 0;
}
});
console.log(arr); // [15, 10, 5, 1]
- concat()和slice()和splice()
concat()方法可以将数组拼接,它可以接收任意类型的数据,包括数组,返回一个新的数组,原数组不变,例如:
let arr = ['one', 'two', 'three', 'four'];
let newArr = arr.concat('five', ['six', 'seven']);
console.log(arr); // ["one", "two", "three", "four"]
console.log(newArr); // ["one", "two", "three", "four", "five", "six", "seven"]
slice()是截取数组的方法,它接收一个或两个参数,当传入一个参数时表示截取从当前位置开始到末位置,当传入两个参数时表示截取这个区间的长度,无论传入几个参数,都会返回一个新的数组,原数组不变,例如:
// 传入一个参数
let arr = ['one', 'two', 'three', 'four'];
let newArr = arr.slice(1);
console.log(arr); // ["one", "two", "three", "four"] 原数组不变
console.log(newArr); // ["two", "three", "four"] 返回的新数组
// 传入两个参数
let arr = ['one', 'two', 'three', 'four'];
let newArr = arr.slice(1, 3); // 截取第一项,第二项,不包含第三项,左闭右开
console.log(arr); // ["one", "two", "three", "four"] 原数组不变
console.log(newArr); // ["two", "three"] 返回的新数组
splice()方法比较强大,可以对数组进行删除,插入,替换。它可以接收3个及以上参数,第一个参数时起始位置,第二个参数为删除的项数,其余参数为插入的内容,以数组形式返回删除的内容,例如删除:
let arr = ['one', 'two', 'three', 'four'];
let newArr = arr.splice(1, 2);
console.log(arr); // ["one", "four"]
console.log(newArr); // ["two", "three"]
插入
let arr = ['one', 'two', 'three', 'four'];
let newArr = arr.splice(1, 0, 'add');
console.log(arr); // ["one", "add", "two", "three", "four"]
console.log(newArr); // []
替换
let arr = ['one', 'two', 'three', 'four'];
letnewArr = arr.splice(2, 1, 'add'); // 将第三项替换为add
console.log(arr); // ["one", "two", "add", "four"]
console.log(newArr); // ["three"]
- indexOf()和lastIndexOf()
indexOf()接收两个参数,第二个参数可选,第一个参数为查找的内容,第二个为查找的起始位置,要是能在数组中查到,则返回该内容的位置,若查不到则返回-1,例如:
let arr = [1, 2, 3, 4, 5, 4, 3, 2, 1];
console.log(arr.indexOf(4)); // 3
console.log(arr.lastIndexOf(4)); // 5
console.log(arr.indexOf(4, 4)); // 5
console.log(arr.lastIndexOf(4, 4)); // 5
console.log(arr.indexOf(6)); // -1
- forEach()和map()
forEach()和map()都是遍历数组的每一项,它们都接收一个函数作为参数,函数接收3个可选参数,第一个为数组项,第二个为数组位置角标,第三个为数组本身,它们的区别是前者没有返回值,后者将函数的返回结果以数组形式返回,例如:
let arr = [1, 2, 3, 4, 5, 4, 3, 2, 1];
letcount = 0;
let forEachResult = arr.forEach(function(item, index, array) {
return item;
});
let mapResult = arr.map(function(item, index, array) {
return item;
});
console.log(forEachResult); // undefined
console.log(mapResult); // [1, 2, 3, 4, 5, 4, 3, 2, 1]
- reduce()
reduce()也是接收一个函数作为参数,该函数接收4个参数,第一个参数为该函数上一次执行的返回值,第二个参数为当前数组项的值,第三个参数为数组的索引,第四个为数组对象本身。
let arr = [1, 2, 3, 4, 5];
let sum = arr.reduce((pre, cur, index, array) => {
return pre + cur;
});
console.log(sum); // 15