当前位置:网站首页>How to implement overloading in JS
How to implement overloading in JS
2022-06-12 20:35:00 【Stand on the bridge and bake cold noodles】
What is overload ?
Same function name , Multiple functions with different parameter lists , It can be called according to the different values of the arguments passed in , Execute different logic .
benefits : Reduce the number of function names , Lighten the burden on the caller
How to achieve it ?
stay JS Two functions with the same name are forbidden to exist at the same time , If they exist at the same time , The functions defined later will override the previous functions ,
Can use arguments To solve .
What is? arguments?
Function , You don't have to define it , It is an array of class objects .
What is a class array ? Looks like an array , With subscript , Yes length attribute , It can also cycle . The difference is that the array is Array Object of type , Class array is Object object . Class arrays do not have permission to use array methods
How to overload ?
- Define only one function , No parameters are required
- Inside the function , Judge arguments The length of , Element value , Element type and other conditions , Different logic is executed according to different actual parameter values
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <script> function pay( ){ // arguments[ ].length // 0 1... if(arguments.length==0){ console.log(` Mobile payment ...`) }else if(arguments.length==1){ console.log(` cash payment , Take you ${arguments[0]} element `) }else{ if(arguments[1]=="123456"){ console.log(` Pay by credit card , From your card number ${arguments[0]} Deduction successful !`) }else{ console.log(` Pay by credit card , Incorrect password `); } } } pay(); pay(100); pay("6553 1234","123456"); pay("6553 1234","12345"); </script> </body> </html> Running results : Mobile payment ... cash payment , Take you 100 element Pay by credit card , From your card number 6553 1234 Deduction successful ! Pay by credit card , Incorrect password
边栏推荐
- How to determine the sample size of an inspection lot in SAP QM's initial sampling strategy?
- Before job hopping, Jin San made up the interview questions. Jin San successfully landed at Tencent and got a 30K test offer
- Troubleshooting of service port failure
- Event distribution mechanism of view
- Halcon angle and radian interchange
- A simple understanding of consistent hash
- Restful API 接口规范
- How can CTCM in the inspection lot system status of SAP QM be eliminated?
- 服务端口不通排查
- Detailed explanation of SQL exists usage
猜你喜欢

同时做测试,别人已经年薪20w起,为什么你还在为达到月薪10k而努力?

It has been engaged in the functional test of 10K to the test development of 40W annual salary for 5 years, and spent 7 days sorting out the super comprehensive learning route

一致性哈希的简单认识

Introduction to system mode development of rouya wechat mall

Halcon angle and radian interchange

跳槽前恶补面试题,金三成功上岸腾讯,拿到30k的测开offer

牛客網:三數之和

设计规则检查约束(set_max_transition、set_max_capacitance)

Centos7 installing PHP

Properties to YML
随机推荐
【生成对抗网络学习 其三】BiGAN论文阅读笔记及其原理理解
MySQL + PostgreSQL batch insert update insertorupdate
Unauthorized rce in VMware vCenter
Solve the cvxpy error the solver GLPK_ MI is not installed
初步了解认识正则表达式(Regex)
SAP QM preliminary - cannot assign a sampling policy to an inspection characteristic when maintaining an inspection plan by executing transaction code qp02?
Algorinote_2_主定理与 Akra-Bazzi 定理
Operating instructions for installing mysql5.7 in centos7
QT knowledge: QT widgets widget class [01]
At the same time, do the test. Others have been paid 20W a year. Why are you still working hard to reach 10K a month?
Transaction code qs28 of SAP QM preliminary level
synchronized下的 i+=2 和 i++ i++执行结果居然不一样
[generation confrontation network learning III] reading notes of Bigan paper and its principle understanding
How to determine fragment restored from Backstack
Handwritten promise
Successful transition from self-study test halfway, 10K for the first test
Generate API documents using swagger (go language example)
A simple understanding of consistent hash
JS深浅拷贝
Lightroom 大使系列:用 Meg Loeks 捕捉怀旧之情