Javascript自定义对象 - IE中的预期标识符

时间:2011-03-18 10:51:58

标签: javascript internet-explorer custom-object

我不熟悉在JavaScript中创建自定义对象,因此它很容易变得简单。

我有这些对象:

        function jsonObj(_id,_title,_class,_icon)
        {
            this.attr = new jsonAttrObj(_id,_title,_class);
            this.data = new jsonDataObj(_title,_icon);
            this.children = new Array();
        };

        function jsonAttrObj(_id, _title, _class)
        {
            this.id = _id;
            this.title = _title;
            this.class = _class;
        };

        function jsonDataObj(_title, _icon)
        {
            this.title = _title;
            this.icon = _icon;
        };

我使用var jsonObject = new jsonObj(id,title,class,icon);将其称为字符串变量。

它们在Chrome和Firefox中运行良好,但不适用于IE(8)。 IE有错误 - 预期标识符。

3 个答案:

答案 0 :(得分:5)

您不能将保留关键字“class”用作任何变量或属性名称。这里有趣的事情 - 这是IE正确的事情之一,其余的不是。

答案 1 :(得分:0)

我认为这是你的“对象”定义的顺序,或者你使用引起问题的class-keyword ...

答案 2 :(得分:0)

“class”是一个保留关键字,正如@JAAulde指出的那样。但是,如果将它括在引号中,您仍然可以使用'class'作为js属性名称:

this."class" = _class;

这很重要,因为某些库(如Bootbox)要求您传递包含“class”属性的选项对象。像上面的代码行一样在引号中转义类属性名称将使其在IE以及其他浏览器中起作用。