Creating your own objects is just part of the fun in ECMAScript. How would you like to modify the behavior of existing objects? This is completely possible in ECMAScript, so dream up whatever methods you’d like for a String,Array, Number, or any other object,because the possibilities are endless. Remember the prototype object from an earlier section in this chapter? You already know that each constructor has a prototype property that can be used to define methods.What you don’t already know is that each of the native objects in ECMAScript also has a prototype property that can be used in the exactly same way.
Creating a new method
You can define a new method for any existing class by using its prototype property, just as you would with your own classes. For instance, remember the toString() method of Number that outputs a hexadecimal string if you pass in 16 as an argument? Wouldn’t it be nicer to have a toHexString() method to handle the process? It’s simple to create it:
In this context,the this keyword points to the instance of Number and so has full access to all the Number methods.With this code, it is possible to do this:
Because the number 15 is equal to hexadecimal F, the alert displays “F”. And remember the discussion about using an Array as a queue? The only thing missing was properly named methods. You can add enqueue() and dequeue() to Array and just have them call the existing push() and shift() methods respectively:
You can also, of course, add methods that don’t rely on existing methods at all.For example, say that you want to determine the position of a particular item in an array. You have no native method to do such a thing. You can easily create a method that does this:
This method, named indexOf() to keep it consistent with the String method of the same name, searches each item in the array until it finds the equivalent of the item passed in. If the item is found, the method returns the position; if not, the method returns –1. With this defined,the following code is possible:
Lastly, if you want to add a new method to every native object in ECMAScript, you must define it on the Object’s prototype property. As discussed in the last chapter, all native objects inherit from Object, and so any changes to Object are reflected in all native objects. For example, if you want to add a method that outputs the current value of the object in an alert, you do the following:
Here, both the String and Number objects inherit the showValue() method from Object, displaying “hello” and “25” when called on their respective objects.
Redefining an existing method
Just as it is possible to define new methods for existing classes, it is also possible to redefine existing methods. As discussed in the previous chapter, function names are simply pointers to functions, and as such, can be easily changed to point to other functions. What happens if you change a native method, such as toString()?The previous code is perfectly legal and works as expected:
You may recall from Chapter 2 that the Function’s toString() method normally outputs the source code of the function.By overriding that method, you can supply a different string to return (in this case, “Function code hidden”).But what happened to the original function that toString() was pointing to? Well, it has gone on to the garbage collector because it was fully dereferenced. You have no way to get that original function back, which is why it is always safer to store a pointer to the original method that you are overriding, just in case you need it later. You may even want to call that original method under certain circumstances in your new method:
In this code, the first line saves a reference to the current toString() method in a property called originalToString. Then, the toString() method is overridden with a custom method. This new method checks to see if the length of the function source code is longer greater than 100. If so, the method returns a small error message stating that the function code is too long; otherwise, it return the source code by calling originalToString().
Very late binding
Technically speaking, there is no such thing as very late binding. The term is used in this book to describe a phenomenon in ECMAScript where it is possible to define a method for a type of object after the object has already been instantiated. For example:
In most programming languages, you must define object methods well in advance of object instantiation. Here, the sayHi() method is added to the Object class after an instance has been created. Not only is that unheard of in traditional languages, but the instance of Object is then automatically assigned the method and it can be used immediately (on the following line).
Java Script Related Tutorials
|Adv Java Tutorial||Core Java Tutorial|
|Java-Springs Tutorial||Java Servlets Tutorial|
|EJB(Enterprise JavaBeans) Tutorial||JavaServer Faces (JSF) Tutorial|
|Java Swing Tutorial||Java Tutorial|
|JavaMail API Tutorial||Java 8 Tutorial|
|Java XML Tutorial||Java Bean Utils Tutorial|
|The Java Debugger (JDB) Tutorial||Java.math Package Tutorial|
Java Script Related Interview Questions
|Java Script Interview Questions||Adv Java Interview Questions|
|Core Java Interview Questions||Java-Springs Interview Questions|
|Java Servlets Interview Questions||EJB(Enterprise JavaBeans) Interview Questions|
|JavaServer Faces (JSF) Interview Questions||Java Swing Interview Questions|
|Java 8 Interview Questions||Java XML Interview Questions|
|JavaFX Interview Questions||The Java Debugger (JDB) Interview Questions|
Java Script Related Practice Tests
|Java Script Practice Tests||Adv Java Practice Tests|
|Core Java Practice Tests||Java-Springs Practice Tests|
|Java Servlets Practice Tests||EJB(Enterprise JavaBeans) Practice Tests|
|JavaServer Faces (JSF) Practice Tests||Java 8 Practice Tests|
All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.