From 2ac3447383cb2083f758b91ff4566e75b32cd4b7 Mon Sep 17 00:00:00 2001 From: Neil Fraser Date: Sat, 13 Jul 2019 00:15:55 -0700 Subject: [PATCH] Create null objects properly. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit new Object(null) doesn’t do anything, that’s just broken. And the for loop with keys is silly. --- core/blocks.js | 2 +- core/variable_map.js | 9 ++++----- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/core/blocks.js b/core/blocks.js index c27be1d40..f3ad06826 100644 --- a/core/blocks.js +++ b/core/blocks.js @@ -34,4 +34,4 @@ goog.provide('Blockly.Blocks'); * A mapping of block type names to block prototype objects. * @type {!Object.} */ -Blockly.Blocks = new Object(null); +Blockly.Blocks = Object.create(null); diff --git a/core/variable_map.js b/core/variable_map.js index 670191e1a..c6f27d26e 100644 --- a/core/variable_map.js +++ b/core/variable_map.js @@ -48,7 +48,7 @@ Blockly.VariableMap = function(workspace) { * @type {!Object.>} * @private */ - this.variableMap_ = {}; + this.variableMap_ = Object.create(null); /** * The workspace this map belongs to. @@ -61,7 +61,7 @@ Blockly.VariableMap = function(workspace) { * Clear the variable map. */ Blockly.VariableMap.prototype.clear = function() { - this.variableMap_ = new Object(null); + this.variableMap_ = Object.create(null); }; /* Begin functions for renaming variables. */ @@ -380,9 +380,8 @@ Blockly.VariableMap.prototype.getVariableTypes = function(ws) { */ Blockly.VariableMap.prototype.getAllVariables = function() { var all_variables = []; - var keys = Object.keys(this.variableMap_); - for (var i = 0; i < keys.length; i++ ) { - all_variables = all_variables.concat(this.variableMap_[keys[i]]); + for (var key in this.variableMap_) { + all_variables = all_variables.concat(this.variableMap_[key]); } return all_variables; };