简体   繁体   English

JavaScript保留字和对象

[英]Javascript reserved word and object

I'm making a dictionary of words, so there are 1,000,000+ words. 我正在制作单词词典,所以有1,000,000多个单词。 The problem comes when I need to store the word constructor . 问题出在我需要存储constructor一词时。 I know this is a reserved word in javascript, but I need to add it to the dictionary. 我知道这是javascript中的保留字,但我需要将其添加到字典中。

var dictionary = {} 
console.log(dictionary ['word_1']) 
//undefined, this is good
console.log(dictionary ['word_2']) 
//undefined, this is good
console.log(dictionary ['constructor']) 
//[Function: Object] 
// this cause initialization code to break

How can I fix this? 我怎样才能解决这个问题? I could muck with the it like key=key+"_" but that seems bad. 我可以像key=key+"_"那样糟透了,但这似乎很糟糕。 Is there anything else I can do? 我还能做些什么吗?

Instead of using a JS object, you could use the built-in Map type which uses strings/symbols as keys and does not conflict with any existing properties. 除了使用JS对象,您还可以使用内置的Map类型,该类型使用字符串/符号作为键,并且不与任何现有属性冲突。

Replace var dictionary = {} with var dictionary = new Map() var dictionary = {}替换为var dictionary = new Map()

Override the constructor key as undefined 覆盖undefinedconstructor

According to the MDN Object.prototype page , the only thing that isn't hidden by the __fieldname__ schema is the "constructor field". 根据MDN Object.prototype页面 ,唯一未被__fieldname__模式隐藏的是“构造器字段”。 Thus, you could just initialize your objects via { 'constructor': undefined } . 因此,您可以通过{ 'constructor': undefined }初始化对象。

However, you would have to make sure that in your for .. in statements would filter out all keys with undefined as their value, as it would pick up constructor as a "valid" key (even though it wouldn't before you specifically set it to undefined). 但是,您必须确保在for .. in语句中将所有undefined键作为其值过滤掉,因为它将选择constructor作为“有效”键(即使在您专门设置之前也不会这样做)它为未定义)。 IE IE

for(var key in obj) if(obj[key] !== undefined) { /* do things */ }

Check for types when getting/setting 在获取/设置时检查类型

Otherwise, you could just check the type when you 'fetch' or 'store' it. 否则,您可以仅在“获取”或“存储”它时检查类型。 IE IE

function get(obj, key) {
  if(typeof obj[key] !== 'function') // optionally, `&& typeof obj[key] !== 'object')`
    return obj[key];
  else
    return undefined;
}

I think you should store all words and translation of them in an array. 我认为您应该将所有单词及其翻译存储在一个数组中。 When you need to translate a word, you can use find method of Array. 当您需要翻译单词时,可以使用Array的find方法。

For example: 例如:

var dict = [
    { word: "abc", translated: "xyz" },
    ...
];

Then: 然后:

var searching_word = "abc";
var translation = dict.find(function (item) {
    return item.word == searching_word;
});
console.log(translation.translated);
// --> xyz

To achieve expected result , use below option of using index to get value of any key value 为了达到预期的效果,请使用以下使用索引的选项来获取任何键值的值

var dictionary = {};

var dictionary1 = {
  constructor: "test"
};

//simple function to get key value using index
function getVal(obj, val) {
  var keys = Object.keys(obj);
  var index = keys.indexOf(val);//get index of key, in our case -contructor
  return obj[keys[index]]; // return value using indec of that key
}

console.log(getVal(dictionary, "constructor"));//undefined as expected
console.log(getVal(dictionary1, "constructor"));//test

console.log(dictionary["word_1"]);
//undefined, this is good
console.log(dictionary["word_2"]);
//undefined, this is good

codepen - https://codepen.io/nagasai/pen/LOEGxM codepen- https: //codepen.io/nagasai/pen/LOEGxM

For testing , I gave one object with key-constructor and other object without constructor. 为了进行测试,我给了一个带有键构造器的对象,另一个给了没有构造器的对象。

Basically I am getting the index of key first and getting value using index 基本上我先获取键的索引,然后使用索引获取值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM