Skip to content Skip to sidebar Skip to footer

Javascript: Instantiating New Object From It's Own Recursion

I'd like to be able to create a function that returns a grid of parameter defined size and inserts parameter defined data into the grid's 'cubbies'. For instance: function createGr

Solution 1:

Something like this: createGrid(1,2,createGrid(2,2,createObj)) - [but] it just provides a reference to the first grid in the next cubby.

Same problem as last time: You're passing a reference to the one constructed grid, which will then be referenced from all new cubbies - it will alert("was object"); and objects are copied by reference.

If you want an instantiation to happen for each constructed cell, then you will need to pass a function for the dataFunction parameter again:

createGrid(1,2,function() {
    return createGrid(2,2,createObj);
})

Also notice that if (typeof dataFunction == 'object') data = new dataFunction(); is rubbish. You can use new only on constructor functions, whose typeof is 'function'. Otherwise (when dataFunction is not constructable) it would throw an exception.

Post a Comment for "Javascript: Instantiating New Object From It's Own Recursion"