Javascript Function

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 





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.");  
}); 



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章