原型
- 每一个函数都会有一个原型属性 prototype;
- 通过 new + 「函数」 的方式会创建一个对象,这个函数被称为构造函数,浏览器会给被创建的对象添加一个属性 proto,这个属性指向构造函数的 prototype;
- 通过 proto 属性可以实现 JavaScript 中的继承,不过在 ES6 中可以通过关键字 class 定义类来实现继承。
this
this 代表函数运行时的环境。注意是函数,是函数,还有就是运行时!
function hello(){
console.log(this) // window
}
hello()
// hello() 实在window中调用的,this的运行环境就是window。
运算
== === 区别
== 值相等,=== 值和类型相等
赋值时的|| 和&& 特性
js中 null,undefined,0,'' 在逻辑运算中都为false,其它为true
var result = a || b
// a 为 true,result都取a的值.否则取b的
var result = a && b
// a 为true,取b,反之亦然。
数组
数组深度拷贝
最近简单方式:转化为JSON字符串,再格式化回来
join
使用指定分隔符,连接两个或多个数组的元素,返回一个字符串。
let arr =['a','b','c']
arr.join(',')
//输出 a,b,c
slice
用于提取数组中一个片段,作为新数组返回。
slice(start[,end]): 接收2个参数
splice
从数组中删除指定索引开始的项目,然后返回被删除的项目
arr.splice(index,num,a1,a2,...,an);
a1,a2,...an:可选,为数组添加的元素;
sort
对数组的元素进行排序,改变原数组。
let a1= [1,3,2,6,57]
a1.sort() //输出2,3,5,7]
a1.sort(function(a,b){
return a>b ? 1 : a<b? -1:0;
})//输出2,3,5,7]
some
检测数组元素中是否有元素符合指定条件。
var ages = [3, 10, 18, 20];
function checkAdult(age) {
return age >= 18;
}
ages.some(checkAdult)
forEach()
用于调用数组的每个元素,并将元素传递给回调函数
function myFunction(item, index) {
demoP.innerHTML = "index[" + index + "]: " + item + "<br>";
}
numbers.forEach(myFunction)
其他操作符
Set
数组去重
Array.from(new Set([1,2,2,3,4,4]))
Map
Map 转 Obj
function fun(s){
let obj = Object.create(null)
for (let[k,v] of s){
obj[k] = v
}
return obj
}
const a = new Map().set('yes',true).set('no',false)
fun(a)
// 输出 {yes:true, no:false}
Obj转Map
function fun(obj){
let a = new Map()
for (let k of Object.key(obj)){
a.set(k,obj[k])
}
return a
}
fun({yes:true, no:false})