Home >Web Front-end >JS Tutorial >What is polymorphism in Javascript language

What is polymorphism in Javascript language

小云云
小云云Original
2018-01-29 13:45:562445browse

The Java language is a statically typed language. Due to strict type checking when compiling the code, variables cannot be assigned values ​​of different types. This type checking will make the code rigid, but Java also The effect of polymorphism can be obtained through inheritance (implementation inheritance and interface inheritance). Polymorphism can be achieved by upward transformation.

The actual meaning of polymorphism is that the same operation, acting on different objects, can produce different interpretations and different execution results. The variable type of JavaScript is variable at runtime, which means that the polymorphism of JavaScript objects is inherent.

The following picture explains it:

The master gives instructions to "chicken and duck" at the same time. The chicken and duck croak at the same time, but their The cry is different.
What is polymorphism in Javascript language

We use code to demonstrate this example:

In traditional code, we are used to using if else to stack hard-coded code, but if this object Keeping it up will result in continuous addition of code. It is always dangerous to modify the code. The more places you modify, the greater the possibility of program errors. And when there are more types of animals, it will cause if else It is piled up into a huge function, which is not conducive to maintenance.

var makeSound = function(animal) {
     if (animal instanceof Dog) {
        console.log('wanwan');
     } else if(animal instanceof Cat) {
        console.log('miaomiao');
     }
}

var Dog = function() {}
var Cat = function() {}
makeSound(new Dog());
makeSound(new Cat());

The modified code is as follows:

 var makeSound = function(animal) {
     animal.sound();
}
var Dog = function() {}
Dog.prototype.sound = function(){
   console.log('wanwan');
}

var Cat = function() {}
Cat.prototype.sound = function() {
   console.log('miaomiao');
}
makeSound(new Dog());
makeSound(new Cat());

How to understand that if the object cannot respond to a request, it will delegate the request to the prototype of its constructor. This principle is prototypal inheritance The essence of , here is a piece of code:

var obj = {name:'lin'};

var A = function() {};

A .prototype = obj;

var a = new A();
console.log(a.name);

Explanation: Traversing all the attributes of object a, name was not found Properties
The request to find the name property is delegated to the prototype of object a's constructor, which is recorded by a._proto_ and points to A.prototype,
and A.prototype is set to object obj
in Find the name attribute in object obj and return it.

Related recommendations:

How to learn JavaScript language? What are the important and difficult points in learning?

JavaScript language structure notes (1)_Basic knowledge

Detailed explanation of the basic grammatical requirements of JavaScript language_Basic knowledge


The above is the detailed content of What is polymorphism in Javascript language. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn