answersLogoWhite

0

Functions are one of the absolute best aspects of Javascript (though the topic can be confusing at points). There are essentially three types of function definitions:

variable function declaration

factorial(5); // error

var factorial = function(target){

if (target === 0) {

return 1;

} else {

return target * factorial( target - 1 );

}

}

factorial(5); // 120

notes on usage

calling a "variable" function such as this before it is declared will result in an error as these functions are not "hoisted" (they aren't moved to the top of stack during compilation to ensure they are globally available). This is ideal for scope management of functions.

traditional function declaration

sayhi('Jack'); // Hello Jack

function sayhi(name){

return "Hello " + name;

}

sayhi('Jake'); // Hello Jake

notes on usage

mostly the same as above, except the scope will automatically be global and this will be hoisted

anonymous functions

// immediate invocation style

(function(msg){

return 'Shake hands, shake hands, ' + msg;

})('Shake Shake Shake');

// deferred invocation (demonstrated as callback), first example is not an anomyous function but the function that will accept a function as a parameter

var shake_hands = function(callback){

return "Shake hands, shake hands, " + callback();

}

// anonymous function passed as argument to shake_hands function

shake_hands(function(msg){

return 'Shake hands, shake hands, ' + msg;

});

notes on usage

anonymous functions are typically used as callbacks (functions that are executed once another function has completed). They can be invoked immediately or deferred

Calling functions

There are 3 acceptable ways to call or "use" functions in javascript

Traditional:

factorial(5);

Providing context (setting what the keyword 'this' will refer to in the function):

factorial.call(null, 5);

// apply is just like call, but accepts an array of arguments to the function

factorial.apply(null, [5]);

User Avatar

Wiki User

12y ago

What else can I help you with?

Related Questions

How do you use settime out in php?

we cant use set timeout function in php because it is of java script function


Does Java Script use the same compiler as Java?

No


How do you script a RuneScape bot?

RuneScape bots use Java. Use a Java Binary Code.


What is function in Java Script?

function in the scripting language can give an attribute to something to make it preform a task that you want it to handle.


What is java fx2.1.1?

Java script


Is Java script needed for Poptropica?

No, Java script is not needed to play poptropica.


What can javascript do?

Java script can be used to automate simple tasks that may be repetitive to computer users. You can also use java script in web pages to have interaction with the user.


Do you need to learn Java script to be able to use it?

No. Java Script is huge and learning all of it is impossible. Just try and code the basics and later get to the harder stuff


What command do you use to display a message in java script?

document.write("hello world")


How can we use script tag in java?

to implement javascript embed the statements between <script> n </script>,,, and include any scripting language. eg:<script LANGUAGE="javascript"> block of codes </script>


Where can I download java script free?

When it comes to java script you can simply download it free from the java website. You can also go to CNET downloads and get it free from there.


Does java script coding mean using it for Java programs only?

Java script coding is computer coding, many things require Java, and you need to have Java enabled to view these types of programs.