当前位置:网站首页>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));
结果
边栏推荐
猜你喜欢
随机推荐
小程序组件的总结
平行坐标图:高维数据可视化必备图形
word文档里插入图片显示不完整,只显示一半,怎么处理?
U盘拔出时总是提示有程序正在使用?
Make a file upload progress bar
云商店专访 | 云速ERP,小场景里帮企业做“大生意”
Embedded Development: Embedded Fundamentals - Software Error Classification
StarRocks 2.3 新版本特性介绍
西人马重磅发布自研电荷信号调理芯片CU0102B
一线大厂软件测试面试题及答案解析,2022最强版...
Neo4j开源NoSQL数据库
[转帖]为什么收入越来越少?
成都 | 转行软件测试,从零收入到月薪过万,人生迎来新转折...
PIL库和opencv库
C pitfalls and pitfalls
sticky定位后设置right:0不生效
腾讯开源摘星计划培养开源贡献者的实践思考
深投控收购意大利半导体设备商被叫停
Google Cloud X Kyligence|如何从业务视角管理数据湖?
FPGA设计16位二进制全加器模块









