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
JS构造函数也是普通函数,区别就是使用new操作符调用函数,就可以称为构造函数。通过new操作符调用函数可以创建一个新的对象。比如:
function Person(name) { this.name = name; } let p = new Person('bobobo'); console.log(typeof p); // 'object' console.log(p.name); // 'bobobo'
之前学习this的指向就知道了使用new调用函数,那么this指向的是创建出来的新对象。那么new操作符调用的过程经历的步骤:
模拟实现new操作符
// 实现 function create(Con, ...args) { let obj = {} // setPrototypeOf设置一个对象的 prototype 对象 Object.setPrototypeOf(obj, Con.prototype) let result = Con.apply(obj, args) return result instanceof Object ? result : obj } // 用法 function Person(name) { this.name = name } const p = create(Person, 'bobo'); /** * 实现来源于作者:yck * 链接:https://juejin.im/post/5c7b963ae51d453eb173896e */
The text was updated successfully, but these errors were encountered:
lfb
No branches or pull requests
构造函数
JS构造函数也是普通函数,区别就是使用new操作符调用函数,就可以称为构造函数。通过new操作符调用函数可以创建一个新的对象。比如:
new操作符
之前学习this的指向就知道了使用new调用函数,那么this指向的是创建出来的新对象。那么new操作符调用的过程经历的步骤:
模拟实现new操作符
The text was updated successfully, but these errors were encountered: