Javascript Function

来源:互联网 发布:淘宝店怎么改店名 编辑:程序博客网 时间:2024/05/05 09:43

Functions are link to Function.prototype. Function.prototype is linked to Object.prototype.

Since functions are object, they can have property, method, they can be passed to functions.

The missing parameter will have value  undefined

A function always returns a value. If the return value is not specified, then undefined is returned.

every function receives two additional parameters: this and arguments.

Invocation 




http://stackoverflow.com/questions/9674252/javascript-why-this-inside-the-private-function-refers-to-the-global-scope?rq=1


pattern of invocation:

the method invocation pattern - When a function is stored as a property of an object, we call it a method, this is bounded to the object

the function invocation pattern - When a function is NOT the property of an object. this is bounded to global object, ( window in DOM ). THIS IS A DESIGN MISTAKE, this should be bounded to ccurrent context instead

the constructor invocation pattern - this binds to newly created object

and the apply invocation pattern





Inner Functions

REMEMBER: inner function has no access to this and arguments
inner functions are created and disposed every time a function is called. In the example below, bar should be moved outside of foo. The same applies to anonymous function. 

function foo(a, b) {      function bar() {          return a + b;      }        return bar();  }    foo(1, 2);  

document.addEventListener("click", function(evt) {      alert("You clicked the page.");  }); 



原创粉丝点击