当前位置:网站首页>function arguments
function arguments
2022-07-29 19:34:00 【A Shan classmate.】
函数的参数
基础
函数是可以带参数的
函数:The most basic packaging method,The purpose is to reuse methods
封装:Pack some loose pieces together to form a whole
复用:重复使用
Functions can be written in various ways depending on the number of formal parameters
For example, encapsulate a method that returns hours, minutes and seconds
<body>
<!-- 函数是可以带参数的 -->
<!-- 函数:The most basic purpose of encapsulation is to reuse methods -->
<script> // Get the current date and print it function showData(){
var now =new Date(); var hour=now.getHours(); var minute=now.getMinutes(); var second=now.getSeconds(); return `${
hour}:${
minute}:${
second}` } //The call prints the return value console.log(showData()); </script>
</body>

具体情况如上:Put a whole bunch of code in {}里面,形成一个整体,然后起一个名字,Then call it by the function name{}里面的代码
Functions can take parameters
单参数
** 制作一个abs函数,返回参数的绝对值**
<script> // 制作一个abs函数,返回参数的绝对值 // 如果》0Just return a positive numbernum本生 // 如果>0It's a negative numbernumbecomes positive and returns function abs(x){
if(x > 0) return x; if(x < 0){
return x*=-1 } return num>0 ? x : -num; } console.log(abs(-90)); </script>

多参数
制作一个abs函数,Returns the sum between the two
<script>; // --------分割-------- // // Multiple parameters are separated by commas function add(x,y){
return x + y; } add(1,2) </script>
函数的arguments
arguments:函数中隐式自带的变量,When the stored function is called,parameter received
适合场景:Cooperate to implement those parameters whose incoming length is not fixed
Create a function that adds all the arguments passed in
function add() {
var min = 0;
for (i = 0; i < arguments.length; i++) {
min += arguments[i];
}
return min;
}
console.log(add(1, 2, 3, 4));
console.log(add(1, 2, 3, 4, 5, 6, 7, 8));
console.log(add(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12));
结果
边栏推荐
猜你喜欢

LL(1),LR(0),SLR(1),LALR(1),LR(1)对比与分析

Canal实现Mysql数据增量同步更新至Mysql/Redis

一次挖矿程序的清理(回忆版)

效率技巧│十分钟学会 xmind 思维导图的使用

First-line big factory software test interview questions and answer analysis, the strongest version of 2022...

FP6601QS6 SOT-23-6 USB专用充电端口控制器 QC2.0/3.0快充协议IC

成都 | 转行软件测试,从零收入到月薪过万,人生迎来新转折...

招聘|字节跳动云原生计算,期待你的加入

centos7服务器安全策略

手机银行体验性测试:如何获取用户真实感受
随机推荐
关于Image scaleType的属性详解,以及每一个属性的区别
R语言时间序列数据提取:使用xts包的first函数提取时间序列中最前面一个月的数据(first 1 month)
罚款182.28亿元!市场监管总局针对阿里巴巴垄断行为做出行政处罚
亿级用户背后的字节跳动云原生计算最佳实践
答对这3个面试问题,薪资直涨20K
SK海力士工厂发生氢氟酸泄漏,3名工人受伤!官方称不会影响生产
我用两行代码实现了一个数据库!
北汇信息继续扩大V2X测试服务,扎根重庆,服务全国
工作9年了!
无人驾驶技术有什么优点,人工驾驶的优缺点英文
Zadig 环境负载均衡:0 人工干预,极速部署
MarkBERT
R语言时间序列数据提取:使用xts包的last函数提取时间序列中最后面10天的数据(last 10 day)
实现一个可调节大小的 Switch 开关
R语言ggplot2可视化绘制条形图(bar plot)、使用gghighlight包突出高亮条形图中的特定条形(highlight specific bar plot)
Go 语言如何读取 excel 测试数据,简单易学
EasyNVR更新版本至(V5.3.0)后页面不显示通道配置该如何解决?
第21章 内存管理
Neo4j开源NoSQL数据库
transformer通俗理解