Web hosting provider - 1101Chapter 41 .Functions and Custom Objects function if

1101Chapter 41 .Functions and Custom Objects function if your script has only a reference to the function. For example, if your script defines a function via the new Function() constructor (or other anonymous shortcut supported by the browser), you receive a reference to the function as a result of the constructor. To invoke the function later using only that reference (presumably preserved in a global variable), use either the apply()or call() method. Both of these methods achieve the same result, but choosing one method over the other depends on the form in which the function s parameters are conveyed (more about that in a moment). The first parameter of both methods is a reference to the object that the function treats as the current object. For garden-variety functions defined in your script, use the keyword this, which means that the function s context becomes the current object (just like a regular function). In fact, if there are no parameters to be sent to the function, you can omit parameters to both methods altogether. The object reference comes into play when the function being invoked is one that is normally defined as a method to a custom object. (I cover some of these concepts later in this chapter, so you may need to return here after you are familiar with custom objects.) Consider the following code that generates a custom object and assigns a method to the object to display an alert about properties of the object: // function to be invoked as a method from a car object function showCar() { alert(this.make + : + this.color) } // car object constructor function function car(make, color) { this.make = make this.color = color this.show = showCar } // create instance of a car object var myCar = new car( Ford , blue ) The normal way of getting the myCar object to display an alert about its properties is: myCar.show() At that point, the showCar() function runs, picking up the current car object as the context for the thisreferences in the function. In other words, when the showCar() function runs as a method of the object, the function treats the object as the current object. With the call()or apply()methods, however, you don t have to bind the showCar() function to the myCar object. You can omit the statement in the car() constructor that assigns the showCar function to a method name for the object. Instead, a script can invoke the showCar() method and instruct it to treat myCaras the current object: showCar.call(myCar) The showCar() function operates just as before, and the object reference in the call() method s first parameter slot is treated as the current object for the showCar() function. functionObject.apply()
If you are looking for cheap and quality webhost to host and run your website check Jboss Web Hosting services.

Leave a Reply