Skip to content

Commit 3e4cfef

Browse files
Better method checking
1 parent 1ec88dd commit 3e4cfef

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

modules/registry.js

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,7 @@ class Registry {
176176
cloned = object;
177177
console.warn("Could not clone object:", error);
178178
}
179-
checkForClashingFunctions(cloned, instantiated);
180-
instantiated = Object.assign(instantiated, cloned);
179+
assign(instantiated, cloned);
181180
instantiated.init ? instantiated.init() : {}; //Initialise if possible.
182181
return instantiated;
183182
}
@@ -202,16 +201,27 @@ class Registry {
202201

203202
let hasNonAscii = (str) => [...str].some((char) => char.charCodeAt(0) > 127);
204203

205-
function checkForClashingFunctions(source, target) {
206-
for (let prop in source) {
207-
if (prop in target) {
208-
if (typeof target[prop] === "function") {
209-
throw new SyntaxError(
210-
"Property '" + prop + "' clashes with a class method!"
211-
);
212-
}
204+
/**A version of `Object.assign()` which only copies keys present on both objects, and will not allow functions to be overridden.\
205+
* Mutates the original object, and returns it.
206+
*/
207+
function assign(target, source) {
208+
for (let key of Object.getOwnPropertyNames(source)) {
209+
let value = source[key];
210+
let replace = target[key];
211+
if (replace !== undefined) {
212+
if (typeof replace !== "function") {
213+
target[key] = value;
214+
} else console.warn("Cannot replace an object's method: " + key);
215+
} else {
216+
console.warn(
217+
"Cannot create properties using `construct()`-derived functions: " +
218+
key +
219+
" is not present on type " +
220+
target.constructor.name
221+
);
213222
}
214223
}
224+
return target;
215225
}
216226

217227
export { Registry };

0 commit comments

Comments
 (0)