Javascript Custom Object - Expected Identifier in IE
I'm new to creating custom object in JavaScript, so it could easily be something simple.
I have these objects:
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)
开发者_JAVA百科 {
this.title = _title;
this.icon = _icon;
};
I call it using var jsonObject = new jsonObj(id,title,class,icon);
all being string vars.
They work fine in Chrome and Firefox, but not IE(8). IE has the error - Expected Identifier.
You cannot use the reserved keyword 'class' as any variable or property name. Funny thing here--this is one of the few places where IE is getting it right and the rest are not.
I think it's the order of your "object" definitions, or your use of the class-keyword that's causing problems..
"class" is a reserved keyword, as @JAAulde points out. You can still use 'class' as a js property name, though, if you enclose it in quotes:
this."class" = _class;
This is important because some libraries such as Bootbox require you to pass an options object including a 'class' property. Escaping the class property name in quotes like the above line of code will get this to work in IE as well as other browsers.
精彩评论