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

Sam*_*Sam 6 javascript internet-explorer-8 backbone.js

什么是IE8和toString对象的方法?

我试图toString在我的Backbone.js模型中覆盖,但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"
Run Code Online (Sandbox Code Playgroud)

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

Jar*_*eer 9

如果你移动到toString外面Backbone.Model.extend:

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

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

编辑(感谢@Ferdinand Prantl):

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

DontEnumBug

在IE <9中,JScript将跳过对象原型链中具有DontEnum属性的同名属性的任何对象中的任何属性.

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

  • Backbone通过for-in枚举来复制原型中的所有属性.IE使用名称跳过属性:constructor,toString,toLocaleString,valueOf和isPrototypeOf.它被称为[DontEnumBug](https://developer.mozilla.org/en-US/docs/ECMAScript_DontEnum_attribute#JScript_DontEnum_Bug). (2认同)