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
(1)首先创建了一个新的空对象 (2)设置原型,将对象的原型设置为函数的 prototype 对象。 (3)让函数的 this 指向这个对象,执行构造函数的代码(为这个新对象添加属性) (4)判断函数的返回值类型,如果是值类型,返回创建的对象。如果是引用类型,就返回这个引用类型的对象。
function myNew(constructorFn, ...args) { let newObj = {} newObj.__proto__ = constructorFn.prototype; // newObj = Object.create(constructor.prototype); let result = constructorFn.apply(newObj, args) return result instanceof Object ? result : newObj } function Animal(name) { this.name = name; } let animal = myNew(Animal, 'dog') console.log(animal);
The text was updated successfully, but these errors were encountered:
function myNew(constructor, ...args) { const obj = Object.create(constructor.prototype); const result = constructor.apply(obj, args); return result instanceof Object ? result : obj; }
Sorry, something went wrong.
function myNew(){ let obj = {}; let con = [].shift.call(arguments); obj.__proto__ = con.prototype; let res = con.call(obj, ...arguments); return res instanceof Obejct ? res : obj; }
No branches or pull requests
(1)首先创建了一个新的空对象
(2)设置原型,将对象的原型设置为函数的 prototype 对象。
(3)让函数的 this 指向这个对象,执行构造函数的代码(为这个新对象添加属性)
(4)判断函数的返回值类型,如果是值类型,返回创建的对象。如果是引用类型,就返回这个引用类型的对象。
The text was updated successfully, but these errors were encountered: