I have a javascript class with an array property. I’m created a method on the object to return a specific object from that array. However it’s not working as I expected. I don’t get an error in any browser but when I try to put it into jsfiddle I get back an error saying
Error:
Problem at line 5 character 18: Cannot set property ‘first’ of undefined
Implied global: Page 1
I have no idea what that means but it’s referring to this line
for (var i = 0; i < obj.containerDivs.length; i++) {
Here’s the full code. Can anyone tell me what I’m doing wrong? Why won’t the GetContainerDiv function return the dv but later when I iterate through the elements without the function it works fine. Thanks.
Page = function() {
this.containerDivs = ["div1", "div2", "div3"];
this.GetContainerDiv = function(obj, divId) {
for (var i = 0; i < obj.containerDivs.length; i++) {
if (obj.containerDivs[i] == divId) return d;
}
return null;
}
}
var page = new Page();
var dv = page.GetContainerDiv(page, "div1");
console.log(dv);
for (var i = 0; i < page.containerDivs.length; i++) {
if (page.containerDivs[i] == "div1") console.log(page.containerDivs[i]);
}
this.GetContainerDiv = function(obj, divId) {
for (var i = 0; i < obj.containerDivs.length; i++) {