P
Patient Guy
The code below shows the familiar way of restricting a function to be a
method of a constructed object:
function aConstructor(arg)
{
if (typeof(arg) == "undefined")
return (null);
this.property1 = arg;
this.property2 = aConstantDefinedGlobally;
this.method1 = function (anArg) {
return (this.property1 + this.property2);
};
this.method2 = function (arg1, arg2) {
var aVar = arg1 + this.property1;
// a lot---maybe 100 lines of method code
// .
// .
// .
// .
};
return (this);
}
Method 'method1' and the code manipulating object properties was added to
make it a better example in showing brief bits of code.
Note that method 'method2' is rather long in terms of lines of code.
Suppose for the sake of code readability, the attempt is made to define
the method "external" to the constructor, but that it still be a method of
the object, and not a function in global scope (oh, and another question:
is a function actually a method of the global object?). As so:
function method2(arg1, arg2)
{
var aVar.arg1 + this.property1;
// lots of lines of code follows
}
function aConstructor(arg)
{
if (typeof(arg) == "undefined")
return (null);
this.property1 = arg;
this.property2 = aConstantDefinedGlobally;
this.method1 = function (anArg) {
return (this.property1 + this.property2);
};
this.method2 = method2;
return (this);
}
Of course, this was tried and does not work, with the 'this' object is
function method2 is the global object.
So the question is, how can I set up the code so that method 'method2'
remains a method of the constructor and should not be called as a function
in global scope?
method of a constructed object:
function aConstructor(arg)
{
if (typeof(arg) == "undefined")
return (null);
this.property1 = arg;
this.property2 = aConstantDefinedGlobally;
this.method1 = function (anArg) {
return (this.property1 + this.property2);
};
this.method2 = function (arg1, arg2) {
var aVar = arg1 + this.property1;
// a lot---maybe 100 lines of method code
// .
// .
// .
// .
};
return (this);
}
Method 'method1' and the code manipulating object properties was added to
make it a better example in showing brief bits of code.
Note that method 'method2' is rather long in terms of lines of code.
Suppose for the sake of code readability, the attempt is made to define
the method "external" to the constructor, but that it still be a method of
the object, and not a function in global scope (oh, and another question:
is a function actually a method of the global object?). As so:
function method2(arg1, arg2)
{
var aVar.arg1 + this.property1;
// lots of lines of code follows
}
function aConstructor(arg)
{
if (typeof(arg) == "undefined")
return (null);
this.property1 = arg;
this.property2 = aConstantDefinedGlobally;
this.method1 = function (anArg) {
return (this.property1 + this.property2);
};
this.method2 = method2;
return (this);
}
Of course, this was tried and does not work, with the 'this' object is
function method2 is the global object.
So the question is, how can I set up the code so that method 'method2'
remains a method of the constructor and should not be called as a function
in global scope?