Forum Moderators: open
function Car()
{
this.gas = 0;
}
Now I want a method that is accessible via an instance of that class. For example:
function CarFillUp()
{
alert("Executing: " + arguments.callee);
this.gas = 10;
}
Car.prototype.fillUp = CarFillUp;
Thus, I could do the following:
var camry = new Car();
carmry.fillUp();
How can I prevent method CarFillUp() from being called directly?
For example, I don't want someone to be able to do this:
var camry = new Car();
camry.fillUp();
CarFillUp();
One solution is to define the function in the assignment to the class:
Car.prototype.fillUp = function(){
alert("Executing: " + arguments.callee);
this.gas = 10;
}
But the problem with that is that I don't know what function I'm in during execution (because the function does not have a name). That is, arguments.callee will return an empty string.
Is it valid to name the function when assigning it? For example:
Car.prototype.fillUp = function CarFillUp(){
alert("Executing: " + arguments.callee);
this.gas = 10;
}
And if so, does that prevent someone from accessing the function by name (that is, they can do object.fillUp(); but not object.CarFillUp(); or CarFillUp();)?
Thanks.
One thing I was concerned about was conflicting method names. For example, I might have 2 "classes" defined in the same JS that both have their own method named "fillUp". However, it seems that I AM able to do this:
function Car()
{
this.gas = 0;
}
Car.prototype.fillUp = function fillUp()
{
alert("Executing: " + arguments.callee);
this.gas = 10;
}function Bucket()
{
this.gallons = 0;
}
Bucket.prototype.fillUp = function fillUp()
{
alert("Executing: " + arguments.callee);
this.gallons = 2;
}
And there doesn't *seem* to be any conflict. Can anyone verify that this is ok to do?
Thus, I can then do:
var camry = new Car();
camry.fillUp();
var gasJug = new Bucket();
gasJug.fillUp();
And the appropriate functions are called. I think this also makes it so the functions can't be called directly... that is, they can only be called as a member function of the class. Correct?
With reference to #2, there is a conflict in the code, but it doesn't affect you..
Here is a function:
function doo(){ /* stuff */ } One result of this is that a global variable, doo, is assigned the the function (as an object).
You have two functions declared with the same name, fillUp. The result will be that the global var, fillUp, will be assigned to the last declared function. However, since references to each function are also stored as properties of Car.prototype, and Bucket.prototype respectively, you can still access and use these functions.
In fact, in most cases, one would usually assign anonymous functions to the prototype:
Bucket.prototype.fillUp = function()
{
this.gallons = 2;
}
This way, there is no global variable that holds a reference to the function. Note, however, that the function can still be called in another context (that of the prototype itself).
Bucket.prototype.fillUp();
It is possible to competely isolate the function by declaring it inside the constructor for each object, but this creates innefficiency, makes inheritance tricky, and probably isn't worth it.
In fact, in most cases, one would usually assign anonymous functions to the prototype:Bucket.prototype.fillUp = function()
{
this.gallons = 2;
}This way, there is no global variable that holds a reference to the function. Note, however, that the function can still be called in another context (that of the prototype itself).
Bucket.prototype.fillUp();
Yep, I understand that. The problem, though, is that I have some built in javascript logging. In the body of my functions, I will typically call one of the global logging functions, passing in the current "arguments.callee" value, so that I can strip out the function name and include it in the logging message. For example, inside of Bucket.prototype.fillUp, I might do this:
debug("Gallons pre-fill: " + this.gallons, arguments.callee);
If this is an anonymous function, then arguments.callee does not contain a function name, so this doesn't give me the results I want. :(
The simple way to work around that is to hard-code the information you want to log:
I guess it depends on how you interpret "simple". :) I was trying to avoid having to retype the function name and just include something static for the 2nd parameter. For example I could just paste this:
debug("",arguments.callee);
where ever I wanted logging, and then fill in the first argument (not having to worry about what function I'm in, or if the function name changes not having to change all my debugging calls).