5

How can I use a Chinese character or a number as the key of an object like this:

var obj = { 我: 'me',  20: 'you' };   

console.log(obj.我);  // me
console.log(obj[我]); // -> Reference Error: 我 is not defined

console.log(obj[20]); // you
console.log(obj.20);  // ->Syntax Error: Unexpected number
royhowie
  • 11,075
  • 14
  • 50
  • 67
  • 3
    You can't and shouldn't. – Jazi Apr 23 '15 at 06:39
  • It's JS syntax limitation. You can't do so. – Raptor Apr 23 '15 at 06:40
  • 7
    yes we can: `console.log(obj['我']);` (we just can't refer to undefined symbols); unlike other langs, JS has been unicode-friendly since the start... – dandavis Apr 23 '15 at 06:41
  • as we all know, we can use the string as the key, so we call use obj[‘我’]. If so, when we declare the key with chinese or number, what changed? let's see some code again:console.log(Object.keys(a)); – Justin Wang Apr 23 '15 at 06:49

3 Answers3

9

In order to use dot notation in object, key must be a valid JavaScript identifier. Mozilla Documentation Network states:

You can use ISO 8859-1 or Unicode letters such as å and ü in identifiers. You can also use the Unicode escape sequences as characters in identifiers.

And indeed, in Firefox this is valid syntax:

var x = { 我: 5 };
x.我
var 我 = 42;
console.log(我);

Chrome accepts this too.

In case your browser (or browser your visitors use) do not follow standards that closely, you can use:

var obj = { '我': 'me',  20: 'you' };
console.log(obj['我']);

It's worth noting that you can use arbitrary strings as object keys, even if they are not valid JavaScript identifiers (eg. thay contain spaces and/or punctuation):

var obj = { " ": "space", ";": "semicolon" };
0

It's generally a bad practice to include non ANSI characters in your source JS code in .html* or .js files, as they may be lost during editing the files (i.e. while saving as ANSI instead of Unicode format) or when switching the hosting OS.

Consider converting these characters to their Unicode equivalent using JavaScript's escape() method for instance (i.e. escape('我') returns: "%u6211").

Check the following link for complete info: W3C character escapes in markup and CSS

royhowie
  • 11,075
  • 14
  • 50
  • 67
m3nation
  • 206
  • 2
  • 7
  • "Unicode equivalent"...? I think you mean Unicode character escape sequence. Those characters are already "Unicode" (in the widest sense), there's no more equivalent equivalent than you already have. – deceze Apr 23 '15 at 07:15
0

Your confusion is that obj.我 works but obj[我] doesn't? On the other hand, obj[20] does?

Well, 20 is a value literal. You can write 20 anywhere to create the number value 20. On the other hand, is not a valid literal for anything. If you want to create the string value "我", you need to write '我'. On the other hand, if you want to be a variable name, you need to declare the variable beforehand with var 我. The error you're seeing is telling you that there's no variable defined.

On the other hand again, obj.20 is invalid syntax since 20 is not a valid variable name. The reason for that is that numeric literals create numbers, hence the syntax would become ambiguous if they could also be used as variable names. Does foo = 20 mean you want to assign the number 20 to foo, or the value of the variable 20? Hence, numeric literals are reserved for numbers, period.

obj.我  → requires valid symbol name, 我 is valid symbol name
obj[我] → requires valid *value*, 我 is not value literal nor declared variable

obj.20  → requires valid symbol name, 20 is not valid symbol name
obj[20] → requires valid *value*, 20 is valid value literal

This would work just fine:

var 我 = '我';
obj[我];
deceze
  • 510,633
  • 85
  • 743
  • 889