es6的类
01-判断数据类型的方法
# typeof: 只能用来判断基本数据类型
typeof '123' 'string'
typeof 123 'number'
typeof null 'object'
typeof [1,2,3] 'object'
typeof {uname: '小妲己'} 'object'
typeof function() {} 'function'
# constructor构造器
[1,2,3].construtor
{}.constructor
'小妲己'.construtor
# instanceof:判断是否是构造函数的实例对象
[1,2,3] instanceof Array
if (arr1 instanceof Array) {
console.log('数组');
} else if (arr1 instanceof Function) {
console.log('函数');
} else {
console.log('对象');
}
得到:::[1,2,3] instanceof Array ? [] : {}
# Object.prototype.toString.call()
Object.prototype.toString.call([1,2,3])
Object.prototype.toString.call({})
Object.prototype.toString.call('小妲己')
Object.prototype.toString.call(undefined)
function getType(data) {
return Object.prototype.toString.call(data)
}
02-es6的类
`类`: 批量创建具有相同属性和方法的实例对象
`语法`:
class Person {
属性 = 值
constructor(形参) {
this.属性 = 值
}
方法名() {}
static 方法名() {}
}
`调用`: let p1 = new Person(实参)