Recently, I encountered a problem while trying to use the prototype method to apply the same function or variable to multiple objects. Despite creating numerous objects in the following manner:
var item = {
a: {
aa: "lalala",
ab: 1,
something: 3
},
b: {
ba: "jfjb",
bb: 2,
something: 4
}
}
When attempting to implement the prototype method like so:
item.prototype.bob = 2;
An error message stating
"Cannot set property 'bob' of undefined"
was displayed. The same issue arose when trying to create a method:
item.prototype.bob = function() {
100 - this.something;
this.something++;
}
I am at a loss as to what I might be doing incorrectly. Is there an alternative method that can achieve the desired outcome across multiple objects?