Detailed examples in JS

Polymorphism is very important in object-oriented programming languages. In Java, the polymorphic effect is obtained by inheritance. As:

 Public Abstract Class Animal {Abstract Void Makesound (); // Abstract Method} PUBLIC CLASS Chicken Extends Animal {PUBLIC VOID MAKESOUND () {system.out.println ("Giggle");}} public class duck outs animal {public void makess () {system.out.println ("嘎");}} Animal Duck = new duck (); // (1) Animal Chicken = new chicken (); // (2)   
The polymorphism is actually separated from “What to do” and “Who will do”, to achieve This is the time to eliminate the coupling relationship between the type. In Java, the polymorphism can be achieved by transition. JavaScript variables are variable during operation, and a JS object can represent both Objects that can represent the Duck type, but also objects of the Chicken type, which means that the polymorphism of the JavaScript object is born.
The most fundamental role of polymorphism is to eliminate these conditional branch statements by converting the conditional branch statement of processization into an object.

Suppose we have to write a map application, now there are two optional map API providers for us to access their own applications. At present, we have selected Google Maps, and the SHOW method is provided in the API of Google Maps, which is responsible for displaying the entire map on the page. The sample code is as follows:

VARGoogleMap = {show: function () {console.log (‘Start Rendering Google Map “);}}; var rendermap = function () { ();}; rendermap (); // Out: Start Rendering Google Map Var GoogleMap = {Show: Function () {Console.log (‘Start Rendering Google Map “);}}; var baidumap = {show: function () {console.log (‘ Start Rendering Baidu Map ‘);}} var rendermap = function (type) {if (type === ‘Google’) { ();} else if (type === ‘baidu’) { ();}}; rendermap ‘Google’); // Out: Start Rendering Google Map RenderMap (‘Baidu’); // Out: Start Rendering Baidu Map
  Can be seen, although rendermap functions Currently maintains a certain flexibility, this flexibility is very fragile, once you need to replace it into a map, you must have to change the rendermap function, continue to stack the conditional branch in the inside.  
We still first abstracize the same part of the program, that is, showing a map:

VAR Rendermap = function (map) {IF ( Map.Show InstanceOf Function) { ();}}; rendermap (GoogleMap); // Out: Start Rendering Google Map RenderMap(Baidumap); // Out: Start rendering Baidu map

After we added the support of Tencent map, then we quickly changed this feature, and do not have to Modify the original code:
  VAR TENCENTMAP = {show: function () {console.log ('Start Rendering Tencent Map ");}} rendermap (sosomap); // Output: Start rendering Tencent Map  
The polymorphism is critical, and many design modes are cleverly utilized.

The above is a polymorphistic example in the JS introduced by Xiaobian, I hope to help everyone!
© Copyright Notice
Just support it if you like
comment Grab the couch

Please log in to comment