"George Jempty" <ch*****@highstream.net> wrote in message
news:Oi**************@fe01.atl2.webusenet.com...
<snip>
Here's something I've discovered using "priveleged
methods" a la Douglas Crockford
(http://crockford.com/javascript/private.html):
If you want a constructor to utilize priveleged methods the
methods must be defined in the constructor ABOVE where they
are called. Consider the following:
function ExaminableField(element, examCriteria)
{
var formElement = element;
var name = element.name;
var required = true;
//must be defined before used privately
this.isRequired = function()
{
if (arguments.length == 1)
{
required = arguments[0]
}
return required;
}
if (!!examCriteria)
This double NOT (- !! -) is a nice construct as it produces a boolean
that represents the type-converted true-ness of its right-most operand.
Useful to flag the existence of a browser feature to avoid having to
have it type-converted for multiple subsequent tests:-
var useGetById = !!document.getElementById;
( compared with clunkier looking alternatives like:-
var useGetById = (document.getelementById)?true:false;
)
- but it is not needed here. The first - ! - will have to type-convert
its operand to a boolean value prior to inverting it, the second - ! -
will then invert that boolean and produce the value used to resolve
the - if - statement. However, if the identifier was used directly
within the if statement it would be type-converted to boolean to resolve
the - if - statement, so - if(examCriteria) - must always produce the
same results as - if(!!examCriteria) - but fractionally quicker.
{
this.isRequired(examCriteria.required);
}
}
It had been causing an error when I had my if block above the
definition of this.isRequired. Thought this might help
somebody, and/or contribute to a proper convention.
Because the function assigned to - this.isRequired - is a function
expression it is evaluated inline as the constructor executes so there
would be no value assigned to - this.isRequired - prior to the execution
of the assignment.
In contrast, inner function definitions (private methods in class based
OO terminology) are evaluated during the creation of the "variable"
object as the script enters the execution context for the function
call[1]. So it should be possible to call such a function in code that
physically precedes its definition and also render that function object
privileged by assigning a reference to it to a public object member:-
function ExaminableField(element, examCriteria){
var formElement = element;
var name = element.name;
var required = true;
if(examCriteria){
_requed(examCriteria.required);
}
this.isRequired = _requed;
function _requed(){
if(arguments.length == 1){
required = arguments[0]
}
return required;
}
}
- Doing so seems unnecessary. I would just put up with having to write
the constructor in the required order if I wanted the method to be
privileged.
[1] The Mozilla/Gecko implementation seems to be a bit off spec (ECMA
262 3rd Edition) in this respect as it handles inner function
definitions contained within blocks, such as - if(x){ function doX(){
.... } } - as if they were inline in some sense. It would be unusual to
be using inner function definitions in that way so there should be no
significant consequences.
Richard.