- 1.js的數據類型
boolean number string null undefined bigint symbol object
按存儲方式分,前面七種為基本數據類型,存儲在棧上,object是引用數據類型,存儲在堆上,在棧中存儲指針
按es標準分,bigint 和symbol是es6新增的數據類型,bigint存儲大整數,symbol解決全局屬性名沖突的問題
- 2.js數據類型檢測的方式
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(){} //functionObject.prototype.toString().call()([] instanceof Array)
(function(){} instanceof Function)
({} instanceof Object)
//instanceof只能用于對象,返回布爾值(2).constructor===Number//true(true).constructor===Boolean//true
- 3.判斷數組類型的方式有那些
//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的區別
undefinde代表為定義,變量聲明了但未初始化是未定義
null代表空對象,一般用作某些對象變量的初始化值
undefined==void 0
typeof null=object null的地址是0和對象的地址相同
-
- 0.1+0.2!==0.3
// 方法一:放大10倍之后相加在縮小十倍
//方法二:封裝浮點數相等的函數
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.創建了一個新的空對象object
2.將新空對象與構造函數通過原型鏈連接起來
3.將構造函數中的this綁定到新建的object上并設置為新建對象result
4.返回類型判斷
function MyNew(fn,...args){const obj={}obj.__proto__=fn.prototypelet result=fn.apply(obj,args)return result instanceof Object?result:obj
}
function Person(name,age){this.name=namethis.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=SubSub.prototype.show=function(){}
//2.構造函數繼承function Person(name,age){this.name=namethis.age=age}function Student(name,age,price){Person.call(this,name,age)this.price=price}
//3.原型鏈加構造函數function Person(name,age){this.name=namethis.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=PersonStudent.prototype.show=function(){}//4.class extendsclass 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}