小小的病毒 發表於 2019-12-28 14:27:00

js (JavaScript)函数声明的几种形式及用法

<h2>1、函数声明</h2>
<p>语法:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> functionName(parameters) {
执行的代码
}</span></pre>
</div>
<p>函数声明后不会立即执行,只是在初始化的时候会将函数声明提升,会在我们需要的时候调用到。</p>
<h2>2、函数表达式(匿名函数)</h2>
<p>语法:</p>
<div class="cnblogs_code">
<pre>var x = function (a, b) {return a * b};<br>var z = x(4, 3);</pre>
</div>
<p>以上函数实际上是一个&nbsp;<strong>匿名函数</strong>&nbsp;(函数没有名称)。</p>
<p>函数存储在变量中,不需要函数名称,通常通过变量名来调用。</p>
<h2>3、Function() 构造函数</h2>
<p>通过内置的 JavaScript 函数构造器(Function())定义。</p>
<p>语法:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">var</span> myFunction = <span style="color: rgba(0, 0, 255, 1)">new</span> Function("a", "b", "return a * b"<span style="color: rgba(0, 0, 0, 1)">);

</span><span style="color: rgba(0, 0, 255, 1)">var</span> x = myFunction(4, 3);</pre>
</div>
<p>如果函数调用前使用了&nbsp;<strong>new</strong>&nbsp;关键字, 则是调用了构造函数。</p>
<p>这看起来就像创建了新的函数,但实际上 JavaScript 函数是重新创建的对象:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 构造函数:</span>
<span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> myFunction(arg1, arg2) {
    </span><span style="color: rgba(0, 0, 255, 1)">this</span>.firstName =<span style="color: rgba(0, 0, 0, 1)"> arg1;
    </span><span style="color: rgba(0, 0, 255, 1)">this</span>.lastName=<span style="color: rgba(0, 0, 0, 1)"> arg2;
}

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> This    creates a new object</span>
<span style="color: rgba(0, 0, 255, 1)">var</span> x = <span style="color: rgba(0, 0, 255, 1)">new</span> myFunction("John","Doe"<span style="color: rgba(0, 0, 0, 1)">);
x.firstName;                           </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 返回 "John"</span></pre>
</div>
<p>构造函数的调用会创建一个新的对象。新对象会继承构造函数的属性和方法。</p>
<p><strong>构造函数中&nbsp;this&nbsp;关键字没有任何的值。 this&nbsp;的值在函数调用实例化对象(new object)时创建。</strong></p>
<p>&nbsp;</p>
<p>实际上,你不必使用构造函数。上面实例可以写成:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">var</span> myFunction = <span style="color: rgba(0, 0, 255, 1)">function</span> (a, b) {<span style="color: rgba(0, 0, 255, 1)">return</span> a *<span style="color: rgba(0, 0, 0, 1)"> b};

</span><span style="color: rgba(0, 0, 255, 1)">var</span> x = myFunction(4, 3);</pre>
</div>
<h2>4、自调用函数</h2>
<p>函数表达式可以 "自调用"。</p>
<p>自调用表达式会自动调用。</p>
<p>如果表达式后面紧跟 () ,则会自动调用。</p>
<p>不能自调用声明的函数。</p>
<p>通过添加括号,来说明它是一个函数表达式:</p>
<div class="cnblogs_code">
<pre>(<span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> () {
    </span><span style="color: rgba(0, 0, 255, 1)">var</span> x = "Hello!!";      <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 我将调用自己</span>
<span style="color: rgba(0, 0, 0, 1)">})();

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">为一个匿名自调用函数</span></pre>
</div>
<h2>5、箭头函数</h2>
<p>ES6 新增了箭头函数。</p>
<p>箭头函数表达式的语法比普通函数表达式更简洁。</p>
<p>&nbsp;</p>
<p>语法:</p>
<div class="cnblogs_code">
<pre>(参数1, 参数2, …, 参数N) =&gt;<span style="color: rgba(0, 0, 0, 1)"> { 函数声明 }

