If I make an object through a new constructor()
call, to something like this:
function constructor(){
function privateFn(){
console.log('this is private');
}
this.publicFn = function(){
console.log('this is public');
}
function doSomething(){
privateFn();
publicFn();
}
console.log(this.publicFn ? "exists" : "does not exist"); // logs exists
doSomething(); //throws an error -> publicFn() is not this.publicFn().
}
new constructor();
So the question is, is there any way to make this accessible without the this.
part?
My IDE (netbeans) appears to recognize it even without the this.
though that doesn't necessarily mean anything, but it got me wondering, is it possible to somehow reference publicFn()
as a function, rather then as a property of the object? Maybe construct it differently?
EDIT: The goal is to create an object which has both private and public methods, with the new
constructor, but simultaneously allow the constructor itself as well as all the object's methods to call the public methods without the this.
prefix.
SPECIAL EDIT FOR BEKIM BACAJ
Updated this section specially for you just to show you what I mean.
It is not the console.log tricking me, the call to a doSomething
or whatever other method comes after the object is indeed created. It is still not accessible though, and now that I've seen the answer as to why, it makes some sense. The contextual this
is not the same for the private methods, or rather the functions within the constructor have this
set to window, so their contextual this
is not the same as that of the public method they are attempting to call. The pattern that Phylogenesis suggested is what I am looking for and it fits the needs.
function constructor(){
function privateFn(){
console.log('Call to privateFN succesful');
console.log('------------------------------------------------------------------------------------');
}
function doSomething(){
console.log('Executing: doSomething;');
//I want to be able to access both private and public methods here (which i can)
//I want to be able to access them the same way (which I cannot).
privateFn(); //works
publicFn(); //error
//Read NOTE1 below.
}
this.callDoSomething = function(){
console.log('Call to callDoSomething succesful');
console.log('------------------------------------------------------------------------------------');
//made just so that you can access doSomething from the outside to test.
doSomething();
};
this.publicFn = function(){
console.log('Call to publicFN succesful');
console.log('------------------------------------------------------------------------------------');
};
//.... many more methods (we are talking hundreds)
//.... some of them are public, some are private
//.... some are resutls of calling functions that return functions from other files (collaborative work),
//.... these might be public or private, which we achive like so:
//.... var externalModule = extrenalModuleInitializer(this);
//.... as you can imagine this externalModuleInitializer function can either:
//.... A) PRIVATE: return a function, making this externalModule variable a private method
//.... B) PUBLIC: do stuff with the `this` object that was passed, adding public methods to it
//.... So since I do not know if certain parts of this object will be private or public, i need
//.... a way to access them in the same way in order to make the coding easier.
}
console.clear();
var a = new constructor();
a.publicFn();
a.callDoSomething();