Javascript自定义对象-IE中的预期标识符
我不熟悉用JavaScript创建自定义对象,所以它可以很简单 我有这些物品:Javascript自定义对象-IE中的预期标识符,javascript,internet-explorer,custom-object,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 Arr
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=newjsonobj(id、title、class、icon)调用它代码>都是字符串变量
它们在Chrome和Firefox中运行良好,但在IE(8)中却不行。IE具有错误-预期标识符。不能将保留关键字“class”用作任何变量或属性名。有趣的是,这是IE为数不多的几个正确的地方之一,其他地方都不正确。我认为是你的“对象”定义的顺序,或者是你对class关键字的使用造成了问题。正如@JAAulde所指出的,“class”是一个保留关键字。不过,如果将“class”括在引号中,您仍然可以将其用作js属性名:
this.“class”=\u class代码>
这一点很重要,因为某些库(如)要求您传递包含“class”属性的options对象。将类属性名称转义为引号(如上列代码)将使其在IE和其他浏览器中都能正常工作。定义顺序不是问题。考虑到构造器的使用地点和JavaScript的提升机制,它是按原样排列的。Doh!我知道事情很简单:\谢谢!