JavaScript Function Apply

Method Reuse With the apply() method, you can write a method that can be used on different objects. The JavaScript apply() Method The apply() method is similar to the call() method (previous chapter). In this example the fullName method of person is applied on person1: Example var person = { fullName: function() { return this.firstName + ” ” + this.lastName; } } var person1 = { firstName: “Mary”, lastName: “Doe”, } person.fullName.apply(person1);  // Will return “Mary Doe” Try it […]

JavaScript Function Call

Method Reuse With the call() method, you can write a method that can be used on different objects. All Functions are Methods In JavaScript all functions are object methods. If a function is not a method of a JavaScript object, it is a function of the global object (see previous chapter). The example below creates an […]

JavaScript Function Invocation

The code inside a JavaScript function will execute when “something” invokes it. Invoking a JavaScript Function The code inside a function is not executed when the function is defined. The code inside a function is executed when the function is invoked. It is common to use the term “call a function” instead of “invoke a function“. It […]

JavaScript Function Definitions

JavaScript functions are defined with the function keyword. You can use a function declaration or a function expression. Function Declarations Earlier in this tutorial, you learned that functions are declared with the following syntax: function functionName(parameters) { code to be executed } Declared functions are not executed immediately. They are “saved for later use”, and will be executed later, when they are invoked (called upon). […]

JavaScript Function Parameters

A JavaScript function does not perform any checking on parameter values (arguments). Function Parameters and Arguments Earlier in this tutorial, you learned that functions can have parameters: functionName(parameter1, parameter2, parameter3) { code to be executed } Function parameters are the names listed in the function definition. Function arguments are the real values passed to (and received by) the function. Parameter Rules JavaScript function definitions […]

JavaScript Closures

JavaScript variables can belong to the local or global scope. Global variables can be made local (private) with closures. Global Variables A function can access all variables defined inside the function, like this: Example function myFunction() { var a = 4; return a * a; } Try it Yourself » But a function can also access variables defined outside the function, like this: Example var a = 4; function myFunction() { […]