Javascript Learning Function(1)

1.Function defination
they can be created via literals.
They can be assigned to variables, array entries, and properties of other objects.
They can be passed as arguments to functions.
They can be returned as values from functions.
They can possess properties that can be dynamically created and assigned.



2. browser process model
  1. Set up the user interface
  2. Enter a loop waiting for events to occur
  3. Invoke handlers (also called listeners) for those events


3. event type


Browser events, such as when a page is finished loading or when it’s to be
unloaded
Network events, such as responses to an Ajax request
User events, such as mouse clicks, mouse moves, or keypresses
Timer events, such as when a timeout expires or an interval fires


4.browser event process
It’s important to note that the browser event loop is single-threaded. Every event that’s
placed into the event queue is handled in the order that it’s placed onto the queue.


5.sort with comparer
var values = [ 213, 16, 2058, 54, 10, 1965, 57, 9 ];
values.sort(function(value1,value2){ return value2 - value1; });




6.function context
a."global" function will attach to window obj
function f(){}
alert(type of window.f == "function");




b.function name
function f(){};
alert(f.name);

var a = function(){return 1;}
alert(a.name)




window.f = function(){return 1;};
//same with global defination version
alert(f.name);




c.function scope


function outer(){

function inner(){}


alert(typeof inner == "function")
alert(window.inner);//undefined 


}


alert(window.inner);//undefined 
alert(window.outer);




conclusion :

a.named functions are added as properties to window
b.named function f()has a name property that contains the string“f”
c.global variables, even those containing functions, end up on window
d.the anonymous function has a name property consisting of the empty string (though we have not given a name)
e.function have scope



7.function invocation
a. as function
b.as method
c.as constructor
d.apply(),call()

e.arguments


if more than need ->ignore
if not enough,passing undefined
**pass this and arguments these two parameters implicitly
for arguments , can access length property to get length of passing arguments length
**it is an array like object but not array


for "this" parameter:

it refers function context , depends on how it's invoked, can consider as invocation context:


  1. for (global) function call , the invocation context would be window.
  2. for function as method ,the invocation context would be the method owner , code :
function obj(){}
obj.prototype.f = function(){alert(this);}


new obj().f();




3. for constructor
will be the new allocated object

function obj(){this.f=function(){return this;};}
var o1 = new obj();
var o2 = new obj();
alert(o1.f());
alert(o2.f());




4. for call , apply
will be the one we paassed in




function foreach(list,callback){
for(var i = 0;i<list.length;i++)
callback.call(list[i]);
}


foreach([1,2,3,4],function(){alert(this);});




can see, we implicitly passed list[i] as "this" parameter passed in callback


你可能感兴趣的:(JavaScript)