contents   index   previous   next



SCOPING - FUNCTIONS

 

Functions are more complex. The normal behavior for a function is to search its local variables and parameters first. Next, the local variables and parameters of its parent function are searched. This only applies if the function is nested inside a parent function, and it includes all parents if it is nested several levels deep. Finally, the global variables are searched. Again, the user can modify this behavior using the with statement.

 

There are several methods for controlling the scope of functions. If you call the function directly using seEval, you can specify additions to the scope chain using the ssetScopeStart and setScopeEnd methods of the SEEvalParams class as described above. This method is rarely used because functions are usually called from within a script.

The second method is to use the SE.IMPLICIT_THIS and SE.IMPLICIT_PARENTS attributes. A script function can be given these attributes using the seSetAttribs API call. Both of these attributes modify the function's scope chain by adding elements to the scope chain after the local variables, but before the global variables. The SE.IMPLICIT_THIS flag makes the function add its this object to the scope chain. This makes the function behave much like a Java method in that members of the this object can be referred to directly without having to qualify them with this. as is normal for JavaScript. SE.IMPLICIT_PARENTS is similar, except the parents of the this variable are added to the scope chain. Parents are linked through the __parent__ (two underscores on each side) member. this.__parent__ is the parent of the this variable and is added to the scope chain if SE.IMPLICIT_PARENTS attribute is set in the called function. Next, the parent of that object is added and so forth for all parents in the chain. This is most useful for implementing browser behavior, notably event handlers. The parents of an event handler, the element it belongs to, the document it is in, and the window it is part of, are all implicitly added in this fashion.

 


CONTINUE FUNCTION