In this conclusion to a three-part series on nesting JavaScript functions, we see how nested functions can be used as object types. We shall make it simple. We shall consider only the case where there is only one level of nested functions.
Nested JavaScript Functions as Objects - Position of Nested Function in the Constructor (Page 4 of 5 )
The question here is, “should you define a function in the constructor before you assign it as a method, or should you assign it as a method before you define it in the constructor?”
The following two constructor functions are the same.
This one:
function ObjectType()
{
this.propertyType1 = "Yes type one";
function functionType2()
{
alert('This is function type two');
}
this.methodType2 = functionType2;
}
and this one:
function ObjectType()
{
this.propertyType1 = "Yes type one";
this.methodType2 = functionType2;
function functionType2()
{
alert('This is function type two');
}
}
In the first case the function is defined before it is assigned as a method. In the second case it is assigned before it is defined as a method. Both constructor functions work in JavaScript. It does not matter whether you assign the method before you define it, or define it before you assign it.