2015년 8월 30일 일요일

Javascript Function

Let's see javascript function and its usage. Learning test is written using Jasmine. Please refer Jasmine usage which I wrote before if you have interest it.


1. Define function and its usage


Define function in javascript is nothing special. Let's see [line 8] closely. The function can be assigned to variable. It means that function can be a argument of another function just like a variable. It is also possible call like [line 11]. It this case, The name of variable which have a function as a value would be a the name of function.


2. call by value, call by reference

If the argument is primitive type, call by value is used. If the argument is non-primitive type like array or object, call by reference is used.

3. Arguments

Javascript don't support function oveloading. See [line 6]. You can know the reason. If more arguments are used than defined when call function, the other argument which are not defined wouldn't be used. You can know what values are used as a argument using "arguments" object. Refer "func2" in following example.


4. Closure

Javascript have the concept of closure. The "inside" function is defined in "outside" and "outside" function return the "inside" function itself. Variable "a" which is argument of "outside" can be referred in "inside" function.  You can call it like [line 9] and [line 10].


5. Reference 

- MDN - https://developer.mozilla.org/en/docs/Web/JavaScript
- Jasmine - http://jasmine.github.io/2.3/introduction.html

댓글 없음:

댓글 쓰기