-
Notifications
You must be signed in to change notification settings - Fork 11
Intro to JavaScript
first you define a function. as you do it an empty your_function.prototype object is created like “{}”.
later when you use same function to create a new object using the new operator.
every thing that is in .prototype overrides the function properties and assigned to the newly created object as in inherited functions.
usually the current this variable is referenced to the last object created with “new”
when going into a sub function this resets to this variable of the module (this of the script file)
when you do var x={}
it means var x= new Object();
when you do var x=[]
it means var x= new Array();
// notice you create object with new operator
technique of “loosing” this to – wrap some code inside an anonymous function:
function server() {
function test(){
/// this. // this *this* is of server funvction object
(function(){ this. })() // wrapper function - this is the trick. // that *this* is of module
}
}
it also creates a new variable scope.
more on that: http://www.adequatelygood.com/2010/2/JavaScript-Scoping-and-Hoisting
you see here callback inside a callback. a privet variable passed by reference.
function server() {
function add( data , callback ) {
var that=this;
that.beforeadd( data , function (){
that.doadd( data , function (){
that.afteradd( data , function (){
if(callback)callback(); } ); } ); } );
}}
var x;
this.x=x; // now x is is public
func.call(new_this,arg1,arg2)
or func.apply(new_this,[arg1,arg2])
in general it allows to call a function as it was a function of an object.
sys.puts('this of palce1 ');for( x in this) sys.puts(x); sys.puts('/this of palce1 ');
to make async code you just use callback at the end, as the add function above. and or add it as an event as in picard below
http://helephant.com/2008/08/how-javascript-objects-work/
http://github.com/dantebronto/picard – see async example to learn what is async. async usualy is only about not blocking when transferring data
http://www.robertsosinski.com/2009/04/28/binding-scope-in-javascript/ – - .apply() and .call()
http://www.packtpub.com/article/using-prototype-property-in-javascript — prototype
http://mckoss.com/jscript/object.htm – - oop in javascript
http://ajax.org – - a real oop javascript framework you can learn more from it about objects and exported functions