如何在Javascript对象中使用Unicode字符串键? [英] How can i use Unicode string key In Javascript Object?

查看:83
本文介绍了如何在Javascript对象中使用Unicode字符串键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Object中使用unicode字符串作为键,例如:

I want to use unicode string in Object as key, something as:

var t = {"姓名": "naitong"};

没关系,t [姓名]返回naitong

it's ok , t["姓名"] return "naitong"

但是

Object.keys({"姓名": "naitong"})

我得到了,一个空白字符串

I got " ", a blank string

任何人都知道为什么?

编辑:

我安装了firebug并在控制台中尝试它,它可以工作。
实际上我使用mozrepl,这样我就可以在emacs中编辑和运行javascript。
所以这与mozrepl有关

I install firebug and try it in the console, it works. Acctually i use mozrepl, so that i can editing and run javascript in emacs. So This have something to do with mozrepl

我已经确认mozrepl只支持7bit safe ASCII,转换unicode,我必须json-encode它在emacs中,如下:

I have confirm that mozrepl support only "7bit safe ASCII", to tranform unicode ,i have to json-encode it in emacs, as:

alert(Object.keys(JSON.parse("{\"\\u59d3\\u540d\":\"naitong\"}")))

这是我在stackoverflow上提出的第一个问题,我快速得到了回复。
谢谢大家。

This is my first question asked on stackoverflow, and i got quick resp. Thank you all.

推荐答案

在firebug控制台中为我工作正常:

Works fine for me in the firebug console:

>>> Object.keys({"姓名": "naitong"})
["姓名"]

也许您正试图在使用不包含这些符号的不同字符集的页面上显示它。

Maybe you are trying to display it on a page that uses a different charset which does not contain those symbols.

这篇关于如何在Javascript对象中使用Unicode字符串键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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