Execution context in JavaScript

后端 未结 2 1450
一整个雨季
一整个雨季 2021-01-28 12:21

I have an object named Gallery with several properties and methods. One of them, setCurrentPicture, removes all existing photos and videos DOM elements from gallery container b

2条回答
  •  [愿得一人]
    2021-01-28 12:39

    I should add bind this to Array.prototype.forEach.call instead of implementing this to callback function.

     Array.prototype.forEach.call(container.children, function(item) {
              if (item.tagName === 'VIDEO') {
                document.removeEventListener('click', this.togglePlayVideo);
              }
              if (item.tagName === 'VIDEO' || item.tagName === 'IMG') {
                container.removeChild(item);
              }
            }.bind(this));
    

    Thanks to all for the answers.

提交回复
热议问题