JavaScript保留字和对象 [英] Javascript reserved word and object

查看:59
本文介绍了JavaScript保留字和对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作单词词典,所以有1,000,000+个单词. 当我需要存储单词constructor时,问题就来了.我知道这是javascript中的保留字,但我需要将其添加到字典中.

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

我该如何解决?我可以像key=key+"_"那样弄糟它,但这似乎很糟糕.我还能做些什么吗?

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?

推荐答案

您可以使用内置的Map类型而不使用JS对象,该类型将字符串/符号用作键,并且不与任何现有属性冲突

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.

替换 var dictionary = {}var dictionary = new Map()

这篇关于JavaScript保留字和对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