-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
59 additions
and
25 deletions.
There are no files selected for viewing
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
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
export function fromESClass(ESClass) { | ||
class ExtendedClass extends ESClass { | ||
static extend(proto) { | ||
const subclass = class extends ExtendedClass { | ||
constructor() { | ||
super(); | ||
if (proto && proto.init) { | ||
proto.init.apply(this, arguments); | ||
} | ||
} | ||
}; | ||
|
||
// Copy the prototype so that the constructor is not overwritten | ||
Object.assign(subclass.prototype, proto); | ||
|
||
addInstanceGetter(subclass.prototype); | ||
|
||
// Apply the prototype to fn to allow for chaining | ||
subclass.fn = subclass.prototype; | ||
|
||
return subclass; | ||
} | ||
} | ||
|
||
addInstanceGetter(ExtendedClass.prototype); | ||
|
||
// Apply the prototype to fn to allow for chaining | ||
ExtendedClass.fn = ExtendedClass.prototype; | ||
|
||
return ExtendedClass; | ||
} | ||
|
||
function addInstanceGetter(proto) { | ||
Object.defineProperty(proto, '_instance', { | ||
get: function() { | ||
return this; | ||
} | ||
}); | ||
} |