Problems with Object.toString in IE8, backbone.js

What happens with IE8 and the toString method of objects?

I am trying to override toString in my models in Backbone.js, but IE8 does not seem to know that this method exists. Changing the method name to something else works fine, but why can't I use toString ? This works in 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 Code: http://jsfiddle.net/x96mR/3/

+6
source share
1 answer

If you move toString outside of Backbone.Model.extend in:

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

It works. I would suspect that Backbone does some funky things that don't work properly in IE8

Edit (thanks @Ferdinand Prantl):

All properties passed to Backbone.extend are added to the prototype model using the for-in enumeration. IE < 9 has an error in which it will not copy some properties called DontEnumBug.

Dontenumbug

In IE <9, JScript will skip any property in any object where there is a property of the same name in the prototype chain of the object that has the DontEnum attribute.

toString , valueOf, toLocaleString, prototype, isPrototypeOf, property IsEnumerable, hasOwnProperty, length and unique will be skipped.

+9
source

Source: https://habr.com/ru/post/893521/


All Articles