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.

Bob
  • 3,074
  • 11
  • 43
  • 62

3 Answers3

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.

JAAulde
  • 19,250
  • 5
  • 52
  • 63
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.

Brad Griffith
  • 451
  • 3
  • 5
0

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

Thor Jacobsen
  • 8,621
  • 2
  • 27
  • 26
  • 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 Mar 18 '11 at 11:05