(参数1, 参数2, …, 参数N) </span>=&gt;<span style="color: rgba(0, 0, 0, 1)"> 表达式(单一)
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 相当于:(参数1, 参数2, …, 参数N) =&gt;{ return 表达式; }</span></pre>
</div>
<p>当只有一个参数时,圆括号是可选的:</p>
<div class="cnblogs_code">
<pre>(单一参数) =&gt;<span style="color: rgba(0, 0, 0, 1)"> {函数声明}
单一参数 </span>=&gt; {函数声明}</pre>
</div>
<p>没有参数的函数应该写成一对圆括号:</p>
<div class="cnblogs_code">
<pre>() =&gt; {函数声明}</pre>
</div>
<p>例:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> ES5</span>
<span style="color: rgba(0, 0, 255, 1)">var</span> x = <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(x, y) {
   </span><span style="color: rgba(0, 0, 255, 1)">return</span> x *<span style="color: rgba(0, 0, 0, 1)"> y;
}

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> ES6</span>
const x = (x, y) =&gt; x * y;</pre>
</div>
<p>有的箭头函数都没有自己的&nbsp;<strong>this</strong>。 不适合顶一个&nbsp;<strong>对象的方法</strong>。</p>
<p>当我们使用箭头函数的时候,箭头函数会默认帮我们绑定外层 this 的值,所以在箭头函数中 this 的值和外层的 this 是一样的。</p>
<p>箭头函数是不能提升的,所以需要在使用之前定义。</p>
<p>使用&nbsp;<strong>const</strong>&nbsp;比使用&nbsp;<strong>var</strong>&nbsp;更安全,因为函数表达式始终是一个常量。</p>
<p>如果函数部分只是一个语句,则可以省略 return 关键字和大括号 {},这样做是一个比较好的习惯:</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<h2>6、使用函数注意点</h2>
<p>(1)、函数提升</p>
<ul>
<li>在之前的教程中我们已经了解了 "hoisting(提升)"。</li>
<li>提升(Hoisting)是 JavaScript 默认将当前作用域提升到前面去的的行为。</li>
<li>提升(Hoisting)应用在变量的声明与函数的声明。</li>
<li>因此,函数可以在声明之前调用:</li>
</ul>
<div class="cnblogs_code">
<pre>myFunction(5<span style="color: rgba(0, 0, 0, 1)">);

</span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> myFunction(y) {
    </span><span style="color: rgba(0, 0, 255, 1)">return</span> y *<span style="color: rgba(0, 0, 0, 1)"> y;
}</span></pre>
</div>
<p><strong>注意</strong>:使用表达式定义函数时无法提升。</p>
<p>(2)、函数式对象</p>
<ul>
<li>在 JavaScript 中使用&nbsp;<strong>typeof</strong>&nbsp;操作符判断函数类型将返回 "function" 。</li>
<li>但是JavaScript 函数描述为一个对象更加准确。</li>
<li>JavaScript 函数有&nbsp;<strong>属性</strong>&nbsp;和&nbsp;<strong>方法</strong>。</li>
<li>arguments.length 属性返回函数调用过程接收到的参数个数:</li>
</ul>
<div class="cnblogs_Highlighter">
<pre class="brush:csharp;gutter:true;">function myFunction(a, b) {    //2
    return arguments.length;
}
</pre>
</div>
<p>  toString() 方法将函数作为一个字符串返回:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> myFunction(a, b) {
    </span><span style="color: rgba(0, 0, 255, 1)">return</span> a *<span style="color: rgba(0, 0, 0, 1)"> b;
}

</span><span style="color: rgba(0, 0, 255, 1)">var</span> txt = myFunction.toString();   <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">function myFunction(a, b) { return a * b; }</span></pre>
</div><br><br>
来源:https://www.cnblogs.com/art-poet/p/12111592.html
頁: [1]
查看完整版本: js (JavaScript)函数声明的几种形式及用法