-
Notifications
You must be signed in to change notification settings - Fork 30k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
util: add createClassWrapper to internal/util
Utility function for wrapping an ES6 class with a constructor function that does not require the new keyword PR-URL: #11391 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Sam Roberts <vieuxtech@gmail.com>
- Loading branch information
Showing
2 changed files
with
51 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Flags: --expose-internals | ||
'use strict'; | ||
|
||
require('../common'); | ||
const assert = require('assert'); | ||
const util = require('internal/util'); | ||
|
||
const createClassWrapper = util.createClassWrapper; | ||
|
||
class A { | ||
constructor(a, b, c) { | ||
this.a = a; | ||
this.b = b; | ||
this.c = c; | ||
} | ||
} | ||
|
||
const B = createClassWrapper(A); | ||
|
||
assert.strictEqual(typeof B, 'function'); | ||
assert(B(1, 2, 3) instanceof B); | ||
assert(B(1, 2, 3) instanceof A); | ||
assert(new B(1, 2, 3) instanceof B); | ||
assert(new B(1, 2, 3) instanceof A); | ||
assert.strictEqual(B.name, A.name); | ||
assert.strictEqual(B.length, A.length); | ||
|
||
const b = new B(1, 2, 3); | ||
assert.strictEqual(b.a, 1); | ||
assert.strictEqual(b.b, 2); | ||
assert.strictEqual(b.c, 3); |