- 1.js的數(shù)據(jù)類型
boolean number string null undefined bigint symbol object
按存儲方式分佩抹,前面七種為基本數(shù)據(jù)類型婚夫,存儲在棧上,object是引用數(shù)據(jù)類型,存儲在堆上艾君,在棧中存儲指針
按es標(biāo)準(zhǔn)分缰猴,bigint 和symbol是es6新增的數(shù)據(jù)類型氛琢,bigint存儲大整數(shù)只嚣,symbol解決全局屬性名沖突的問題
- 2.js數(shù)據(jù)類型檢測的方式
typeof 2 //number
typeof true //boolean
typeof 'srt' //string
typeof undefined //undefined
typeof null //object
typeof 1n //bigint
typeof Symbol() //symbol
typeof {} //object
typeof [] //object
typeof function(){} //function
Object.prototype.toString().call()
([] instanceof Array)
(function(){} instanceof Function)
({} instanceof Object)
//instanceof只能用于對象,返回布爾值
(2).constructor===Number//true
(true).constructor===Boolean//true
- 3.判斷數(shù)組類型的方式有那些
//1.通過原型判斷
const a=[]
const b={}
a instanceof Array
Array.prototype.isPrototypeOf(a)
a.__proto__===Array.prototype
//2.通過object.prototype.tostring.call()
const a=[]
Object.prototype.toString().call(a)
//3.es6的array.isarray()
Array.isArray(a)
- 4.null和undefined的區(qū)別
undefinde代表為定義,變量聲明了但未初始化是未定義
null代表空對象艺沼,一般用作某些對象變量的初始化值
undefined==void 0
typeof null=object null的地址是0和對象的地址相同
- 0.1+0.2册舞!==0.3
// 方法一:放大10倍之后相加在縮小十倍
//方法二:封裝浮點數(shù)相等的函數(shù)
function feg(a,b){
return Math.abs(a-b)<Number.EPSILON
}
feg(0.1+0.2,0.3)
- 6.空類型
[]==false//true
Boolean([])//true
Number([])//0
- 7.包裝類型
const a='abc'
a.length//3
a.toUpperCase()//'ABC'
const c=Object('abc')
const cc=Object('abc').valueOf()
- 8.new做了什么工作
1.創(chuàng)建了一個新的空對象object
2.將新空對象與構(gòu)造函數(shù)通過原型鏈連接起來
3.將構(gòu)造函數(shù)中的this綁定到新建的object上并設(shè)置為新建對象result
4.返回類型判斷
function MyNew(fn,...args){
const obj={}
obj.__proto__=fn.prototype
let result=fn.apply(obj,args)
return result instanceof Object?result:obj
}
function Person(name,age){
this.name=name
this.age=age
}
Person.prototype.sayHello=function(){
console.log(this.name)
}
const person=MyNew(Person,'test',20)
person.sayHello()
- 9.繼承
//1.原型鏈繼承
function Sup(){
this.prop='sup'
}
Sup.prototype.show=function(){}
function Sub(){
this.prop='sub'
}
Sub.prototype=new Sup()
Sub.prototype.constructor=Sub
Sub.prototype.show=function(){}
//2.構(gòu)造函數(shù)繼承
function Person(name,age){
this.name=name
this.age=age
}
function Student(name,age,price){
Person.call(this,name,age)
this.price=price
}
//3.原型鏈加構(gòu)造函數(shù)
function Person(name,age){
this.name=name
this.age=age
}
Person.prototype.show=function(){}
function Student(name,age,price){
Person.call(this,name,age)
this.price=price
}
Student.prototype=new Person()
Student.prototype.constructor=Person
Student.prototype.show=function(){}
//4.class extends
class Animal{
constructor(kind){
this.kind=kind
}
}
class Cat extends Animal{
constructor(kind) {
super.constructor(kind);
}
}
- 10.深拷貝
function deep(p,c){
let c = c||{}
for(let i in p){
if(typeof p[i]==='object'){
c[i]=p[i].constructor=='Array'?[]:{}
deep(p[i],c[i])
}else{
c[i]=p[i]
}
}
return c
}