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.

it("should return 'Hello'", function() {
function returnHello() {
return "Hello";
};
expect(returnHello()).toBe("Hello");
var sayHello = function() {
return "Hello";
}
expect(sayHello()).toBe("Hello");
});
//javascript don't support "Function overloading".
it("should return 'hello javascript world'", function() {
function returnHelloWithName(firstName, lastName) {
return "Hello " + firstName + " " + lastName;
};
expect(returnHelloWithName("javascript", "world")).toBe("Hello javascript world");
});

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.
it("call by value if argument is primitive type", function() {
var hello = "hi";
expect(hello).toBe("hi");
function sayHello(hello) {
hello = "hello";
return hello;
}
sayHello(hello);
expect(hello).toBe("hi");
});
it("call by reference if argument is non-primitive type", function() {
var arayHello = ["hi", "hello", "olleh"];
expect(arayHello[0]).toBe("hi");
function sayHello(arayHello) {
arayHello[0] = "good day";
return arayHello;
}
sayHello(arayHello);
expect(arayHello[0]).toBe("good day");
});

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.

it("arguments", function() {
var func1 = function(a, b) {
return a + " " + b;
}
expect(func1("hi", "there")).toBe("hi there");
expect(func1("hi", "there", "good")).toBe("hi there");
var func2 = function() {
var returnVal = "";
for (i = 0; i < arguments.length; i++) {
returnVal += arguments[i];
}
return returnVal;
}
expect(func2("hi", "there", "good")).toBe("hitheregood");
});

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].

it("closure", function() {
function outside(a) {
return function inside(b) {
return a * b;
}
};
var func = outside(2);
expect(func(4)).toBe(8);
expect(outside(3)(2)).toBe(6);
});
it("complex closure", function() {
var buyItem = function(name) {
var maker;
return {
setName: function(newName) {
name = newName;
},
getName: function() {
return name;
},
getMaker: function() {
return maker;
},
setMaker: function(newMaker) {
maker = newMaker
}
}
}
var item = buyItem("mac");
expect(item.getName()).toBe("mac");
item.setName("mac book");
expect(item.getName()).toBe("mac book");
item.setMaker("apple");
expect(item.getMaker()).toBe("apple");
});

5. Reference 

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

댓글 없음:

댓글 쓰기