- 创建一个新对象。
- 这个新对象会被执行[[原型]]连接。
- 将构造函数的作用域赋值给新对象,即this指向这个新对象.
- 如果函数没有返回其他对象,那么new表达式中的函数调用会自动返回这个新对象。
function new(func) { lat target = {}; target.__proto__ = func.prototype; let res = func.call(target); if (typeof(res) == "object" || typeof(res) == "function") { return res; } return target; }
通过new方式创建对象
最新推荐文章于 2024-08-11 20:44:12 发布