Skip to content

Instantly share code, notes, and snippets.

@ahonn
Last active September 25, 2019 10:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahonn/914f7bf9067cd8e4198c94d4ac9a821c to your computer and use it in GitHub Desktop.
Save ahonn/914f7bf9067cd8e4198c94d4ac9a821c to your computer and use it in GitHub Desktop.
implement the `new` keyword
function myNew(constructor, ...args) {
const self = {};
self.__proto__ = constructor.prototype;
const obj = constructor.apply(self, args);
// 构造函数返回对象或者函数时,直接返回
if (typeof obj === 'object' || typeof obj === 'function') {
return obj;
}
return self;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment