当前位置:网站首页>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));
结果
边栏推荐
猜你喜欢
随机推荐
云商店专访 | 云速ERP,小场景里帮企业做“大生意”
KubeMeet 报名 | 「边缘原生」线上技术沙龙完整议程公布!
UG安装出现Server Start Failed. The Server May Already Be Running!!解决方法
最近很郁闷
Postgresql-xl全局快照代码走读与GTM原理(支线1)
罚款182.28亿元!市场监管总局针对阿里巴巴垄断行为做出行政处罚
Low code of the trilogy
【学习笔记】NOIP模拟赛
Security整各Gateway后配置不生效?
《STL 源码剖析》学习笔记之容器(二)list
字节跳动使用 Flink State 的经验分享
带你入门云开发实践总结篇
EasyNVR更新版本至(V5.3.0)后页面不显示通道配置该如何解决?
centos8安装redis
Android 面试黑洞——当我按下 Home 键再切回来,会发生什么?
AI 通过了图灵测试,科学家反应冷淡:“很棒,但没必要”
Word Embedding与Word2Vec学习
新博客
我用两行代码实现了一个数据库!
滚动条样式