Javascript for..in looping over arguments ie.for( arg in arguments) does not work in IE8 but it works in Chrome 8

前端 未结 6 1508
刺人心
刺人心 2020-12-31 17:01

I faced this strange situation where foreach like construct of javascript does not work in IE but it works in FF. Well not all for..in just this special funcito

6条回答
  •  萌比男神i
    2020-12-31 17:54

    Try using this as a format function:

    String.prototype.format = function() {
        var me = this;
        for (var i = 0; i < arguments.length; i++)
            me = me.replace(new RegExp('\\{' + i + '\\}', 'g'), arguments[i]);
        return me;
    }
    

    Now this should work:

    alert('The {0} is dead. Don\'t code {0}. Code {1} that is open source!'.format('ASP', 'PHP'))
    

    DEMO

    Tested and working in IE

提交回复
热议问题