Js基本26:自定义函数
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">什么是函数</span></strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">函数<span style="color: black;">便是</span>一段<span style="color: black;">能够</span>重复执行的代码段。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">咱们</span>把一段相对独立的<span style="color: black;">拥有</span>特定功能的代码块封装起来,形成一个独立实体,起个名字(函数名),在后续<span style="color: black;">研发</span>中<span style="color: black;">能够</span>反复调用。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">函数的<span style="color: black;">功效</span><span style="color: black;">便是</span>封装一段代码,将来<span style="color: black;">能够</span>重复<span style="color: black;">运用</span>。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">固定语法:</strong></p> <span style="color: black;">// 1. 定义函数(函数声明式):</span>
<span style="color: black;">function</span>函数名(){
函数体<span style="color: black;">-</span> 需要重复时候用的代码
}
<span style="color: black;">function</span> <span style="color: black;">tellStory</span>() {
<span style="color: black;">console</span><span style="color: black;">.log</span>(<span style="color: black;">从前有座山,山上有座庙</span>);
<span style="color: black;">console</span><span style="color: black;">.log</span>(<span style="color: black;">庙里有个老和尚和一个小和尚</span>);
<span style="color: black;">console</span><span style="color: black;">.log</span>(<span style="color: black;">老和尚正在给小和尚讲故事</span>);
<span style="color: black;">console</span><span style="color: black;">.log</span>(<span style="color: black;">讲的什么呢?</span>);
}
<span style="color: black;">// <span style="color: black;">重视</span>点:函数里面的代码是不会执行的,需要调用才会执行</span>
<span style="color: black;">// 2. 调用函数(函数调用式):</span>
函数名();
<span style="color: black;">tellStory</span>(); <span style="color: black;">//此时在<span style="color: black;">掌控</span>台中就会输出一个故事</span>
<span style="color: black;">// <span style="color: black;">倘若</span>想输出多次,就<span style="color: black;">能够</span>调用多次这个函数</span>
<span style="color: black;">tellStory</span>();
<span style="color: black;">tellStory</span>();
<span style="color: black;">tellStory</span>();
<span style="color: black;">tellStory</span>();
<span style="color: black;">// 每调用一次,就会执行一次,做了代码的重复<span style="color: black;">运用</span>。</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">函数定义时是不会执行的,<span style="color: black;">仅有</span>调用时才会执行。<span style="color: black;">能够</span><span style="color: black;">运用</span>代码调试工具进行代码执行过程观察。</span></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">2、函数的参数</span></strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">咱们</span><span style="color: black;">期盼</span>在调用函数的时候,<span style="color: black;">能够</span><span style="color: black;">晓得</span>小和尚的名字是什么,<span style="color: black;">况且</span>每次<span style="color: black;">能够</span>给<span style="color: black;">区别</span>的小和尚讲故事。<span style="color: black;">亦</span><span style="color: black;">便是</span>说<span style="color: black;">咱们</span>重复<span style="color: black;">运用</span>的过程里面有会变化的东西,<span style="color: black;">咱们</span><span style="color: black;">运用</span><span style="color: black;"><span style="color: black;">参数</span></span><span style="color: black;">处理</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">语法:</strong></p> <span style="color: black;"><span style="color: black;">function</span> 函数名(<span style="color: black;">参数</span>)</span>{
函数体
}<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">如:</p> <span style="color: black;"><span style="color: black;">function</span> <span style="color: black;">tellStory</span>(<span style="color: black;">name</span>)</span>{
<span style="color: black;">console</span>.log(<span style="color: black;">"从前有座山,山里有座庙"</span>);
<span style="color: black;">console</span>.log(<span style="color: black;">"庙里有个老和尚在给小和尚讲故事"</span>);
<span style="color: black;">console</span>.log(<span style="color: black;">"讲的是什么呢?"</span>);
<span style="color: black;">console</span>.log(<span style="color: black;">"老和尚对"</span>+ name +<span style="color: black;">"说:"</span>);
}<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">便是</span>说参数就<span style="color: black;">好似</span>函数里面的一个变量<span style="color: black;">同样</span>,<span style="color: black;">能够</span>存储一个随时变量的数据</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">当<span style="color: black;">咱们</span>调用函数的时候,再给一个真实的值</p> <span style="color: black;">tellStory</span>(<span style="color: black;">清风</span>);
<span style="color: black;">tellStory</span>(<span style="color: black;">明月</span>);<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">倘若</span><span style="color: black;">咱们</span>想在调用函数的时候,把老和尚的名字<span style="color: black;">亦</span><span style="color: black;">知道</span>一下,<span style="color: black;">亦</span>是<span style="color: black;">能够</span>的</p> <span style="color: black;"><span style="color: black;">function</span> <span style="color: black;">tellStory</span>(<span style="color: black;">name1,name2</span>)</span>{
<span style="color: black;">console</span>.log(<span style="color: black;">"从前有座山,山里有座庙"</span>);
<span style="color: black;">console</span>.log(<span style="color: black;">"庙里有个老和尚在给小和尚讲故事"</span>);
<span style="color: black;">console</span>.log(<span style="color: black;">"讲的是什么呢?"</span>);
<span style="color: black;">console</span>.log(name1 <span style="color: black;">"对"</span>+ name2 +<span style="color: black;">"说:"</span>);
}<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">要调用的时候:</p> <span style="color: black;">tellStory</span>(<span style="color: black;">圆通</span>,<span style="color: black;">清风</span>);<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">总结:</span></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">函数的参数<span style="color: black;">能够</span>一个,<span style="color: black;">亦</span><span style="color: black;">能够</span>是多个,只要在重复的过程中,有多个会产生变化的数据,都<span style="color: black;">能够</span><span style="color: black;">运用</span>参数的方式<span style="color: black;">处理</span></span></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">在定义函数时写的占位用的参数,<span style="color: black;">咱们</span><span style="color: black;">叫作</span>为</span></span><strong style="color: blue;"><span style="color: black;"><span style="color: black;">形参</span></span></strong><span style="color: black;"><span style="color: black;">,在调用函数,<span style="color: black;">实质</span>参与函数执行的参数,<span style="color: black;">咱们</span><span style="color: black;">叫作</span>为</span></span><strong style="color: blue;"><span style="color: black;"><span style="color: black;">实参</span></span></strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">3、函数的返回值</span></strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">函数执行完毕之后会有一个执行结果,该结果就叫 —— 返回值</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">倘若</span>你<span style="color: black;">期盼</span>函数执行完毕之后,有一个你想要的数据,就<span style="color: black;">能够</span><span style="color: black;">运用</span>返回值来实现</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">函数默认<span style="color: black;">状况</span>下,得到的结果是 —— undefined</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">想要修改<span style="color: black;">能够</span>修改,<span style="color: black;">运用</span>return修改</p> <span style="color: black;"><span style="color: black;">function</span> 函数名(<span style="color: black;">参数</span>)</span>{
<span style="color: black;">return</span> 想要的数据
}<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">4、书写函数的<span style="color: black;">过程</span></span></strong></p><span style="color: black;">写出要实现的过程</span><span style="color: black;">分析过程中<span style="color: black;">那些</span>是会变化的,<span style="color: black;">那些</span>是不会变化的</span><span style="color: black;">把变化的<span style="color: black;">做为</span>参数,不变的<span style="color: black;">做为</span>函数体</span><span style="color: black;"><span style="color: black;">根据</span>函数的语法把代码写来</span><span style="color: black;"><span style="color: black;">思虑</span><span style="color: black;">是不是</span>需要修改函数的返回值</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">5、<span style="color: black;">弥补</span>函数</span></strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">函数的声明还<span style="color: black;">能够</span><span style="color: black;">运用</span> :</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">5.1、函数表达式</span></strong></p> <span style="color: black;">var</span> 函数名 = <span style="color: black;"><span style="color: black;">function</span>(<span style="color: black;">参数</span>)</span>{
函数体
}
<span style="color: black;">var</span> getSum = <span style="color: black;"><span style="color: black;">function</span>(<span style="color: black;">a,b</span>)</span>{
<span style="color: black;">return</span> a + b;
}<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">5.2、匿名函数</span></strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">无</span>名字的函数</p> <span style="color: black;"><span style="color: black;">function</span> (<span style="color: black;">参数</span>)</span>{ 函数体 }<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">5.3、自调用函数</span></strong></p> <span style="color: black;">// 立即执行函数</span>
(<span style="color: black;"><span style="color: black;">function</span>(<span style="color: black;">{
<span style="color: black;">//</span> 写<span style="color: black;">有些</span>为了避免变量污染的代码
}</span>))();</span>
页:
[1]