JavaScript functions

TODO: this sections contains the escense but has a bad english. it should be rewritten more elegantly.

The first problem that we face when working with native javascript is that , in javascript, we work with functions and the java language doesn't support such a concept or nothing similar. In java, the only beahaviour language concepts we have are instance methods and static methods. Mixing native javascript functions and java code without realizing what we really are doing, can cause very nasty bugs and problems, and so in this section we will examine possible problems and advices about how to do it right.

function context:

what is a function context? In javascript, any function can be evaluated using any object as its context. The context of a function when evaluated is the object referenced by the "this" keyword.

Important: Javascript function can be called with an arbitrary context object while Java instance methods must always have a fixed context.

since in java we always have to use an object, the function in java have a fixed context (the this object). But in javascript this is not true and we must ensure the context. for example, the following example for registering a mouse click listener in document.body will work, but it is incorrect! :


Runnable r = new Runnable(){
  public void run() {
    System.out.println("clicked");
  }
};
/**
 * @j2sNative
 * document.body.onclick=r.run; //WRONG!
 */{}
    

Why this is wrong? When an html document is clicked, and a function is registered for listen to clicks with "element.onclick", the function will be evaluated using the clicked html element as the function context. This means that any reference to "this" in the function body, will point to the target html element and not where it should be, to the Runnable instance. Let examine an example that fails because of this:

With java static methods there is no problem because they do no context (no this pointer). But it is not useful to work with static methods for