| comments | Javascript source | Execution result in your current browser |
|---|---|---|
| define a global function | function Rect(w, h) {
this.width=w;
this.height=h
document.write("this="+this);
}
|
|
| call it as static function | Rect(2,3); //call as static function, "this" inside would be global/window
//same this, inside or outside function
document.write("<br/>width="+width);
|
|
| call it as constructor | var sq=new Rect(20,30);//this is object function(Class) instance
document.write("<br/> the value of width="+sq.width);
document.write("<br/> the value of global width="+width);
|