I have this code that works perfectly well on its own :
var Person = {
getName: function() {
alert("Johnny");
}
};
var myContext = "Person";
var theCodeToExecute = myContext + ".getName()";
var theFunctionItself = new Function(theCodeToExecute);
theFunctionItself();
But when I put it inside jQuery, it stops working.
$(document).ready(function() {
//Where the code should be.
});
I'm aware that very similar questions have been answered, but none of them fixed that precise problem.
EDIT :
Thanks everyone for the answers. I guess I narrowed down my problem so much that I made my intentions very unclear. Here's a clearer version of what i'm trying to achieve :
var Road = {
House: function(color, size, neighbor){
this.color = color;
this.size = size;
this.neighbor = neighbor;
}
};
Road.House.prototype.getSize = function() {
alert(this.size);
};
Road.House.prototype.getNeighborSize = function() {
var theNeighbor = this.neighbor;
var codeToExecute = theNeighbor + ".getSize()";
var tmpFunc = new Function(codeToExecute);
tmpFunc(); //this only works when outside of jQuery.
};
var house1 = new Road.House("blue", "small", "house2");
var house2 = new Road.House("red", "huge", "house3");
house1.getNeighborSize(); //this successfully returns "huge" when outside of jQuery.
Again, this works perfectly well on its own but doesn't work within jQuery, and I need it to work within jQuery because the final version of my functions will use a lot of jQuery code. Thanks again!
LAST EDIT :
Thanks Felix for your excellent help. There seems to be one final problem. I can only get the size of a neighbor if the neighbor is declared BEFORE the house I'm querying.
var house1 = new Road.House("red", "big", house2);
var house2 = new Road.House("blue", "huge", house3);
var house4 = new Road.House("blue", "small", house4);
var house3 = new Road.House("blue", "little", house1);
house1.getNeighborSize(); //this doesn't work.
house3.getNeighborSize(); //this works
Thanks again!!