I'm writing a node application and I would like to get the name of the variable that invokes a constructor.
a=new constructorFunction();
b=new constructorFunction();
function constructorFunction(){
this.test="it works!";
this.doSomething=function(){
//this should return the contents of this.test
}
}
console.log(a.doSomething());
console.log(b.doSomething());
As the comment says, I'd like to return the values of a.test and b.test. How can I accomplish this?
It doesn't need to be any more complicated than this (fiddle):
function constructorFunction() {
this.test="it works!";
this.doSomething = function() {
return this.test;
}
}
a=new constructorFunction();
b=new constructorFunction();
function constructorFunction(){
var self = this;
this.test="it works!";
this.doSomething=function(){
return self.test;
}
}
In chrome this works... (V8 so I assume node.js will behave the same)
a=new constructorFunction();
b=new constructorFunction();
function constructorFunction(){
this.test="it works!";
this.doSomething=function(){
return this.test;
}
}
console.log(a.doSomething());