Alexis
Alexis

Reputation: 25163

Javascript reserved word and object

I'm making a dictionary of words, so there are 1,000,000+ words. The problem comes when I need to store the word constructor. I know this is a reserved word in javascript, but I need to add it to the dictionary.

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. Is there anything else I can do?

Upvotes: 2

Views: 1422

Answers (4)

Naga Sai A
Naga Sai A

Reputation: 10975

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

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

Upvotes: 0

Jochen Bedersdorfer
Jochen Bedersdorfer

Reputation: 4122

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.

Replace var dictionary = {} with var dictionary = new Map()

Upvotes: 5

Văn Quyết
Văn Quyết

Reputation: 2526

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.

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

Upvotes: 0

Michael Fedora
Michael Fedora

Reputation: 556

Override the constructor key as undefined

According to the MDN Object.prototype page, the only thing that isn't hidden by the __fieldname__ schema is the "constructor field". Thus, you could just initialize your objects via { '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). I.E.

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. I.E.

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

Upvotes: 0

Related Questions