IE8 Array.prototype.slice: 'this' is not a JavaScript object - Stack Overflow

I'm getting this error message only in IE8, and I don't know how to convert the existing func

I'm getting this error message only in IE8, and I don't know how to convert the existing function for IE8 patibility.

_initEvents : function() {
     var self = this;

     Array.prototype.slice.call(this.menuItems).forEach(function(el, i) {
         var trigger = el.querySelector('a');

         if (self.touch) {
             trigger.addEventListener('touchstart', function(ev) {
                   self._openMenu(this, ev); 
               });
         }
         else {
             trigger.addEventListener('click', function(ev) {
                   self._openMenu(this, ev);
               });  
        }
     });
   window.addEventListener('resize', function(ev) {
         self._resizeHandler();
     });

},

the above is just a part of it, I dont think the rest is needed. The error happens here:

 Array.prototype.slice.call( this.menuItems )

I'm getting this error message only in IE8, and I don't know how to convert the existing function for IE8 patibility.

_initEvents : function() {
     var self = this;

     Array.prototype.slice.call(this.menuItems).forEach(function(el, i) {
         var trigger = el.querySelector('a');

         if (self.touch) {
             trigger.addEventListener('touchstart', function(ev) {
                   self._openMenu(this, ev); 
               });
         }
         else {
             trigger.addEventListener('click', function(ev) {
                   self._openMenu(this, ev);
               });  
        }
     });
   window.addEventListener('resize', function(ev) {
         self._resizeHandler();
     });

},

the above is just a part of it, I dont think the rest is needed. The error happens here:

 Array.prototype.slice.call( this.menuItems )
Share Improve this question edited Jul 1, 2013 at 1:12 Francesco Belladonna 11.7k14 gold badges83 silver badges151 bronze badges asked Jul 1, 2013 at 1:04 MikeMike 1715 silver badges10 bronze badges 11
  • 3 forEach is not supported in IE8. – elclanrs Commented Jul 1, 2013 at 1:05
  • 3 @elclanrs—I may be wrong, but I suspect it doesn't get that far. The error is consistent with menuItems being a DOM object, not a native object. Host objects can't be treated like native objects in IE. – RobG Commented Jul 1, 2013 at 1:15
  • What is menuItems? Is it a DOM object? – RobG Commented Jul 1, 2013 at 1:16
  • @RobG: I immediately thought the same. If it's a NodeList, for instance, slice() won't convert it to an array in IE <= 8. – Andy E Commented Jul 1, 2013 at 1:22
  • 6 There's no reason to do the slice. Since you seem to have .forEach() shimmed, you can just do Array.prototype.forEach.call(this.menuItems, func...). This will avoid the IE8 issue of setting the this value of a native method to a non-native object. – user2437417 Commented Jul 1, 2013 at 1:23
 |  Show 6 more ments

1 Answer 1

Reset to default 5

When you call:

this.menuItems = this.el.querySelectorAll( '.cbp-hsmenu > li' );

the object assigned to menuItems is a static NodeList, which is a host object. Then when you do:

Array.prototype.slice.call( this.menuItems )

you are calling a built–in method with a host object as this. In IE 8 and lower (and probably lots of other older browsers), you can't do that (there is no specification that says you should, though modern browsers let you).

The simple solution is to convert menuItems to an array using some other method than call, or to add a shim for Array.prototype.forEach and use CrazyTrain's suggestion:

Array.prototype.forEach.call(this.menuItems, func...)

because in browsers without a built–in forEach, it will be a native method and work just fine. But for robust code, replace all that with a simple for loop.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744864634a4597907.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信