对象新增方法
var obj = {a:1,b:2};
for(var attr of Object.keys(obj)){
console.log(attr); //返回keys
}
for(var attr of Object.values(obj)){
console.log(attr); //返回values
}
for(var attr of Object.entries(obj)){
console.log(attr); //返回二维数组 ["a", 1] ["b", 2]
}
//解构
for(var [k,v] of Object.entries(obj)){
console.log(k,v); //返回二维数组 ["a", 1] ["b", 2]
}
数组去重
var arr = [1,12,3,5,1,2,7,12];
var arr2 = [...new Set(arr)]; //打散去重
console.log(arr2);
ES6 新构造函数方法
class Person{
constructor(name,age){
this.name = name;
this.age = age;
}
say(){
console.log('say....');
}
eat(){
console.log('eat....');
}
static isCon(obj){ //静态方法,通过构造函数调用的方法,不用NEW
//console.log('静态方法');
return obj instanceof Person; //判断是否为构造函数构造的
}
}
两部完成构造函数继承
class Coder extends Person{ //继承-1
constructor(name,age,money){
super(name,age); //调用父级函数,记得带参数,完成继承-2
this.money = money
}
say(){
console.log('say2....'); //在子类上可以直接对方法进行修改
}
}
var P1 = new Person('AA',22);
console.log(P1);
P1.say();
Person.isCon(P1); //true
网友评论