We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
function myNew(fn, ...args) { // 判断传递的参数是否合法 if (typeof fn !== 'function') { throw new Error('传递的参数不能被new'); } // 创建一个空对象,并且将该对象的__proto__属性指向fn.prototype const obj = Object.create(fn.prototype) // 执行函数体,并将this指向创建出来的对象 const fnResult = fn.apply(obj, args) // 判断函数本身返回值是否为函数或者对象类型 if (typeof fnResult === 'object' || typeof fnResult === 'function') { return fnResult } return obj }
The text was updated successfully, but these errors were encountered:
No branches or pull requests
new操作时做了什么?
代码实现
The text was updated successfully, but these errors were encountered: