语法高亮,滚动条美化,设置页面调整

This commit is contained in:
fofolee
2019-04-19 02:41:09 +08:00
parent 1e8f76c000
commit 359d29ee0b
1590 changed files with 12328 additions and 11441 deletions

View File

@@ -3,9 +3,9 @@
<p><strong><code>function</code></strong> 关键字可以用来在一个表达式中定义一个函数。</p>
<p>你也可以使用 <a href="https://developer.mozilla.orgReference/Global_Objects/Function" title="The Function constructor creates a new Function object. In JavaScript every function is actually a Function object."><code>Function</code></a> 构造函数和一个<a href="Reference/Statements/function">函数声明</a>来定义函数。</p>
<h2 id="Syntax" name="Syntax">语法</h2>
<pre class="syntaxbox">let function_expression = function [<em>name</em>]([<em>param1</em>[, <em>param2[</em>, ..., <em>paramN</em>]]]) {
<pre><code class="language-javascript">let function_expression = function [<em>name</em>]([<em>param1</em>[, <em>param2[</em>, ..., <em>paramN</em>]]]) {
<em>statements</em>
};</pre>
};</code></pre>
<p><a class="new" href="https://developer.mozilla.org/zh-CN/docs/" rel="nofollow">ES2015</a>开始,你也可以使用<a href="https://developer.mozilla.orgReference/Functions/Arrow_functions">箭头函数</a></p>
<h3 id="Parameters" name="Parameters">参数</h3>
<dl>
@@ -20,23 +20,23 @@
<p>函数表达式function expression非常类似于函数声明function statement<span style="line-height: 1.5;">(详情查看</span><a href="Reference/Statements/function" style="line-height: 1.5;">函数声明</a><span style="line-height: 1.5;"></span><span style="line-height: 1.5;">,并且两者拥有几乎相同的语法。</span><span style="line-height: 1.5;">函数表达式与函数声明的最主要区别是函数名称(</span><em>function name</em><span style="line-height: 1.5;">),在函数表达式中可省略它,从而创建匿名函数(</span><em>anonymous</em><span style="line-height: 1.5;"> functions</span>一个函数表达式可以被用作一个IIFEImmediately Invoked Function Expression即时调用的函数表达式它一旦定义就运行。<span style="line-height: 1.5;">更多信息请查看</span><a href="Reference/Functions_and_function_scope" style="line-height: 1.5;">函数</a><span style="line-height: 1.5;"></span></p>
<h3 id="函数表达式提升_(Function_expression_hoisting)"> 函数表达式提升 (Function expression hoisting)</h3>
<p>JavaScript中的函数表达式没有提升,不像函数声明,你在定义函数表达式之前不能使用函数表达式:</p>
<pre class="brush: js"><code><font color="#333333" face="Open Sans, arial, sans-serif" size="3"><span style="background-color: #ffffff; white-space: normal;"> </span></font>notHoisted(); // TypeError: notHoisted is not a function
<pre><code class="language-javascript"><code><font color="#333333" face="Open Sans, arial, sans-serif" size="3"><span style="background-color: #ffffff; white-space: normal;"> </span></font>notHoisted(); // TypeError: notHoisted is not a function
var notHoisted = function() {
console.log('bar');
};</code></pre>
};</code></code></pre>
<h3 id="命名函数表达式Named_function_expression">命名函数表达式Named function expression</h3>
<p>如果你想在函数体内部引用当前函数,则需要创建一个命名函数表达式。<strong>然后函数名称将会(且只会)作为函数体(作用域内)的本地变量</strong>。这样也可以避免使用非标准的 <a href="Reference/Functions_and_function_scope/arguments/callee">arguments.callee</a> 属性。</p>
<pre class="brush: js">var math = {
<pre><code class="language-javascript">var math = {
'factorial': function factorial(n) {
if (n &lt;= 1)
return 1;
return n * factorial(n - 1);
}
};
</pre>
</code></pre>
<p>被函数表达式赋值的那个变量会有一个name属性如果你把这个变量赋值给另一个变量的话这个name属性的值也不会改变。如果函数是一个匿名函数那name属性的值就是被赋值的变量的名称隐藏值。如果函数不是匿名的话那name属性的是就是这个函数的名称显性值。这对于<a href="https://developer.mozilla.orgReference/Functions/Arrow_functions">箭头函数</a>也同样适用箭头函数没有名字所以你只能赋予name属性一个隐性名</p>
<pre class="brush: js"><code>var foo = function() {}
<pre><code class="language-javascript"><code>var foo = function() {}
foo.name // "foo"
var foo2 = foo
@@ -48,16 +48,16 @@ bar.name // "baz"
console.log(foo === foo2); //true
console.log(typeof baz);// undefined
console.log(bar === baz); // false (errors because baz == undefined)</code>
</pre>
</code></pre>
<h2 id="示例">示例</h2>
<p>下面的例子定义了一个匿名函数并把它赋值给变量x。这个函数返回它参数的平方</p>
<pre class="brush: js"><code>var x = function(y) {
<pre><code class="language-javascript"><code>var x = function(y) {
return y * y;
};</code></pre>
};</code></code></pre>
<p>更多情况下被当作<a href="/zh-CN/docs/Mozilla/js-ctypes/Using_js-ctypes/Declaring_and_Using_Callbacks">回调函数</a>使用:</p>
<pre class="brush: js"><code>button.addEventListener('click', function(event) {
<pre><code class="language-javascript"><code>button.addEventListener('click', function(event) {
console.log('button is clicked!')
})</code></pre>
})</code></code></pre>
<h2 id="规范">规范</h2>
<table class="standard-table">
<tbody>