Skip to content

Commit 6e5d5c2

Browse files
committed
dev: clone property descriptors in Context
1 parent c2a475f commit 6e5d5c2

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

src/putility/src/libs/context.js

+17-4
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,20 @@
1818
*/
1919

2020
class Context {
21-
constructor (values) {
22-
for ( const k in values ) this[k] = values[k];
21+
constructor (values = {}) {
22+
const descs = Object.getOwnPropertyDescriptors(values);
23+
for ( const k in descs ) {
24+
Object.defineProperty(this, k, descs[k]);
25+
}
26+
}
27+
follow (source, keys) {
28+
const values = {};
29+
for ( const k of keys ) {
30+
Object.defineProperty(values, k, {
31+
get: () => source[k]
32+
});
33+
}
34+
return this.sub(values);
2335
}
2436
sub (newValues) {
2537
if ( newValues === undefined ) newValues = {};
@@ -36,9 +48,10 @@ class Context {
3648
}
3749
}
3850

39-
for ( const k in newValues ) {
51+
const descs = Object.getOwnPropertyDescriptors(newValues);
52+
for ( const k in descs ){
4053
if ( alreadyApplied[k] ) continue;
41-
sub[k] = newValues[k];
54+
Object.defineProperty(sub, k, descs[k]);
4255
}
4356

4457
return sub;

0 commit comments

Comments
 (0)