Is this possible and if so how? (I haven’t been able to find a way for it to work)
var foo = function()
{
console.log("Original Function");
this = function()
{
console.log("New Function");
}
};
console.log("Calling foo()");
foo();
Desired output:
Original Function
Calling foo()
New Function
To answer the questions in advance, yes I know there are other ways to do this. I’m just curious if I could do something like var bar = new foo(); and have foo() assign some time dependent properties to itself without an extra function.
EDIT: Response to Charmanders question, and a slightly less simplified, more practical application
var node = function(parent)
{
this = function()
{
for (i in this)
{
// perform actions (which will occasionally include calling i()
}
}
if (parent !== null)
{
this.parent = parent;
// code to determine children
this.somechild = new node(this);
this.someotherchild = new node(this);
}
};
var ancestor = new node(new node(null));
Assigning a new value to
thiswill not work, but you can simply return a function: