Javascript reserved words? - Stack Overflow

I have some JS code which generates the following object, return {"type": "some thing&qu

I have some JS code which generates the following object,

return {
    "type": "some thing",
    "width": 2,
    "colour": "#AA12BB",
    "values": [2,3,4]
}

The creation of this isn't a problem.

In writing the test for the method that returns this am having a problem accessing the width/type attributes: the following assertions fail (it leads to a execution/syntax error, which go away when i ment them).

assertEquals('some thing', jsonObj.type);
assertEquals(2, jsonObj.width);

while

assertEquals('#AA12BB', jsonObj.colour);

passes

Since I cannot change the key names for what I am doing, is there any way to access these values?

I have some JS code which generates the following object,

return {
    "type": "some thing",
    "width": 2,
    "colour": "#AA12BB",
    "values": [2,3,4]
}

The creation of this isn't a problem.

In writing the test for the method that returns this am having a problem accessing the width/type attributes: the following assertions fail (it leads to a execution/syntax error, which go away when i ment them).

assertEquals('some thing', jsonObj.type);
assertEquals(2, jsonObj.width);

while

assertEquals('#AA12BB', jsonObj.colour);

passes

Since I cannot change the key names for what I am doing, is there any way to access these values?

Share Improve this question asked Feb 11, 2009 at 19:00 j pimmelj pimmel 11.7k6 gold badges36 silver badges43 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

Try this:

assertEquals('some thing', jsonObj["type"]);
assertEquals(2, jsonObj["width"]);

Your example works fine for me. ‘width’ and ‘type’ are not reserved words in JavaScript (although ‘typeof’ is).

dot notation does not work with reserved words, such as "type". In that case you have to use array notation.

Mozilla's list of Java Script Reserved words.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745118718a4612285.html

相关推荐

  • Javascript reserved words? - Stack Overflow

    I have some JS code which generates the following object, return {"type": "some thing&qu

    12小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信