JavaScript对象文字的有趣问题

时间:2012-04-25 23:06:13

标签: javascript json object-literal

尝试运行此代码:

var oObject = {"COL_SEQ":16,"SERVICE_CD":0.000000,"SERVICE_ID":0.000000,"COL_NAME":"","COL_OPTION":"nosort nofilter","COL_CLASS":"REMOVE_PATIENT","COL_TYPE":"","COL_LABEL":"","COL_OPTION":""};

alert(oObject.COL_SEQ);
alert(oObject.COL_OPTION);
alert(oObject.COL_CLASS);

我希望第二个警报显示“nosort nofilter”,但它没有;它会发出空白信息。

实例: http://jsfiddle.net/zD7Wm/

为什么会这样?

1 个答案:

答案 0 :(得分:4)

在对象的末尾有第二个(空)COL_OPTION,它会覆盖你的初始值。

如果你取出第二个,你的代码可以正常工作:

var oObject = {"COL_SEQ":16,"SERVICE_CD":0.000000,"SERVICE_ID":0.000000,"COL_NAME":"","COL_OPTION":"nosort nofilter","COL_CLASS":"REMOVE_PATIENT","COL_TYPE":"","COL_LABEL":""};