Reputation: 6042
I want the domMatch to be visible inside for each arrow function, if the body of the function can see this
of outer scope which in my full case is a class there should also be a way to pass domMatch object.
let tr = document.createElement("tr");
let domMatch = {};
domMatch.status = this.status;
... other props
Object.getOwnPropertyNames(domMatch).forEach(function (domMatchField, a, b, domMatch) { // domMatch is undefined
let cell = tr.insertCell(-1);
cell.innerText = domMatch[domMatchField];
});
Upvotes: 2
Views: 4517
Reputation: 136134
The problem is that you're trying to pass the variable domMatch
through the callback function in forEach
- this function accepts (at most) 3 arguments so of course the fourth will be undefined
Just omit that argument, and domMatch
should be available inside the inner scope.
var domMatch = {};
domMatch.status = "foo";
Object.getOwnPropertyNames(domMatch).forEach(function(key, index, arr){
console.log("iterating over key:", key);
console.log("index:",index);
console.log("Original array:", arr);
// Now the important bit!
console.log("domMatch", domMatch);
});
Upvotes: 3