From 605ec3c0e24380d708c506b6901bff87a070a5c7 Mon Sep 17 00:00:00 2001 From: GoToLoop Date: Sat, 14 Nov 2015 14:57:30 -0200 Subject: [PATCH] Set non-enumerable for Object.prototype.clone() "pacman.js" can't mix up w/ other scripts which rely on [`for...in`](https://developer.Mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in) or [**Object.keys()**](https://developer.Mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys) but don't check for [**hasOwnProperty()**](https://developer.Mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty). Any inclusions for built-in native objects should be accompanied by [**Object.defineProperty()**](https://developer.Mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty) and have its new method set to `{ enumerable: false }`! --- pacman.js | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/pacman.js b/pacman.js index fd0dfd6..49da957 100644 --- a/pacman.js +++ b/pacman.js @@ -1254,16 +1254,13 @@ Pacman.WALLS = [ ]; Object.prototype.clone = function () { - var i, newObj = (this instanceof Array) ? [] : {}; - for (i in this) { - if (i === 'clone') { - continue; - } - if (this[i] && typeof this[i] === "object") { - newObj[i] = this[i].clone(); - } else { - newObj[i] = this[i]; - } - } - return newObj; + const newObj = this instanceof Array? [] : {}; + var i, v; + + for (i in this) if (i !== 'clone') + v = this[i], newObj[i] = v && typeof v === 'object'? v.clone() : v; + + return newObj; }; + +Object.defineProperty(Object.prototype, 'clone', { enumerable: false });