IE8中的Object.toString问题,backbone.js

时间:2011-07-25 19:38:12

标签: javascript internet-explorer-8 backbone.js

IE8和对象的toString方法有什么用?

我试图在Backbone.js的模型中覆盖toString,但IE8似乎没有认识到该方法存在。将方法名称更改为其他名称可以正常工作,但为什么我不能使用toString?这适用于Chrome。

var Foo = Backbone.Model.extend({
    toString: function(){ return this.get("name"); },
    description: function(){ return this.get("name"); }
});

var f = new Foo({name: "a foo"});

document.writeln(f.toString());    // "[object Object]", should be "a foo"
document.writeln("<br/>");
document.writeln(f.description()); // "a foo"

JSFiddle代码:http://jsfiddle.net/x96mR/3/

1 个答案:

答案 0 :(得分:9)

如果您将toString移到Backbone.Model.extend之外的地方:

Foo.prototype.toString = function(){ return this.get("name"); };

有效。我怀疑Backbone正在做一些在IE8中无法正常工作的时髦东西

编辑(感谢@Ferdinand Prantl):

传递到Backbone.extend的所有属性都会使用prototype枚举添加到模型的for-in中。 IE < 9有一个错误,它不会复制称为DontEnumBug的某些属性。

<强> DontEnumBug

  

在IE&lt; 9,JScript将跳过任何对象所在的任何属性   在对象的原型链中有一个同名的属性   具有DontEnum属性。

构造函数, toString ,valueOf,toLocaleString,prototype,isPrototypeOf,propertyIsEnumerable,hasOwnProperty,length和unique都将被跳过。

相关问题