class
字符串扩展
数值的扩展
数组的扩展
对象扩展
日记本
相关推荐
运营公众号一周了,这是我公众号一天的收入。
阅读 16631
住在不隔音的宾馆是什么体验
阅读 45091
舔狗 舔到最后应有尽有
阅读 28352
疫情下的思考—副业
阅读 1612
你说我渣吗?
阅读 75480es6(class,字符串扩展,数值扩展,数组扩展,对象扩展)
class
// function Person(name,age){
// this.name = name;
// this.age = age;
// }
// let person = new Person('kobe',40);
// console.log(person);
class Person{
//类的构造方法 相当于Python的__init__
constructor(name,age){
this.name = name;
this.age = age;
}
//类的一般方法
showName(){
console.log('调用父类的方法');
console.log(this.name,this.age);
}
}
//子类 Python写法:class StarPerson(Person):
class StarPerson extends Person{
constructor(name,age,salary){
super(name,age);//调用父类的构造方法
this.salary = salary;
}
//父类方法的重写
showName(){
console.log('调用子类的方法');
console.log(this.name,this.age,this.salary);
}
}
let person = new Person('kobe',40);
console.log(person);
// person.showName();
let sp = new StarPerson('wade',36,1243254360);
console.log(sp);
sp.showName();
字符串扩展
let str = 'adfasdawohenfiinwegk';//true
console.log(str.includes('a'));//true
console.log(str.startsWith('a'));//true
console.log(str.endsWith('k'));//true
console.log(str.repeat(3));//重复三次
数值的扩展
console.log(0b1010);//10
console.log(0o56);//46
console.log(Number.isFinite(123));//true
console.log(Number.isFinite(Infinity));//false
console.log(Number.isNaN(NaN));//true
console.log(Number.isInteger(123));//true
console.log(Number.isInteger(123.456));//false
console.log(Number.isInteger(123.0));//true
console.log(Number.parseInt('123abc456'));//123
console.log(Number.parseInt('a123abc456'));//NaN
console.log(Math.trunc(123.123));//123
数组的扩展
let btns = document.getElementsByTagName('button');
Array.from(btns).forEach(function(item,index){
console.log(item);
})
let arr = Array.of(1,4,'abc',true);
console.log(arr);//[1,4,'abc',true]
let arr2 = [2,3,4,2,5,7,3,6,5];
let result = arr2.find(function(item,index){
return item > 4;
})
console.log(result);//5
result = arr2.findIndex(function(item,index){
return item > 4;
})
console.log(result);//4(5的下标)
对象扩展
console.log(0 == -0);//true
console.log(NaN == NaN);//false
console.log(Object.is(0,-0));//false
console.log(Object.is(NaN,NaN));//true
let obj = {};
let obj1 = {username:'iverson',age:43};
let obj2 = {sex:'男'};
Object.assign(obj,obj1,obj2);
console.log(obj);//{username:'iverson',age:43,sex:'男'};
let obj3 = {};
let obj4 = {money: 5050000};
boj3.__proto__= obj4;
console.log(obj3)
console.log(obj3.money);