- 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(){} //function
Object.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.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.构造函数继承
function Person(name,age){
this.name=name
this.age=age
}
function Student(name,age,price){
Person.call(this,name,age)
this.price=price
}
//3.原型链加构造函数
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
}