2

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)
        {
            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.

3 Answers 3

5

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.

1
  • Doh! I knew it was something so simple :\ Thanks!
    – Bob
    Commented Mar 18, 2011 at 11:08
0

I think it's the order of your "object" definitions, or your use of the class-keyword that's causing problems..

1
  • The order of definition is not an issue. Given where the use of the constructors takes place, and JavaScript's hoisting mechanism, it is fine ordered as is.
    – JAAulde
    Commented Mar 18, 2011 at 11:05
0

"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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.