User:Feixie/JavaScript

Concept
+--- self/parent/history/navigator/screen/top +--- location current window --+--- Frames[] +--- document --+--- Forms +--- Table + ...
 * Objects (browser objects/document objects)
 * Structure

Book

 * Learning Javescript 2ed

Timers

 * setTimeout/clearTimeout
 * setInterval/clearInterval

Function
fuction(param1, param2...){ } var func = new Function("param1", "param2", "return param1 * param2;"); var func2 = function (params) { statements; } //function literal, not dynamic, parsed once. var ret = func(param1, param2); function outer{ var outMem; function inner{ var inMem; } }
 * Declarative function/static function
 * Anonymous function/Dynamic function. like perl eval statement.
 * Nested Function/Function Closure
 * inner can access outMem, while outer can't access inMem. If outer returns inner to calling app, a chain of inner-outer will be reserved. so that both won't be reclaimed. This is closure in JS.

OO
function Person(name){   // Person is an object (object constructor function) this.name = name;      // when new Person is invoked, it will 1) create a general object;2)set __proto__ as Person.prototype; // 3) run code in Person function to set local variables } Person.prototype.age = "0"; // Change the prototype of Person constructor object                              //(which will affect all objects use it as prototype). function Programmer(language){     this.language = language; } Programmer.prototype = new Person;  // create a new Person object from Person constructer/prototype                                     //and assign it to Programmer's prototype var p = new Programmer("Javascript"); Person.prototype.age = 18;          // Will affect all objects who use Person object as prototype Person.prototype.name = "xxx";      // this won't affect p, bcz p's prototype (a Person obj) has its own name field. assert(p.name = null);              // Name resolution: 1) find in local names; 2) if not found, find in its prototype assert(p.age == 18); function Base{} function Deriv{} alert(Deriv.prototype.constructor == Deriv); var d1 = new Deriv; alert(d1.constructor == Deriv); alert(Base.prototype.constructor == Base); alert(Deriv.prototype.constructor == Deriv); alert(d1.__proto__ == Deriv.prototype); var oldDerivProto = Deriv.prototype; Deriv.prototype = new Base; alert(d1.constructor == Deriv); alert(d1.constructor.prototype == Deriv.prototype); alert(d1.__proto__ == oldDerivProto); var d2 = new Deriv; alert(d2.constructor == Base); alert(Deriv.prototype.constructor == Base); alert(d2.constructor.prototype == Base.prototype); Deriv.prototype.constructor = Deriv; alert(Deriv.prototype.constructor == Deriv);
 * Object Model
 * Some concepts
 * Fuction: all functions are objects. invoking function with a keyword now in front of it leads to a new instance of the function object.
 * Object: all object are funcions. they have constructors behaves like functions
 * Instance: instantiated from function prefixed with new or object constructor.
 * Encapsulation
 * this represents the instance of an object in its constructor. sort of public member.
 * var is sort of private member of the instance

function Base(bd){ this.bd = bd; var pri_d = "private data"; this.bFunc = function{return this.bd + pri_d;} } function Derived(dd){ this.dd = dd; Base.call(this, dd + "-"); this.dFunc = function{return this.bFunc + this.dd;} } Derived.prototype = new Base;   //populdate Base's prototype (by calling Base's constructor) //to Derived's prototype. Derived derives all members of Base. var deriv = new Derived("aaaa");  //Invoke Derived's constructor to construct Base's member and its own members alert(deriv.dFunc); //- //calling sequences: //Base //Derived // Base(...) var oneOff = { member1 : "value1", getMem : function{return this.member1;} } john = new Array; john.firstName = "John" john["lastName"] = "Smith" john.age = 22 mary = { firstName: "Mary", lastName: "Jones", age: 21 }; john.gender = "male" mary["gender"] = "female" try{ }catch(e){ if (e instanceof Type){ alert(e.message); } }finally{ }
 * Inheritance
 * apply/call. Apply members of an object to another object. it is called in containing object's constructor and takes containing object's instance (this) as first argument followed by arguments (passing to contained object's constructor).
 * prototype. add an instance of contained object to containing object to chain contained object's constructor to containing object.
 * One-off object
 * A compromise between no global variables/functions and many global variables/functions
 * Objects and Associative Arrays
 * Objects can be thought of as associative arrays
 * Error Handling