Home > Web Front-end > Front-end Q&A > Does javascript support polymorphism?

Does javascript support polymorphism?

WBOY
Release: 2022-07-28 17:11:40
Original
2120 people have browsed it

JavaScript supports polymorphism; polymorphism refers to providing a unified interface for entities of different data types. Polymorphic types can apply the operations they support to other types of values. In JavaScript Polymorphism means that the same operation can produce different interpretations and execution results on different objects.

Does javascript support polymorphism?

The operating environment of this tutorial: Windows 10 system, JavaScript version 1.8.5, Dell G3 computer.

javascript supports polymorphism

In programming languages ​​and type theory, polymorphism (English: polymorphism) refers to providing a unified interface for entities of different data types. Polymorphic types (English: polymorphic types) can apply the operations they support to values ​​of other types.

Meaning

The same operation acts on different objects, which can produce different interpretations and different execution results

For example, I raised A cat and a dog. If I give them the same command "bark", the cat will meow and the dog will bark. So asking them to bark is the same operation, but different barks are different executions. result.

For example

The director said that when the action starts, each actor will do what he should do,

instead of the director one by one Tell each actor what to do

The most fundamental benefit of polymorphism is that you no longer have to ask the object "What type are you?"

and then call certain functions of the object based on the answer. A behavior

You just need to call the behavior, and all other polymorphic mechanisms will be arranged for you

// 多态背后的思想是将 做什么 和 谁去做 分离
// 也就是将不变的事物 与 可能改变的事物 分离出来
// 先来一段反例的多态代码
var makeSound = function (animal) {
if (animal instanceof Duck) {
console.log('嘎嘎嘎');
} else if (animal instanceof Chicken) {
console.log('咯咯咯');
}
}
var Duck = function () { };
var Chicken = function () { };
makeSound(new Duck());
makeSound(new Chicken());
// 此时如果加一个狗,还需要再去改源代码
Copy after login

Example

// 首先把不变的部分隔离出来
var makeSound = function (animal) {
animal.sound();
}
//然后把可变的部分各自封装起来,
var Duck = function () { }
Duck.prototype.sound = function () {
console.log("嘎嘎嘎");
}
var Chicken = function () { }
Chicken.prototype.sound = function () {
console.log("咯咯咯");
}
makeSound(new Duck());
makeSound(new Chicken());
//如果有一天改需求了,加一个动物
//简单追加一条代码就可以啦
var Dog = function () { }
Dog.prototype.sound = function () {
console.log("汪汪汪");
}
makeSound(new Dog());
Copy after login

Actual case

// 实际案例
var googleMap = {
show: function () {
console.log('开始渲染谷歌地图');
}
}
var baiduMap = {
show: function () {
console.log('开始渲染百度地图');
}
}
var renderMap = function (map) {
if (map.show instanceof Function) {
map.show();
}
}
renderMap(googleMap)
Copy after login

[Related recommendations: javascript video tutorial, web front-end

The above is the detailed content of Does javascript support polymorphism?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template