new做了什么?
- 创建临时对象。
- 绑定原型。
- 执行构造函数。
- 返回临时对象。
动手来实现一下new
function mynew(Func, ...args){
// 创建临时对象
const obj = {}
// 绑定原型: 将临时对象的原型指向构造函数的原型
obj.__proto__ = Func.Prototype
// 将构造函数的this指向新对象
let result = Func.apply(obj, args)
// 对构造函数返回值做判断,然后返回对应的值
return result instanceof Object ? result : obj
}
JS的数据类型
-
数字 number
-
字符串 string
-
布尔 boolean
-
空 null undefined (约定:表示对象为空 null,表示非对象为空 undefined)
-
对象 object
-
大整数 bigint(JS的number默认是双精度浮点数,即假如存1,会默认以1.0保存 而且是64位,虽然保存的数字比较大,但是精度是有限的。)
-
符号 symbol