Last active
September 25, 2019 10:09
-
-
Save ahonn/914f7bf9067cd8e4198c94d4ac9a821c to your computer and use it in GitHub Desktop.
implement the `new` keyword
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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