简体   繁体   English

为什么这不使用forEach绑定,但是使用for in循环绑定?

[英]Why this doesn't bind using forEach, but does bind using a for in loop?

I was attempting to make a function that sets multiple attributes to an HTML element. 我试图制作一个将多个属性设置为HTML元素的函数。 I used the code provided by a Stack Overflow user on this page 我在此页面上使用了Stack Overflow用户提供的代码

Setting multiple attributes for an element at once with JavaScript 使用JavaScript一次为一个元素设置多个属性

If I use a for in loop, the binding of this is successful, but if I use a forEach it is not? 如果使用for in循环,则此绑定成功,但是如果使用forEach,则绑定不成功? Why is that? 这是为什么?

This Works 这个作品

Element.prototype.attributeSetter = function(attrs){
  for(var prop in attrs){
    if((prop == "style" || prop == "styles") && (typeof attrs[prop] === 'object')){
        for(var n in attrs[prop]){
           this.style[n] = attrs[prop][n];
        }
    }else if(prop == "html"){
        this.innerHTML = attrs[prop];
    }else{
        console.log("this: ", this);
        this.setAttribute(prop, attrs[prop]);
    }
  }
}

This does not work 这行不通

Element.prototype.attributeSetter = function(attrs){
    Object.keys(attrs).forEach(function(prop){
      if((prop == "style" || prop == "styles") && (typeof attrs[prop] === 'object')){
         for(var n in attrs[prop]){
           this.style[n] = attrs[prop][n];
         }
      }else if(prop == "html"){
        this.innerHTML = attrs[prop];
      }else{
        //TypeError: this.setAttribute is not a function
        console.log("this: ", this);
        this.setAttribute(prop, attrs[prop]);
      }
  });
}

However if I don't modify the Element Object and just make a regular function with the ForEach loop it works just fine. 但是,如果我不修改Element Object并仅使用ForEach循环创建一个常规函数,它就可以正常工作。

Simple Implementation 简单实施

var myDiv = document.getElementById("myDiv");

myDiv.attributeSetter({
    class: "coolDiv",
  style: {
    color: "#0110ff",
    border: "2px solid lime"
  },
  "data-order": "one",
  "html": "Cool Div"
}); 

The second snippet with foreach does not work because inside foreach this does not point to the div but to the window object and that is why you are getting error this.setAttribute is not a function 带有foreach的第二个片段不起作用,因为在foreach内部不指向div而是指向window对象,这就是为什么您遇到错误this.setAttribute不是函数

Change the implementation like below. 如下更改实施。

 Element.prototype.attributeSetter = function(attrs){ var elem = this; Object.keys(attrs).forEach(function(prop){ if((prop == "style" || prop == "styles") && (typeof attrs[prop] === 'object')){ for(var n in attrs[prop]){ elem.style[n] = attrs[prop][n]; } }else if(prop == "html"){ elem.innerHTML = attrs[prop]; }else{ //TypeError: this.setAttribute is not a function elem.setAttribute(prop, attrs[prop]); } }); } var myDiv = document.getElementById("myDiv"); myDiv.attributeSetter({ class: "coolDiv", style: { color: "#0110ff", border: "2px solid lime" }, "data-order": "one", "html": "Cool Div" }); 
  <div id="myDiv"></div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM