Say I have a function that I would like reuse as a method on a couple objects in order to add data to those objects.
function addToObject(data) {
for (var d in data) {
if (data.hasOwnProperty(d)) {
this[d] = data[d];
}
}
}
myObjOne = {
add: addToObject
};
myObjTwo = {
add: addToObject
};
My goal here was to be able to call myObjOne.add(myData)
where myData
is an object that I would like to add to myObjOne
and be able to replicate this functionality on myObjTwo
.
My issue is that using this
within addToObject
gives me:
this[d] = data[d];
^ Possible strict violation.
in jshint.
Why is this?