Home | | Internet & World Wide Web HOW TO PROGRAM | | Internet Programming | | Web Programming | References and Reference Parameters - JavaScript(JS)

Chapter: Internet & World Wide Web HOW TO PROGRAM - The Ajax Client - JavaScript: Arrays

| Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail |

References and Reference Parameters - JavaScript(JS)

Two ways to pass arguments to functions (or methods) in many programming languages are pass-by-value and pass-by-reference.

References and Reference Parameters

 

Two ways to pass arguments to functions (or methods) in many programming languages are pass-by-value and pass-by-reference. When an argument is passed to a function by val-ue, a copy of the argument’s value is made and is passed to the called function. In Java-Script, numbers, boolean values and strings are passed to functions by value.

 

With pass-by-reference, the caller gives the called function direct access to the caller’s data and allows it to modify the data if it so chooses. This procedure is accomplished by passing to the called function the actual location in memory (also called the address) where the data resides. Pass-by-reference can improve performance because it can elimi-nate the overhead of copying large amounts of data, but it can weaken security because the called function can access the caller’s data. In JavaScript, all objects (and thus all Arrays) are passed to functions by reference.

To pass a reference to an object into a function, simply specify the reference name in the function call. Normally, the reference name is the identifier that the program uses to manipulate the object. Mentioning the reference by its parameter name in the body of the called function actually refers to the original object in memory, and the original object can be accessed directly by the called function.

 

Arrays are objects in JavaScript, so Arrays are passed to a function by reference—a called function can access the elements of the caller’s original Arrays. The name of an array actually is a reference to an object that contains the array elements and the length variable, which indicates the number of elements in the array. In the next section, we demonstrate pass-by-value and pass-by-reference, using arrays.

 

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail


Copyright © 2018-2020 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.