Java Reference
In-Depth Information
function reverse(string) {
var array = string.split("");
return array.join("");
The function does not change the actual value of the string that's provided as an argument,
var message = "Hello";
<< "olleH"
message; // hasn't changed
<< "Hello"
Functions should, as much as possible, only perform a single operation. They can then be
used as building blocks to create other functions that represent more complex abstractions
and extend the functionality further. By only performing a single task the function becomes
more flexible, as it can be used as a building block in many situations rather than be tightly
coupled with one particular operation. It also makes the code more modular as each func-
tion can be improved or replaced as required, without having to interfere with any of the
other functions. This makes it easy to replace one function with another that either im-
proves the behavior, modifies it slightly, or even changes it completely.
As an example, we can use the square() function that we created in Chapter 4 :
function square(x){
return x*x;
This function can then be used to create a hypotenuse() function that returns the length
of the hypotenuse of a right-angled triangle, [4] given the lengths of the other two sides as
function hypotenuse(a,b) {
total = square(a) + square(b);
return Math.sqrt(total);
Search WWH ::

Custom Search