diff options
author | Irvin <irvinfly@gmail.com> | 2022-02-16 02:07:31 +0800 |
---|---|---|
committer | Irvin <irvinfly@gmail.com> | 2022-02-16 02:35:54 +0800 |
commit | d9e9adb5f80a819fe46349bcf6d1faec734b09cd (patch) | |
tree | e856dc5e31aad0a7d5e8c2f98c9ee139d9569a86 /files/zh-cn/web/javascript/guide | |
parent | 7c2556fe79c44d7c31108b8c8b048d2b7704a95e (diff) | |
download | translated-content-d9e9adb5f80a819fe46349bcf6d1faec734b09cd.tar.gz translated-content-d9e9adb5f80a819fe46349bcf6d1faec734b09cd.tar.bz2 translated-content-d9e9adb5f80a819fe46349bcf6d1faec734b09cd.zip |
remove span tag in zh-CN
Diffstat (limited to 'files/zh-cn/web/javascript/guide')
10 files changed, 104 insertions, 104 deletions
diff --git a/files/zh-cn/web/javascript/guide/control_flow_and_error_handling/index.html b/files/zh-cn/web/javascript/guide/control_flow_and_error_handling/index.html index b2fe41baef..08bbb7ae5b 100644 --- a/files/zh-cn/web/javascript/guide/control_flow_and_error_handling/index.html +++ b/files/zh-cn/web/javascript/guide/control_flow_and_error_handling/index.html @@ -7,7 +7,7 @@ translation_of: Web/JavaScript/Guide/Control_flow_and_error_handling --- <p>{{jsSidebar("JavaScript Guide")}}{{PreviousNext("Web/JavaScript/Guide/Grammar_and_Types", "Web/JavaScript/Guide/Loops_and_iteration")}}</p> -<p class="summary">JavaScript 提供一套灵活的语句集,<span class="notranslate">特别是控制流语句</span>,你可以<span class="notranslate">用它</span>在你的应用程序中实现大量的交互性功能。本章节我们将带来关于JavaScript语句的一些概览。</p> +<p class="summary">JavaScript 提供一套灵活的语句集,特别是控制流语句,你可以用它在你的应用程序中实现大量的交互性功能。本章节我们将带来关于JavaScript语句的一些概览。</p> <p> 这一章中的语句,在 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements">JavaScript参考</a> 中包含更为详尽的细节。在 JavaScript 代码中,分号(;)字符被用来分割语句。</p> @@ -132,9 +132,9 @@ alert(x); // 输出的结果为 2 <p>请不要混淆原始的布尔值<code>true</code>和<code>false</code> 与 {{jsxref("Boolean")}}对象的真和假。例如:</p> -<pre class="brush: js"><code class="language-js"><span class="keyword token">var</span> b <span class="operator token">=</span> <span class="keyword token">new</span> <span class="class-name token">Boolean</span><span class="punctuation token">(</span><span class="keyword token">false</span><span class="punctuation token">)</span><span class="punctuation token">;</span> -<span class="keyword token">if</span> <span class="punctuation token">(</span>b<span class="punctuation token">)</span> <span class="comment token">//结果视为真</span> -<span class="keyword token">if</span> <span class="punctuation token">(</span>b <span class="operator token">==</span> <span class="keyword token">true</span><span class="punctuation token">)</span> <span class="comment token">// 结果视为假</span></code></pre> +<pre class="brush: js"><code class="language-js">var b = new Boolean(false); +if (b) //结果视为真 +if (b == true) // 结果视为假</code></pre> <h4 id="示例_2"><strong>示例</strong></h4> diff --git a/files/zh-cn/web/javascript/guide/expressions_and_operators/index.html b/files/zh-cn/web/javascript/guide/expressions_and_operators/index.html index 62d4b97ab5..c89cc966e5 100644 --- a/files/zh-cn/web/javascript/guide/expressions_and_operators/index.html +++ b/files/zh-cn/web/javascript/guide/expressions_and_operators/index.html @@ -136,22 +136,22 @@ translation_of: Web/JavaScript/Guide/Expressions_and_Operators <p>对于更复杂的赋值,<a href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment">解构赋值</a>语法是一个能从数组或对象对应的数组结构或对象字面量里提取数据的 Javascript 表达式。</p> -<pre class="brush: js line-numbers language-js notranslate"><code class="language-js"><span class="keyword token">var</span> foo <span class="operator token">=</span> <span class="punctuation token">[</span><span class="string token">"one"</span><span class="punctuation token">,</span> <span class="string token">"two"</span><span class="punctuation token">,</span> <span class="string token">"three"</span><span class="punctuation token">]</span><span class="punctuation token">;</span> +<pre class="brush: js line-numbers language-js notranslate"><code class="language-js">var foo = ["one", "two", "three"]; -<span class="comment token">// 不使用解构</span> -<span class="keyword token">var</span> one <span class="operator token">=</span> foo<span class="punctuation token">[</span><span class="number token">0</span><span class="punctuation token">]</span><span class="punctuation token">;</span> -<span class="keyword token">var</span> two <span class="operator token">=</span> foo<span class="punctuation token">[</span><span class="number token">1</span><span class="punctuation token">]</span><span class="punctuation token">;</span> -<span class="keyword token">var</span> three <span class="operator token">=</span> foo<span class="punctuation token">[</span><span class="number token">2</span><span class="punctuation token">]</span><span class="punctuation token">;</span> +// 不使用解构 +var one = foo[0]; +var two = foo[1]; +var three = foo[2]; -<span class="comment token">// 使用解构</span> -<span class="keyword token">var</span> <span class="punctuation token">[</span>one<span class="punctuation token">,</span> two<span class="punctuation token">,</span> three<span class="punctuation token">]</span> <span class="operator token">=</span> foo<span class="punctuation token">;</span></code></pre> +// 使用解构 +var [one, two, three] = foo;</code></pre> <h3 id="比较运算符">比较运算符</h3> <p><a href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/Comparison_Operators">比较运算符</a>比较它的操作数并返回一个基于表达式是否为真的逻辑值。操作数可以是数字,字符串,逻辑,对象值。字符串比较是基于标准的字典顺序,使用Unicode值。在多数情况下,如果两个操作数不是相同的类型, JavaScript 会尝试转换它们为恰当的类型来比较。这种行为通常发生在数字作为操作数的比较。类型转换的例外是使用 <code>===</code> 和 <code>!==</code> 操作符,它们会执行严格的相等和不相等比较。这些运算符不会在检查相等之前转换操作数的类型。下面的表格描述了该示例代码中的各比较运算符</p> -<pre class="brush: js line-numbers language-js notranslate"><code class="language-js"><span class="keyword token">var</span> var1 <span class="operator token">=</span> <span class="number token">3; -</span>var var2 <span class="operator token">=</span> <span class="number token">4</span><span class="punctuation token">;</span></code></pre> +<pre class="brush: js line-numbers language-js notranslate"><code class="language-js">var var1 = 3; +var var2 = 4;</code></pre> <table class="standard-table"> <caption>比较运算符</caption> @@ -222,7 +222,7 @@ translation_of: Web/JavaScript/Guide/Expressions_and_Operators <h3 id="算术运算符">算术运算符</h3> -<p>算术运算符使用数值(字面量或者变量)作为操作数并返回一个数值.标准的算术运算符就是加减乘除(+ - * /)。当操作数是浮点数时,这些运算符表现得跟它们在大多数编程语言中一样(特殊要注意的是,除零会产生{{jsxref("Infinity")}})。<span style="line-height: 1.5;">例如:</span></p> +<p>算术运算符使用数值(字面量或者变量)作为操作数并返回一个数值.标准的算术运算符就是加减乘除(+ - * /)。当操作数是浮点数时,这些运算符表现得跟它们在大多数编程语言中一样(特殊要注意的是,除零会产生{{jsxref("Infinity")}})。例如:</p> <pre class="brush: js notranslate">1 / 2; // 0.5 1 / 2 == 1.0 / 2.0; // true @@ -432,7 +432,7 @@ After: 10100000000000000110000000000001</code></pre> <h4 id="Bitwise_Shift_Operators" name="Bitwise_Shift_Operators">移位运算符</h4> -<p><span style="line-height: 1.5;">移位运算符带两个操作数:</span>第一个是待移位的数,第二个是指定第一个数要被移多少位的数。移位的方向由运算符来控制.</p> +<p>移位运算符带两个操作数:第一个是待移位的数,第二个是指定第一个数要被移多少位的数。移位的方向由运算符来控制.</p> <p>移位运算符把操作数转为32bit整数,然后得出一个与待移位数相同种类的值。</p> @@ -551,7 +551,7 @@ var n3 = !"Cat"; // !t returns false <p>例如,</p> -<pre class="brush: js line-numbers language-js notranslate"><code class="language-js">console<span class="punctuation token">.</span><span class="function token">log</span><span class="punctuation token">(</span><span class="string token">"my "</span> <span class="operator token">+</span> <span class="string token">"string"</span><span class="punctuation token">)</span><span class="punctuation token">;</span> <span class="comment token">// console logs the string "my string".</span></code></pre> +<pre class="brush: js line-numbers language-js notranslate"><code class="language-js">console.log("my " + "string"); // console logs the string "my string".</code></pre> <p>简写操作符 <code>+=</code> 也可以用来拼接字符串,例如:</p> @@ -719,12 +719,12 @@ void expression <p>如下创建了一个超链接文本,当用户单击该文本时,不会有任何效果。</p> -<pre class="brush: html line-numbers language-html notranslate"><code class="language-html"><span class="tag token"><span class="tag token"><span class="punctuation token"><</span>a</span> <span class="attr-name token">href</span><span class="attr-value token"><span class="punctuation token">=</span><span class="punctuation token">"</span>javascript:void(0)<span class="punctuation token">"</span></span><span class="punctuation token">></span></span>Click here to do nothing<span class="tag token"><span class="tag token"><span class="punctuation token"></</span>a</span><span class="punctuation token">></span></span></code></pre> +<pre class="brush: html line-numbers language-html notranslate"><code class="language-html"><a href="javascript:void(0)">Click here to do nothing</a></code></pre> <p>下面的代码创建了一个超链接,当用户单击它时,提交一个表单。</p> -<pre class="brush: html line-numbers language-html notranslate"><code class="language-html"><span class="tag token"><span class="tag token"><span class="punctuation token"><</span>a</span> <span class="attr-name token">href</span><span class="attr-value token"><span class="punctuation token">=</span><span class="punctuation token">"</span>javascript:void(document.form.submit())<span class="punctuation token">"</span></span><span class="punctuation token">></span></span> -Click here to submit<span class="tag token"><span class="tag token"><span class="punctuation token"></</span>a</span><span class="punctuation token">></span></span></code></pre> +<pre class="brush: html line-numbers language-html notranslate"><code class="language-html"><a href="javascript:void(document.form.submit())"> +Click here to submit</a></code></pre> <h3 id="关系操作符">关系操作符</h3> @@ -913,20 +913,20 @@ this.propertyName <p>分组操作符()控制了表达式中计算的优先级. 举例来说, 你可以改变先乘除后加减的顺序,转而先计算加法。</p> -<pre class="brush:js line-numbers language-js notranslate"><code class="language-js"><span class="keyword token">var</span> a <span class="operator token">=</span> <span class="number token">1</span><span class="punctuation token">;</span> -<span class="keyword token">var</span> b <span class="operator token">=</span> <span class="number token">2</span><span class="punctuation token">;</span> -<span class="keyword token">var</span> c <span class="operator token">=</span> <span class="number token">3</span><span class="punctuation token">;</span> +<pre class="brush:js line-numbers language-js notranslate"><code class="language-js">var a = 1; +var b = 2; +var c = 3; -<span class="comment token">// 默认优先级</span> -a <span class="operator token">+</span> b <span class="operator token">*</span> c <span class="comment token">// 7</span> -<span class="comment token">// 默认是这样计算的</span> -a <span class="operator token">+</span> <span class="punctuation token">(</span>b <span class="operator token">*</span> c<span class="punctuation token">)</span> <span class="comment token">// 7</span> +// 默认优先级 +a + b * c // 7 +// 默认是这样计算的 +a + (b * c) // 7 -<span class="comment token">// 现在使加法优先于乘法</span> -<span class="punctuation token">(</span>a <span class="operator token">+</span> b<span class="punctuation token">)</span> <span class="operator token">*</span> c <span class="comment token">// 9</span> +// 现在使加法优先于乘法 +(a + b) * c // 9 -<span class="comment token">// 这等价于</span> -a <span class="operator token">*</span> c <span class="operator token">+</span> b <span class="operator token">*</span> c <span class="comment token">// 9</span></code></pre> +// 这等价于 +a * c + b * c // 9</code></pre> <h5 id="数值推导">数值推导</h5> @@ -941,12 +941,12 @@ a <span class="operator token">*</span> c <span class="operator token">+</span> <p>Comprehensions特性被许多编程语言所采用,该特性能够使你快速地通过一个已有的数组来创建出一个新的数组,比如:</p> -<pre class="brush:js line-numbers language-js notranslate"><code class="language-js"><span class="punctuation token">[</span><span class="keyword token">for</span> <span class="punctuation token">(</span>i <span class="keyword token">of</span> <span class="punctuation token">[</span> <span class="number token">1</span><span class="punctuation token">,</span> <span class="number token">2</span><span class="punctuation token">,</span> <span class="number token">3</span> <span class="punctuation token">]</span><span class="punctuation token">)</span> i<span class="operator token">*</span>i <span class="punctuation token">]</span><span class="punctuation token">;</span> -<span class="comment token">// [ 1, 4, 9 ]</span> +<pre class="brush:js line-numbers language-js notranslate"><code class="language-js">[for (i of [ 1, 2, 3 ]) i*i ]; +// [ 1, 4, 9 ] -<span class="keyword token">var</span> abc <span class="operator token">=</span> <span class="punctuation token">[</span> <span class="string token">"A"</span><span class="punctuation token">,</span> <span class="string token">"B"</span><span class="punctuation token">,</span> <span class="string token">"C"</span> <span class="punctuation token">]</span><span class="punctuation token">;</span> -<span class="punctuation token">[</span><span class="keyword token">for</span> <span class="punctuation token">(</span>letters <span class="keyword token">of</span> abc<span class="punctuation token">)</span> letters<span class="punctuation token">.</span><span class="function token">toLowerCase</span><span class="punctuation token">(</span><span class="punctuation token">)</span><span class="punctuation token">]</span><span class="punctuation token">;</span> -<span class="comment token">// [ "a", "b", "c" ]</span></code></pre> +var abc = [ "A", "B", "C" ]; +[for (letters of abc) letters.toLowerCase()]; +// [ "a", "b", "c" ]</code></pre> <h3 id="左值表达式">左值表达式</h3> diff --git a/files/zh-cn/web/javascript/guide/functions/index.html b/files/zh-cn/web/javascript/guide/functions/index.html index bf26ec3c8e..3d43d61305 100644 --- a/files/zh-cn/web/javascript/guide/functions/index.html +++ b/files/zh-cn/web/javascript/guide/functions/index.html @@ -468,15 +468,15 @@ getCode(); // Returns the secret code <p>例如,设想有一个用来连接字符串的函数。唯一事先确定的参数是在连接后的字符串中用来分隔各个连接部分的字符(译注:比如例子里的分号“;”)。该函数定义如下:</p> -<pre class="brush: js line-numbers language-js notranslate"><code class="language-js"><span class="keyword token">function</span> <span class="function token">myConcat</span><span class="punctuation token">(</span>separator<span class="punctuation token">)</span> <span class="punctuation token">{</span> - <span class="keyword token">var</span> result <span class="operator token">=</span> <span class="string token">''</span><span class="punctuation token">;</span> <span class="comment token">// 把值初始化成一个字符串,这样就可以用来保存字符串了!!</span> - <span class="keyword token">var</span> i<span class="punctuation token">;</span> - <span class="comment token">// iterate through arguments</span> - <span class="keyword token">for</span> <span class="punctuation token">(</span>i <span class="operator token">=</span> <span class="number token">1</span><span class="punctuation token">;</span> i <span class="operator token"><</span> arguments<span class="punctuation token">.</span>length<span class="punctuation token">;</span> i<span class="operator token">++</span><span class="punctuation token">)</span> <span class="punctuation token">{</span> - result <span class="operator token">+</span><span class="operator token">=</span> arguments<span class="punctuation token">[</span>i<span class="punctuation token">]</span> <span class="operator token">+</span> separator<span class="punctuation token">;</span> - <span class="punctuation token">}</span> - <span class="keyword token">return</span> result<span class="punctuation token">;</span> -<span class="punctuation token">}</span></code></pre> +<pre class="brush: js line-numbers language-js notranslate"><code class="language-js">function myConcat(separator) { + var result = ''; // 把值初始化成一个字符串,这样就可以用来保存字符串了!! + var i; + // iterate through arguments + for (i = 1; i < arguments.length; i++) { + result += arguments[i] + separator; + } + return result; +}</code></pre> <p>你可以给这个函数传递任意数量的参数,它会将各个参数连接成一个字符串“列表”:</p> diff --git a/files/zh-cn/web/javascript/guide/grammar_and_types/index.html b/files/zh-cn/web/javascript/guide/grammar_and_types/index.html index 8b00401fcc..9a3aa594c3 100644 --- a/files/zh-cn/web/javascript/guide/grammar_and_types/index.html +++ b/files/zh-cn/web/javascript/guide/grammar_and_types/index.html @@ -109,7 +109,7 @@ if(input === undefined){ } </pre> -<p><code><span class="comment">The following is related to "Variables" section as potential values in assignment.</span>undefined</code> 值在布尔类型环境中会被当作 <code>false</code> 。例如,下面的代码将会执行函数 <code>myFunction</code>,因为数组 <code>myArray</code> 中的元素未被赋值:</p> +<p><code>The following is related to "Variables" section as potential values in assignment.undefined</code> 值在布尔类型环境中会被当作 <code>false</code> 。例如,下面的代码将会执行函数 <code>myFunction</code>,因为数组 <code>myArray</code> 中的元素未被赋值:</p> <pre class="brush: js notranslate">var myArray = []; if (!myArray[0]) myFunction(); @@ -148,7 +148,7 @@ console.log(y); // ReferenceError: y 没有被声明</pre> <h3 id="变量提升">变量提升</h3> -<p>JavaScript 变量的另一个不同寻常的地方是,你可以先使用变量稍后再声明变量而不会引发异常。这一概念称为变量提升;JavaScript 变量感觉上是被“提升”或移到了函数或语句的最前面。但是,提升后的变量将返回 <span style="font-family: courier new,andale mono,monospace;">undefined</span> 值。因此在使用或引用某个变量之后进行声明和初始化操作,这个被提升的变量仍将返回 undefined 值。</p> +<p>JavaScript 变量的另一个不同寻常的地方是,你可以先使用变量稍后再声明变量而不会引发异常。这一概念称为变量提升;JavaScript 变量感觉上是被“提升”或移到了函数或语句的最前面。但是,提升后的变量将返回 undefined 值。因此在使用或引用某个变量之后进行声明和初始化操作,这个被提升的变量仍将返回 undefined 值。</p> <pre class="brush: js notranslate">/** * 例子1 @@ -219,7 +219,7 @@ var baz = function() { <h3 id="全局变量">全局变量</h3> -<p><span class="comment">need links to pages discussing scope chains and the global object</span> 实际上,全局变量是<em>全局对象</em>的属性。在网页中,(译注:缺省的)全局对象是 <code>window</code> ,所以你可以用形如 <code>window.</code><em><code>variable</code> </em>的语法来设置和访问全局变量。</p> +<p>need links to pages discussing scope chains and the global object 实际上,全局变量是<em>全局对象</em>的属性。在网页中,(译注:缺省的)全局对象是 <code>window</code> ,所以你可以用形如 <code>window.</code><em><code>variable</code> </em>的语法来设置和访问全局变量。</p> <p>因此,你可以通过指定 window 或 frame 的名字,在当前 window 或 frame 访问另一个 window 或 frame 中声明的变量。例如,在文档里声明一个叫 <code>phoneNumber</code> 的变量,那么你就可以在子框架里使用 <code>parent.phoneNumber</code> 的方式来引用它。</p> @@ -402,7 +402,7 @@ console.log(a[0]); // 3</pre> <p>布尔类型有两种字面量:<code>true</code>和<code>false</code>。</p> -<p>不要混淆作为布尔对象的真和假与布尔类型的原始值true和false。<span class="short_text" id="result_box" lang="zh-CN"><span>布尔对象是原始</span><span>布尔</span><span>数据类型的一个包装器</span><span>。参见</span></span><span class="short_text" id="result_box" lang="zh-CN"><span><a href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Boolean" title="zh-CN/docs/JavaScript/Guide/Predefined Core Objects#Boolean Object"> 布尔对象</a>。</span></span></p> +<p>不要混淆作为布尔对象的真和假与布尔类型的原始值true和false。布尔对象是原始布尔数据类型的一个包装器。参见<a href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Boolean" title="zh-CN/docs/JavaScript/Guide/Predefined Core Objects#Boolean Object"> 布尔对象</a>。</p> <h3 id="整数_Integers">整数 (Integers)</h3> @@ -411,10 +411,10 @@ console.log(a[0]); // 3</pre> <p>整数可以用十进制(基数为10)、十六进制(基数为16)、八进制(基数为8)以及二进制(基数为2)表示。</p> <ul> - <li><span class="short_text" id="result_box" lang="zh-CN"><span>十进制整数字面量由一串数字序列组成</span><span>,且没有前缀</span><span>0</span><span>。</span></span></li> - <li><span class="short_text" id="result_box" lang="zh-CN"><span>八进制的整数以 0(或</span></span>0O、0o)开头,<span class="short_text" lang="zh-CN"><span>只能包括</span><span>数字0-7</span><span>。</span></span></li> - <li><span class="short_text" id="result_box" lang="zh-CN"><span>十六进制整数以0x(或0X)开头,</span><span>可以包含</span><span>数字(0-9)</span><span>和</span><span>字母 a~f 或 A~</span><span>F</span><span>。</span></span></li> - <li><span class="short_text" id="result_box" lang="zh-CN"><span>二进制整数以0b(或0B)开头,只能包含数字</span><span>0和1。</span></span></li> + <li>十进制整数字面量由一串数字序列组成,且没有前缀0。</li> + <li>八进制的整数以 0(或0O、0o)开头,只能包括数字0-7。</li> + <li>十六进制整数以0x(或0X)开头,可以包含数字(0-9)和字母 a~f 或 A~F。</li> + <li>二进制整数以0b(或0B)开头,只能包含数字0和1。</li> </ul> <p>严格模式下,八进制整数字面量必须以0o或0O开头,而不能以0开头。</p> @@ -431,7 +431,7 @@ console.log(a[0]); // 3</pre> <p>浮点数字面值可以有以下的组成部分:</p> <ul> - <li><span class="short_text" id="result_box" lang="zh-CN"><span>一个十进制</span><span>整数,可以</span><span>带正负号(</span><span>即前缀</span><span class="atn">“+”</span><span class="atn">或“ - ”</span><span>)</span><span>,</span></span></li> + <li>一个十进制整数,可以带正负号(即前缀“+”或“ - ”),</li> <li>小数点(“.”),</li> <li>小数部分(由一串十进制数表示),</li> <li>指数部分。</li> @@ -495,19 +495,19 @@ console.log(unusualPropertyNames["!"]); // Bang!</pre> <p>在ES2015,对象字面值扩展支持在创建时设置原型,简写了 foo: foo 形式的属性赋值,方法定义,支持父方法调用,以及使用表达式动态计算属性名。总之,这些也使对象字面值和类声明更加紧密地联系起来,让基于对象的设计从这些便利中更加受益。</p> -<pre class="brush: js line-numbers language-js notranslate"><code class="language-js"><span class="keyword token">var</span> obj <span class="operator token">=</span> <span class="punctuation token">{</span> - <span class="comment token">// __proto__</span> - __proto__<span class="punctuation token">:</span> theProtoObj<span class="punctuation token">,</span> - <span class="comment token">// Shorthand for ‘handler: handler’</span> - handler<span class="punctuation token">,</span> - <span class="comment token">// Methods</span> - <span class="function token">toString</span><span class="punctuation token">(</span><span class="punctuation token">)</span> <span class="punctuation token">{</span> - <span class="comment token">// Super calls</span> - <span class="keyword token">return</span> <span class="string token">"d "</span> <span class="operator token">+</span> <span class="keyword token">super</span><span class="punctuation token">.</span><span class="function token">toString</span><span class="punctuation token">(</span><span class="punctuation token">)</span><span class="punctuation token">;</span> - <span class="punctuation token">}</span><span class="punctuation token">,</span> - <span class="comment token">// Computed (dynamic) property names</span> - <span class="punctuation token">[</span> <span class="string token">'prop_'</span> <span class="operator token">+</span> <span class="punctuation token">(</span><span class="punctuation token">(</span><span class="punctuation token">)</span> <span class="operator token">=</span><span class="operator token">></span> <span class="number token">42</span><span class="punctuation token">)</span><span class="punctuation token">(</span><span class="punctuation token">)</span> <span class="punctuation token">]</span><span class="punctuation token">:</span> <span class="number token">42</span> -<span class="punctuation token">}</span><span class="punctuation token">;</span></code></pre> +<pre class="brush: js line-numbers language-js notranslate"><code class="language-js">var obj = { + // __proto__ + __proto__: theProtoObj, + // Shorthand for ‘handler: handler’ + handler, + // Methods + toString() { + // Super calls + return "d " + super.toString(); + }, + // Computed (dynamic) property names + [ 'prop_' + (() => 42)() ]: 42 +};</code></pre> <p>请注意:</p> @@ -523,7 +523,7 @@ console.log(foo["2"]); // two</pre> <p>一个正则表达式是字符被斜线(译注:正斜杠“/”)围成的表达式。下面是一个正则表达式文字的一个例子。</p> -<pre class="brush: js line-numbers language-js notranslate"><code class="language-js"><span class="keyword token">var</span> re <span class="operator token">=</span> <span class="regex token">/ab+c/</span><span class="punctuation token">;</span></code></pre> +<pre class="brush: js line-numbers language-js notranslate"><code class="language-js">var re = /ab+c/;</code></pre> <h3 id="字符串字面量_String_literals">字符串字面量 (String literals)</h3> @@ -669,7 +669,7 @@ console.log(quote); is broken \ across multiple\ lines." -<span class="objectBox objectBox-text">console.log(str);</span> // <span class="objectBox objectBox-text">this string is broken across multiplelines.</span> +console.log(str); // this string is broken across multiplelines. </pre> <p>Javascript没有“heredoc”语法,但可以用行末的换行符转义和转义的换行来近似实现 </p> diff --git a/files/zh-cn/web/javascript/guide/index.html b/files/zh-cn/web/javascript/guide/index.html index d1ae6683bd..eb266e6a2b 100644 --- a/files/zh-cn/web/javascript/guide/index.html +++ b/files/zh-cn/web/javascript/guide/index.html @@ -15,7 +15,7 @@ translation_of: Web/JavaScript/Guide <p>本指南分为以下章节:</p> <ul class="card-grid"> - <li><span><a href="/zh-CN/docs/Web/JavaScript/Guide/Introduction">介绍</a></span> + <li><a href="/zh-CN/docs/Web/JavaScript/Guide/Introduction">介绍</a> <p><a href="/zh-CN/docs/Web/JavaScript/Guide/Introduction#What_is_JavaScript.3F">关于这份指南</a><br> <a href="/zh-CN/docs/Web/JavaScript/Guide/Introduction#什么是_JavaScript">关于 JavaScript</a><br> @@ -24,7 +24,7 @@ translation_of: Web/JavaScript/Guide <a href="/zh-CN/docs/Web/JavaScript/Guide/Introduction#Getting_started_with_JavaScript">工具</a><br> <a href="/zh-CN/docs/Web/JavaScript/Guide/Introduction#Hello_world">Hello World</a></p> </li> - <li><span><a href="/zh-CN/docs/Web/JavaScript/Guide/Grammar_and_types">语法与数据类型</a></span> + <li><a href="/zh-CN/docs/Web/JavaScript/Guide/Grammar_and_types">语法与数据类型</a> <p><a href="/zh-CN/docs/Web/JavaScript/Guide/Grammar_and_types#基础">基本语法与注释</a><br> <a href="/zh-CN/docs/Web/JavaScript/Guide/Grammar_and_types#声明">声明</a><br> <a href="/zh-CN/docs/Web/JavaScript/Guide/Grammar_and_types#变量的作用域">变量作用域</a><br> @@ -32,13 +32,13 @@ translation_of: Web/JavaScript/Guide <a href="/zh-CN/docs/Web/JavaScript/Guide/Grammar_and_types#数据结构和类型">数据结构和类型</a><br> <a href="/zh-CN/docs/Web/JavaScript/Guide/Grammar_and_types#字面量_(Literals)">字面量</a></p> </li> - <li><span><a href="/zh-CN/docs/Web/JavaScript/Guide/Control_flow_and_error_handling">控制流与错误处理</a></span> + <li><a href="/zh-CN/docs/Web/JavaScript/Guide/Control_flow_and_error_handling">控制流与错误处理</a> <p><a href="/zh-CN/docs/Web/JavaScript/Guide/Control_flow_and_error_handling#if...else_语句"><code>if...else</code></a><br> <a href="/zh-CN/docs/Web/JavaScript/Guide/Control_flow_and_error_handling#switch_语句"><code>switch</code></a><br> <a href="/zh-CN/docs/Web/JavaScript/Guide/Control_flow_and_error_handling#异常处理语句"><code>try</code>/<code>catch</code>/<code>throw</code></a><br> <a href="/zh-CN/docs/Web/JavaScript/Guide/Control_flow_and_error_handling#使用Error对象">Error 对象</a></p> </li> - <li><span><a href="/zh-CN/docs/Web/JavaScript/Guide/Loops_and_iteration">循环与迭代</a></span> + <li><a href="/zh-CN/docs/Web/JavaScript/Guide/Loops_and_iteration">循环与迭代</a> <p><a href="/zh-CN/docs/Web/JavaScript/Guide/Loops_and_iteration#for_语句"><code>for</code></a><br> <a href="/zh-CN/docs/Web/JavaScript/Guide/Loops_and_iteration#while_语句"><code>while</code></a><br> <a href="/zh-CN/docs/Web/JavaScript/Guide/Loops_and_iteration#do...while_语句"><code>do...while</code></a><br> @@ -49,7 +49,7 @@ translation_of: Web/JavaScript/Guide </ul> <ul class="card-grid"> - <li><span><a href="/zh-CN/docs/Web/JavaScript/Guide/Functions">函数</a></span> + <li><a href="/zh-CN/docs/Web/JavaScript/Guide/Functions">函数</a> <p><a href="/zh-CN/docs/Web/JavaScript/Guide/Functions#定义函数">定义函数</a><br> <a href="/zh-CN/docs/Web/JavaScript/Guide/Functions#调用函数">调用函数</a><br> @@ -58,17 +58,17 @@ translation_of: Web/JavaScript/Guide <a href="/zh-CN/docs/Web/JavaScript/Guide/Functions#使用_arguments_对象">arguments 对象</a> 和 <a href="/zh-CN/docs/Web/JavaScript/Guide/Functions#函数参数">参数</a><br> <a href="/zh-CN/docs/Web/JavaScript/Guide/Functions#箭头函数">箭头函数</a></p> </li> - <li><span><a href="/zh-CN/docs/Web/JavaScript/Guide/Expressions_and_Operators">表达式和运算符</a></span> + <li><a href="/zh-CN/docs/Web/JavaScript/Guide/Expressions_and_Operators">表达式和运算符</a> <p><a href="/zh-CN/docs/Web/JavaScript/Guide/Expressions_and_Operators#赋值运算符(Assignment_operators)">赋值</a> & <a href="/zh-CN/docs/Web/JavaScript/Guide/Expressions_and_Operators#比较运算符(Comparison_operators)">比较</a><br> <a href="/zh-CN/docs/Web/JavaScript/Guide/Expressions_and_Operators#算术运算符(Arithmetic_operators)">算术运算符</a><br> <a href="/zh-CN/docs/Web/JavaScript/Guide/Expressions_and_Operators#位运算符(Bitwise_operators)">位运算</a> & <a href="/zh-CN/docs/Web/JavaScript/Guide/Expressions_and_Operators#逻辑运算符(Logical_operators)">逻辑运算符</a><br> <a href="/zh-CN/docs/Web/JavaScript/Guide/Expressions_and_Operators#conditional_operator">条件(三元)运算符</a></p> </li> - <li><span><a href="/zh-CN/docs/Web/JavaScript/Guide/Numbers_and_dates">数字与日期</a></span> <a href="/zh-CN/docs/Web/JavaScript/Guide/Numbers_and_dates#数字"><code>Number</code> 字面量</a><br> + <li><a href="/zh-CN/docs/Web/JavaScript/Guide/Numbers_and_dates">数字与日期</a> <a href="/zh-CN/docs/Web/JavaScript/Guide/Numbers_and_dates#数字"><code>Number</code> 字面量</a><br> <a href="/zh-CN/docs/Web/JavaScript/Guide/Numbers_and_dates#数字对象"><code>Number</code> 对象</a><br> <a href="/zh-CN/docs/Web/JavaScript/Guide/Numbers_and_dates#数学对象(Math)"><code>Math</code> 对象</a><br> <a href="/zh-CN/docs/Web/JavaScript/Guide/Numbers_and_dates#日期对象"><code>Date</code> 对象</a></li> - <li><span><a href="/zh-CN/docs/Web/JavaScript/Guide/Text_formatting">文本格式化</a></span> + <li><a href="/zh-CN/docs/Web/JavaScript/Guide/Text_formatting">文本格式化</a> <p><a href="/zh-CN/docs/Web/JavaScript/Guide/Text_formatting#字符串">字符串字面量</a><br> <a href="/zh-CN/docs/Web/JavaScript/Guide/Text_formatting#字符串对象">字符串对象</a><br> <a href="/zh-CN/docs/Web/JavaScript/Guide/Text_formatting#多行模板字符串">模版字面量</a><br> @@ -78,24 +78,24 @@ translation_of: Web/JavaScript/Guide </ul> <ul class="card-grid"> - <li><span><a href="/zh-CN/docs/Web/JavaScript/Guide/Indexed_collections">索引集合</a></span> + <li><a href="/zh-CN/docs/Web/JavaScript/Guide/Indexed_collections">索引集合</a> <p><a href="/zh-CN/docs/Web/JavaScript/Guide/Indexed_collections#map%E6%95%B0%E7%BB%84">数组</a><br> <a href="/zh-CN/docs/Web/JavaScript/Guide/Indexed_collections#类型化数组(Typed_Arrays_)">类型数组</a></p> </li> - <li><span><a href="/zh-CN/docs/Web/JavaScript/Guide/Keyed_collections">带键集合</a></span> + <li><a href="/zh-CN/docs/Web/JavaScript/Guide/Keyed_collections">带键集合</a> <p><a href="/zh-CN/docs/Web/JavaScript/Guide/Keyed_collections#Map对象"><code>Map</code></a><br> <a href="/zh-CN/docs/Web/JavaScript/Guide/Keyed_collections#WeakMap对象"><code>WeakMap</code></a><br> <a href="/zh-CN/docs/Web/JavaScript/Guide/Keyed_collections#Set对象"><code>Set</code></a><br> <a href="/zh-CN/docs/Web/JavaScript/Guide/Keyed_collections#WeakSet对象"><code>WeakSet</code></a></p> </li> - <li><span><a href="/zh-CN/docs/Web/JavaScript/Guide/Working_with_Objects">处理对象</a></span> + <li><a href="/zh-CN/docs/Web/JavaScript/Guide/Working_with_Objects">处理对象</a> <p><a href="/zh-CN/docs/Web/JavaScript/Guide/Working_with_Objects#%E5%AF%B9%E8%B1%A1%E5%92%8C%E5%B1%9E%E6%80%A7_2">对象和属性</a><br> <a href="/zh-CN/docs/Web/JavaScript/Guide/Working_with_Objects#%E5%88%9B%E5%BB%BA%E6%96%B0%E5%AF%B9%E8%B1%A1">创建对象</a><br> <a href="/zh-CN/docs/Web/JavaScript/Guide/Working_with_Objects#%E5%AE%9A%E4%B9%89%E6%96%B9%E6%B3%95">定义方法</a><br> <a href="/zh-CN/docs/Web/JavaScript/Guide/Working_with_Objects#%E5%AE%9A%E4%B9%89_getters_%E4%B8%8E_setters">getter 和 setter</a></p> </li> - <li><span><a href="/zh-CN/docs/Web/JavaScript/Guide/Details_of_the_Object_Model">对象模型的细节</a></span> + <li><a href="/zh-CN/docs/Web/JavaScript/Guide/Details_of_the_Object_Model">对象模型的细节</a> <p><a href="/zh-CN/docs/Web/JavaScript/Guide/Details_of_the_Object_Model#class-based_vs_prototype-based_languages">基于原型的面向对象编程</a><br> <a href="/zh-CN/docs/Web/JavaScript/Guide/Details_of_the_Object_Model#%E5%88%9B%E5%BB%BA%E5%B1%82%E7%BA%A7%E7%BB%93%E6%9E%84">创建对象层次结构</a><br> <a href="/zh-CN/docs/Web/JavaScript/Guide/Details_of_the_Object_Model#%E5%AF%B9%E8%B1%A1%E7%9A%84%E5%B1%9E%E6%80%A7">继承</a></p> @@ -103,7 +103,7 @@ translation_of: Web/JavaScript/Guide </ul> <ul class="card-grid"> - <li><span><a href="/zh-CN/docs/Web/JavaScript/Guide/Using_promises">Promises</a></span> + <li><a href="/zh-CN/docs/Web/JavaScript/Guide/Using_promises">Promises</a> <p><a href="/zh-CN/docs/Web/JavaScript/Guide/Using_promises#%E7%BA%A6%E5%AE%9A">约定</a><br> <a href="/zh-CN/docs/Web/JavaScript/Guide/Using_promises#%E9%93%BE%E5%BC%8F%E8%B0%83%E7%94%A8">链式调用</a><br> @@ -111,18 +111,18 @@ translation_of: Web/JavaScript/Guide <a href="/zh-CN/docs/Web/JavaScript/Guide/Using_promises#%E7%BB%84%E5%90%88">组合</a><br> <a href="/zh-CN/docs/Web/JavaScript/Guide/Using_promises#%E6%97%B6%E5%BA%8F">时序</a></p> </li> - <li><span><a href="/zh-CN/docs/Web/JavaScript/Guide/Iterators_and_Generators">迭代器与生成器</a></span> + <li><a href="/zh-CN/docs/Web/JavaScript/Guide/Iterators_and_Generators">迭代器与生成器</a> <p><a href="/zh-CN/docs/Web/JavaScript/Guide/Iterators_and_Generators#%E8%BF%AD%E4%BB%A3%E5%99%A8">迭代器</a><br> <a href="/zh-CN/docs/Web/JavaScript/Guide/Iterators_and_Generators#%E7%94%9F%E6%88%90%E5%99%A8">生成器</a><br> <a href="/zh-CN/docs/Web/JavaScript/Guide/Iterators_and_Generators#%E5%8F%AF%E8%BF%AD%E4%BB%A3%E5%AF%B9%E8%B1%A1">可迭代对象</a></p> </li> - <li><span><a href="/zh-CN/docs/Web/JavaScript/Guide/Meta_programming">元编程</a></span> + <li><a href="/zh-CN/docs/Web/JavaScript/Guide/Meta_programming">元编程</a> <p><a href="/zh-CN/docs/Web/JavaScript/Guide/Meta_programming#%E4%BB%A3%E7%90%86"><code>Proxy</code></a><br> <a href="/zh-CN/docs/Web/JavaScript/Guide/Meta_programming#%E5%8F%A5%E6%9F%84%E5%92%8C%E9%99%B7%E9%98%B1">Handlers 和 traps</a><br> <a href="/zh-CN/docs/Web/JavaScript/Guide/Meta_programming#%E6%92%A4%E9%94%80_Proxy">撤销 Proxy</a><br> <a href="/zh-CN/docs/Web/JavaScript/Guide/Meta_programming#%E5%8F%8D%E5%B0%84"><code>Reflect</code></a></p> </li> - <li><span><a href="/zh-CN/docs/Web/JavaScript/Guide/Modules">JavaScript 模块</a></span> + <li><a href="/zh-CN/docs/Web/JavaScript/Guide/Modules">JavaScript 模块</a> <p><a href="/zh-CN/docs/Web/JavaScript/Guide/Modules#Exporting_module_features">导出模块</a><br> <a href="/zh-CN/docs/Web/JavaScript/Guide/Modules#Importing_features_into_your_script">导入模块</a><br> <a href="/zh-CN/docs/Web/JavaScript/Guide/Modules#Default_exports_versus_named_exports">默认导出</a><br> diff --git a/files/zh-cn/web/javascript/guide/modules/index.html b/files/zh-cn/web/javascript/guide/modules/index.html index 816f3e13ee..1ea1f81adf 100644 --- a/files/zh-cn/web/javascript/guide/modules/index.html +++ b/files/zh-cn/web/javascript/guide/modules/index.html @@ -125,7 +125,7 @@ export function draw(ctx, length, x, y, color) { }; }</pre> -<p>你能够导出函数,<code>var</code><font face="Arial, x-locale-body, sans-serif"><span style="background-color: #ffffff;">,</span></font><code>let</code><font face="Arial, x-locale-body, sans-serif"><span style="background-color: #ffffff;">,</span></font><code>const</code>, 和等会会看到的类。export要放在最外层;比如你不能够在函数内使用<code>export</code>。</p> +<p>你能够导出函数,<code>var</code><font face="Arial, x-locale-body, sans-serif">,</font><code>let</code><font face="Arial, x-locale-body, sans-serif">,</font><code>const</code>, 和等会会看到的类。export要放在最外层;比如你不能够在函数内使用<code>export</code>。</p> <p>一个更方便的方法导出所有你想要导出的模块的方法是在模块文件的末尾使用一个export 语句, 语句是用花括号括起来的用逗号分割的列表。比如:</p> @@ -139,7 +139,7 @@ export function draw(ctx, length, x, y, color) { <p>使用 <code><a href="/en-US/docs/Web/JavaScript/Reference/Statements/import">import</a></code> 语句,然后你被花括号包围的用逗号分隔的你想导入的功能列表,然后是关键字from,然后是模块文件的路径。模块文件的路径是相对于站点根目录的相对路径,对于我们的<code>basic-modules</code> 应该是<code> /js-examples/modules/basic-modules</code>。</p> -<p>当然,我们写的路径有一点不同---我们使用点语法意味 “当前路径”,跟随着包含我们想要找的文件的路径。这比每次都要写下整个相对路径要好得多,因为它更短,使得URL 可移植 ---如果在站点层中你把它移动到不同的路径下面仍然能够工作。<span style='background-color: #fff3d4; color: #333333; display: inline !important; float: none; font-family: x-locale-heading-primary,zillaslab,Palatino,"Palatino Linotype",x-locale-heading-secondary,serif; font-size: 16px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; white-space: normal;'>(修订版 1889482)</span></p> +<p>当然,我们写的路径有一点不同---我们使用点语法意味 “当前路径”,跟随着包含我们想要找的文件的路径。这比每次都要写下整个相对路径要好得多,因为它更短,使得URL 可移植 ---如果在站点层中你把它移动到不同的路径下面仍然能够工作。(修订版 1889482)</p> <p>那么看看例子吧:</p> @@ -152,7 +152,7 @@ export function draw(ctx, length, x, y, color) { <p>你可以在<code><a href="https://github.com/mdn/js-examples/blob/master/modules/basic-modules/main.js">main.mjs</a></code>中看到这些。</p> <div class="blockIndicator note"> -<p><strong>Note</strong>:在一些模块系统中你可以忽略文件扩展名(比如<code>'/model/squre'</code> .这在原生JavaScript 模块系统中不工作。<s>此外,记住你需要包含最前面的正斜杠。 </s> <span style='background-color: #fff3d4; color: #333333; display: inline !important; float: none; font-family: x-locale-heading-primary,zillaslab,Palatino,"Palatino Linotype",x-locale-heading-secondary,serif; font-size: 16px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; white-space: normal;'>(修订版 1889482)</span></p> +<p><strong>Note</strong>:在一些模块系统中你可以忽略文件扩展名(比如<code>'/model/squre'</code> .这在原生JavaScript 模块系统中不工作。<s>此外,记住你需要包含最前面的正斜杠。 </s> (修订版 1889482)</p> </div> <p>因为你导入了这些功能到你的脚本文件,你可以像定义在相同的文件中的一样去使用它。下面展示的是在 <code>main.mjs</code> 中的import 语句下面的内容。</p> diff --git a/files/zh-cn/web/javascript/guide/regular_expressions/quantifiers/index.html b/files/zh-cn/web/javascript/guide/regular_expressions/quantifiers/index.html index 21eda6551d..d705953a69 100644 --- a/files/zh-cn/web/javascript/guide/regular_expressions/quantifiers/index.html +++ b/files/zh-cn/web/javascript/guide/regular_expressions/quantifiers/index.html @@ -99,7 +99,7 @@ var loooongWord = /\b\w{13,}\b/g; var sentence = "Why do I have to learn multiplication table?"; console.table(sentence.match(singleLetterWord)); // ["I"] -console.table(sentence.match(notSoLongWord)); // <span class="message-body-wrapper"><span class="message-flex-body"><span class="devtools-monospace message-body"><span class="objectBox objectBox-array"><span class="arrayLeftBracket">[ </span><span class="objectBox objectBox-string">"Why"</span>, <span class="objectBox objectBox-string">"do"</span>, <span class="objectBox objectBox-string">"I"</span>, <span class="objectBox objectBox-string">"have"</span>, <span class="objectBox objectBox-string">"to"</span>, <span class="objectBox objectBox-string">"learn"</span>, <span class="objectBox objectBox-string">"table"</span><span class="arrayRightBracket"> ]</span></span></span></span></span> +console.table(sentence.match(notSoLongWord)); // [ "Why", "do", "I", "have", "to", "learn", "table" ] console.table(sentence.match(loooongWord)); // ["multiplication"]可选可选字符 </pre> diff --git a/files/zh-cn/web/javascript/guide/regular_expressions/unicode_property_escapes/index.html b/files/zh-cn/web/javascript/guide/regular_expressions/unicode_property_escapes/index.html index 5ef6c0ec75..db7312bea4 100644 --- a/files/zh-cn/web/javascript/guide/regular_expressions/unicode_property_escapes/index.html +++ b/files/zh-cn/web/javascript/guide/regular_expressions/unicode_property_escapes/index.html @@ -39,7 +39,7 @@ translation_of: Web/JavaScript/Guide/Regular_Expressions/Unicode_Property_Escape <li><a href="https://unicode.org/reports/tr24/#Script_Extensions">Script_Extensions</a> (<code>scx</code>)</li> </ul> -<p>参考 <a href="https://www.unicode.org/Public/UCD/latest/ucd/PropertyValueAliases.txt">PropertyValueAliases.txt<span class="hidden"> </span></a></p> +<p>参考 <a href="https://www.unicode.org/Public/UCD/latest/ucd/PropertyValueAliases.txt">PropertyValueAliases.txt </a></p> <dl> <dt>UnicodeBinary属性名</dt> diff --git a/files/zh-cn/web/javascript/guide/text_formatting/index.html b/files/zh-cn/web/javascript/guide/text_formatting/index.html index eac38b4502..fbe34d2cd6 100644 --- a/files/zh-cn/web/javascript/guide/text_formatting/index.html +++ b/files/zh-cn/web/javascript/guide/text_formatting/index.html @@ -38,7 +38,7 @@ translation_of: Web/JavaScript/Guide/Text_formatting <h4 id="Unicode字元逸出">Unicode字元逸出</h4> -<p>这是ECMAScript 6中的新特性。有了Unicode字元逸出,任何字符都可以用16进制数转义, 这使得通过Unicode转义表示大于<code>0x10FFFF</code>的字符成为可能。使用简单的Unicode转义时通常需要分别写字符相应的两个部分(译注:大于<span style="font-family: courier,andale mono,monospace;">0x10FFFF的字符需要拆分为相应的两个小于0x10FFFF的部分</span>)来达到同样的效果。</p> +<p>这是ECMAScript 6中的新特性。有了Unicode字元逸出,任何字符都可以用16进制数转义, 这使得通过Unicode转义表示大于<code>0x10FFFF</code>的字符成为可能。使用简单的Unicode转义时通常需要分别写字符相应的两个部分(译注:大于0x10FFFF的字符需要拆分为相应的两个小于0x10FFFF的部分)来达到同样的效果。</p> <p>请参阅 {{jsxref("String.fromCodePoint()")}} 或 {{jsxref("String.prototype.codePointAt()")}}。</p> diff --git a/files/zh-cn/web/javascript/guide/working_with_objects/index.html b/files/zh-cn/web/javascript/guide/working_with_objects/index.html index 44246037e8..8504e1345e 100644 --- a/files/zh-cn/web/javascript/guide/working_with_objects/index.html +++ b/files/zh-cn/web/javascript/guide/working_with_objects/index.html @@ -12,24 +12,24 @@ translation_of: Web/JavaScript/Guide/Working_with_Objects --- <div><a id="#%E5%AF%B9%E8%B1%A1%E5%92%8C%E5%B1%9E%E6%80%A7" name="#%E5%AF%B9%E8%B1%A1%E5%92%8C%E5%B1%9E%E6%80%A7"></a>{{jsSidebar("JavaScript Guide")}} {{PreviousNext("Web/JavaScript/Guide/Text_formatting", "Web/JavaScript/Guide/Details_of_the_Object_Model")}}</div> -<p class="summary"><span style="line-height: 1.5;">JavaScript </span>的设计是<span style="line-height: 1.5;">一个</span>简单的基于对象的范式。<span style="line-height: 1.5;">一个对象就是一系列属性的集合,一个属性包含一个名和一个值。一个属性的值可以是函数,这种情况下属性也被称为</span><em>方法</em><span style="line-height: 1.5;">。除了浏览器里面预定义的那些对象之外,你也可以定义你自己的对象。本章节讲述了怎么使用对象、属性、函数和方法,怎样实现自定义对象。</span></p> +<p class="summary">JavaScript 的设计是一个简单的基于对象的范式。一个对象就是一系列属性的集合,一个属性包含一个名和一个值。一个属性的值可以是函数,这种情况下属性也被称为<em>方法</em>。除了浏览器里面预定义的那些对象之外,你也可以定义你自己的对象。本章节讲述了怎么使用对象、属性、函数和方法,怎样实现自定义对象。</p> <h2 id="对象概述">对象概述</h2> -<p><span style="line-height: 1.5;">javascript 中的对象(物体),和其它编程语言中的对象一样,可以比照现实生活中的对象(物体)来理解它。 javascript 中对象(物体)的概念可以比照着现实生活中实实在在的物体来理解。</span></p> +<p>javascript 中的对象(物体),和其它编程语言中的对象一样,可以比照现实生活中的对象(物体)来理解它。 javascript 中对象(物体)的概念可以比照着现实生活中实实在在的物体来理解。</p> <p>在javascript中,一个对象可以是一个单独的拥有属性和类型的实体。我们拿它和一个杯子做下类比。一个杯子是一个对象(物体),拥有属性。杯子有颜色,图案,重量,由什么材质构成等等。同样,javascript对象也有属性来定义它的特征。</p> <h2 id="对象和属性">对象和属性</h2> -<p><span style="line-height: 1.5;">一个 javascript 对象有很多属性。一个对象的属性可以被解释成一个附加到对象上的变量。对象的属性和普通的 javascript 变量基本没什么区别,仅仅是属性属于某个对象。属性定义了对象的特征(译注:动态语言面向对象的鸭子类型)。你可以通过点符号来访问一个对象的属性。</span></p> +<p>一个 javascript 对象有很多属性。一个对象的属性可以被解释成一个附加到对象上的变量。对象的属性和普通的 javascript 变量基本没什么区别,仅仅是属性属于某个对象。属性定义了对象的特征(译注:动态语言面向对象的鸭子类型)。你可以通过点符号来访问一个对象的属性。</p> <div style="margin-right: 270px;"> <pre class="brush: js">objectName.propertyName </pre> </div> -<p><span style="line-height: 1.5;">和其他 javascript 变量一样,对象的名字(可以是普通的变量)和属性的名字都是大小写敏感的。你可以在定义一个属性的时候就给它赋值。例如,我们创建一个myCar的对象然后给他三个属性,make,model,year。具体如下所示:</span></p> +<p>和其他 javascript 变量一样,对象的名字(可以是普通的变量)和属性的名字都是大小写敏感的。你可以在定义一个属性的时候就给它赋值。例如,我们创建一个myCar的对象然后给他三个属性,make,model,year。具体如下所示:</p> <pre class="brush: js">var myCar = new Object(); myCar.make = "Ford"; @@ -66,7 +66,7 @@ myObj[""] = "Even an empty string"; console.log(myObj); </pre> -<p><span id="result_box" lang="zh-CN"><span>请注意,方括号中的所有键都将转换为字符串类型,因为JavaScript中的对象只能使用String类型作为键类型。</span> <span>例如,在上面的代码中,当将键obj添加到myObj时,JavaScript将调用obj.toString()方法,并将此结果字符串用作新键。</span></span></p> +<p>请注意,方括号中的所有键都将转换为字符串类型,因为JavaScript中的对象只能使用String类型作为键类型。 例如,在上面的代码中,当将键obj添加到myObj时,JavaScript将调用obj.toString()方法,并将此结果字符串用作新键。</p> <p>你也可以通过存储在变量中的字符串来访问属性:</p> @@ -79,7 +79,7 @@ myCar[propertyName] = "Mustang"; </pre> </div> -<p><span style="line-height: 1.5;">你可以在 </span><span style="line-height: 1.5;"> </span><a class="internal" href="/zh-CN/docs/JavaScript/Guide/Statements#for...in_Statement" style="line-height: 1.5;" title="zh-CN/docs/JavaScript/Guide/Statements#for...in Statement">for...in</a><span style="line-height: 1.5;"> 语句中使用方括号标记以枚举一个对象的所有属性。为了展示它如何工作,下面的函数当你将对象及其名称作为参数传入时,显示对象的属性:</span></p> +<p>你可以在 <a class="internal" href="/zh-CN/docs/JavaScript/Guide/Statements#for...in_Statement" style="line-height: 1.5;" title="zh-CN/docs/JavaScript/Guide/Statements#for...in Statement">for...in</a> 语句中使用方括号标记以枚举一个对象的所有属性。为了展示它如何工作,下面的函数当你将对象及其名称作为参数传入时,显示对象的属性:</p> <pre class="brush: js">function showProps(obj, objName) { var result = ""; @@ -140,14 +140,14 @@ myCar.year = 1969 <pre class="brush: js">var obj = { property_1: value_1, // property_# 可以是一个标识符... 2: value_2, // 或一个数字... - <span lang="EN-US">["property" +3]</span>:<span lang="EN-US"> value_3</span>,<span lang="EN-US"><span> </span></span>// 或<span>一个可计算的<span lang="EN-US">key</span>名<span lang="EN-US">...</span></span> + ["property" +3]: value_3, // 或一个可计算的key名... // ..., "property n": value_n }; // 或一个字符串 </pre> -<p>这里 <code>obj</code> 是新对象的名称,每一个 <code>property_<em>i</em></code> 是一个标识符(可以是一个名称、数字或字符串字面量),并且每个 <code>value_<em>i</em></code> 是一个其值将被赋予 <span style="font-family: courier new,andale mono,monospace; line-height: 1.5;">property_</span><em>i </em><span style="line-height: 1.5;">的表达式。</span><code style="font-style: normal; line-height: 1.5;">obj</code><span style="line-height: 1.5;"> 与赋值是可选的;如果你不需要在其他地方引用对象,你就不需要将它赋给一个变量。(注意在接受一条语句的地方,你可能需要将对象字面量括在括号里,从而避免将字面量与块语句相混淆)</span></p> +<p>这里 <code>obj</code> 是新对象的名称,每一个 <code>property_<em>i</em></code> 是一个标识符(可以是一个名称、数字或字符串字面量),并且每个 <code>value_<em>i</em></code> 是一个其值将被赋予 property_<em>i </em>的表达式。<code style="font-style: normal; line-height: 1.5;">obj</code> 与赋值是可选的;如果你不需要在其他地方引用对象,你就不需要将它赋给一个变量。(注意在接受一条语句的地方,你可能需要将对象字面量括在括号里,从而避免将字面量与块语句相混淆)</p> <p>如果一个对象是通过在顶级脚本的对象初始化器创建的,则 JavaScript 在每次遇到包含该对象字面量的表达式时都会创建对象。同样的,在函数中的初始化器在每次函数调用时也会被创建。</p> @@ -273,11 +273,11 @@ fish.displayType(); // Output:Fishes <p>在 JavaScript 1.0 中,你可以通过名称或序号访问一个属性。但是在 JavaScript 1.1 及之后版本中,如果你最初使用名称定义了一个属性,则你必须通过名称来访问它;而如果你最初使用序号来定义一个属性,则你必须通过索引来访问它。</p> -<p>这个限制发生在你通过构造函数创建一个对象和它的属性(就象我们之前通过 <code>Car</code> 对象类型所做的那样)并且显式地定义了单独的属性(如 <code>m</code><span style="font-family: courier new,andale mono,monospace; line-height: 1.5;">yCar.color = "red")</span><span style="line-height: 1.5;">之时。如果你最初使用索引定义了一个对象属性,例如 <code>myCar[5] = "25"</code>,则你只可能通过 <code>myCar[5]</code> 引用它。</span></p> +<p>这个限制发生在你通过构造函数创建一个对象和它的属性(就象我们之前通过 <code>Car</code> 对象类型所做的那样)并且显式地定义了单独的属性(如 <code>m</code>yCar.color = "red")之时。如果你最初使用索引定义了一个对象属性,例如 <code>myCar[5] = "25"</code>,则你只可能通过 <code>myCar[5]</code> 引用它。</p> -<p><span style="line-height: 1.5;">这条规则的例外是从与HTML对应的对象,例如 <code>forms</code> 数组。对于这些数组的元素,你总是既可以通过其序号(依据其在文档中出现的顺序),也可以按照其名称(如果有的话)访问它。举例而言,如果文档中的第二个 <code><form></code> 标签有一个 <code>NAME</code> 属性且值为<code> "myForm",访问该 form 的方式可以是 document.forms[1],document.forms["myForm"]或 document.myForm。</code></span></p> +<p>这条规则的例外是从与HTML对应的对象,例如 <code>forms</code> 数组。对于这些数组的元素,你总是既可以通过其序号(依据其在文档中出现的顺序),也可以按照其名称(如果有的话)访问它。举例而言,如果文档中的第二个 <code><form></code> 标签有一个 <code>NAME</code> 属性且值为<code> "myForm",访问该 form 的方式可以是 document.forms[1],document.forms["myForm"]或 document.myForm。</code></p> -<p><span style="font-size: 1.7142857142857142rem; letter-spacing: -0.5px; line-height: 24px;">为对象类型定义属性</span></p> +<p>为对象类型定义属性</p> <p>你可以通过 <code>prototype </code>属性为之前定义的对象类型增加属性。这为该类型的所有对象,而不是仅仅一个对象增加了一个属性。下面的代码为所有类型为 <code>car </code>的对象增加了 <code>color</code> 属性,然后为对象 <code>car1</code> 的 <code>color</code> 属性赋值:</p> @@ -285,7 +285,7 @@ fish.displayType(); // Output:Fishes car1.color = "black"; </pre> -<p>参见<span style="line-height: 1.5;"> </span><a href="/zh-CN/docs/JavaScript/Reference" style="line-height: 1.5; text-decoration: underline;" title="zh-CN/docs/JavaScript/Reference">JavaScript Reference</a><span style="line-height: 1.5;"> 中</span><span style="line-height: 1.5;"> Function 对象的 </span><a href="/zh-CN/docs/JavaScript/Reference/Global_Objects/Function/prototype" style="line-height: 1.5;" title="zh-CN/docs/JavaScript/Reference/Global Objects/Function/prototype"><code>prototype</code> 属性</a><span style="line-height: 1.5;"> 。</span></p> +<p>参见 <a href="/zh-CN/docs/JavaScript/Reference" style="line-height: 1.5; text-decoration: underline;" title="zh-CN/docs/JavaScript/Reference">JavaScript Reference</a> 中 Function 对象的 <a href="/zh-CN/docs/JavaScript/Reference/Global_Objects/Function/prototype" style="line-height: 1.5;" title="zh-CN/docs/JavaScript/Reference/Global Objects/Function/prototype"><code>prototype</code> 属性</a> 。</p> <h2 id="定义方法">定义方法</h2> @@ -306,14 +306,14 @@ var myObj = { }; </pre> -<p>这里 <code>objectName</code> 是一个已经存在的对象,<code>methodname</code> 是方法的名称,而 <code style="font-style: normal; line-height: 1.5;">function_name</code><span style="line-height: 1.5;"> </span><span style="line-height: 1.5;">是函数的名称。</span></p> +<p>这里 <code>objectName</code> 是一个已经存在的对象,<code>methodname</code> 是方法的名称,而 <code style="font-style: normal; line-height: 1.5;">function_name</code> 是函数的名称。</p> <p>你可以在对象的上下文中象这样调用方法:</p> <pre class="brush: js">object.methodname(params); </pre> -<p>你可以在对象的构造函数中包含方法定义来<span style="line-height: 1.5;">为某个对象类型定义方法。例如,你可以为之前定义的 <code>car</code> 对象定义一个函数格式化并显示其属性:</span></p> +<p>你可以在对象的构造函数中包含方法定义来为某个对象类型定义方法。例如,你可以为之前定义的 <code>car</code> 对象定义一个函数格式化并显示其属性:</p> <pre class="brush: js">function displayCar() { var result = `A Beautiful ${this.year} ${this.make} ${this.model}`; |