From ef9153fc52b6cea0744b2239921c5dcae4697f11 Mon Sep 17 00:00:00 2001 From: substack Date: Mon, 21 Mar 2022 16:45:18 -1000 Subject: [PATCH] isConstructorOrProto adapted from PR --- index.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 75662ee..e836daf 100644 --- a/index.js +++ b/index.js @@ -6,6 +6,10 @@ function isNumber(x) { return (/^[-+]?(?:\d+(?:\.\d*)?|\.\d+)(e[-+]?\d+)?$/).test(x); } +function isConstructorOrProto(obj, key) { + return key === 'constructor' && (typeof obj[key] === 'function' || key === '__proto__'); +} + function hasKey(obj, keys) { var o = obj; keys.slice(0, -1).forEach(function (key) { @@ -21,7 +25,9 @@ function setKey(obj, keys, value) { var key; for (var i = 0; i < keys.length - 1; i++) { key = keys[i]; - if (key === '__proto__') { return; } + if (key === '__proto__' || isConstructorOrProto(o, key)) { + return; + } if (o[key] === undefined) { o[key] = {}; } if ( o[key] === Object.prototype