javascript
Some Essential JavaScript Questions And Answers(3)
Some Essential JavaScript Questions And Answers
Question5:
What is the significance, and what are the benefits, of including 'use strict' at the beginning of a JavaScript source file?
[譯]:在JavaScript源文件開頭包含'use strict'有什么意義和好處?
Answer:
The short and most important answer here is that use strict is a way to voluntarily enforce stricter parsing and error handling on your JavaScript code at runtime. Code errors that would otherwise have been ignored or would have failed silently will now generate errors or throw exceptions. In general, it is a good practice.
[譯]:對(duì)于這個(gè)問題,既簡(jiǎn)要又最重要的答案是,use strict?是一種在JavaScript代碼運(yùn)行時(shí)自動(dòng)實(shí)行更嚴(yán)格解析和錯(cuò)誤處理的方法。那些被忽略或靜靜地失敗了的錯(cuò)誤,現(xiàn)在會(huì)產(chǎn)生錯(cuò)誤或拋出異常。通常,這是一個(gè)不錯(cuò)的做法。
Some of the key benefits of strict mode include:
[譯]:嚴(yán)格模式一些主要優(yōu)點(diǎn)包括:
- Makes debugging easier. Code errors that would otherwise have been ignored or would have failed silently will now generate errors or throw exceptions, alerting you sooner to problems in your code and directing you more quickly to their source.
- [譯]:使調(diào)試更容易。那些被忽略或靜靜地失敗了的錯(cuò)誤,現(xiàn)在會(huì)產(chǎn)生錯(cuò)誤或拋出異常,更早地指出你代碼中的問題,指引你更快地找到它們的代碼。
- Prevents accidental globals. Without strict mode, assigning a value to an undeclared variable automatically creates a global variable with that name. This is one of the most common errors in JavaScript. In strict mode, attempting to do so throws an error.
- [譯]:預(yù)防意外的全局變量。非嚴(yán)格模式下,向一個(gè)未聲明的變量賦值時(shí),會(huì)自動(dòng)創(chuàng)建該名稱的全局變量(本人舉栗:var a = b = 3;非嚴(yán)格模式下會(huì)自動(dòng)創(chuàng)建全局變量b)。這是JavaScript中最常見的錯(cuò)誤之一。在嚴(yán)格模式下,嘗試這樣做的話會(huì)拋出錯(cuò)誤。
- Eliminates this coercion. Without strict mode, a reference to a this value of null or undefined is automatically coerced to the global. This can cause many headfakes and pull-out-your-hair kind of bugs. In strict mode, referencing a a this value of null or undefined throws an error.
- [譯]:消除?this?強(qiáng)制。如果沒有嚴(yán)格模式,引用一個(gè)空的this值或者未定義的this值時(shí)會(huì)自動(dòng)強(qiáng)制到全局變量。這可能會(huì)導(dǎo)致許多令人頭痛以及讓人恨不得拔自己頭發(fā)的bug。在嚴(yán)格模式下,引用空或未定義的this值會(huì)拋出錯(cuò)誤。
- Disallows duplicate parameter values. Strict mode throws an error when it detects a duplicate named argument for a function (e.g., function foo(val1, val2, val1){}), thereby catching what is almost certainly a bug in your code that you might otherwise have wasted lots of time tracking down.
- Note: It used to be (in ECMAScript 5) that strict mode would disallow duplicate property names (e.g. var object = {foo: "bar", foo: "baz"};) but as of ECMAScript 2015 this is no longer the case.
- [譯]:不允許重復(fù)的屬性名稱、參數(shù)值。嚴(yán)格模式下,當(dāng)檢測(cè)到函數(shù)中(例如,function foo(val1, val2, val1){})重復(fù)命名的參數(shù)時(shí)會(huì)拋出錯(cuò)誤,因此捕捉那些中幾乎可以肯定是個(gè)bug的代碼,可以避免浪費(fèi)大量的跟蹤時(shí)間。注意:ES5中嚴(yán)格模式不允許重復(fù)的屬性名(比如 var object = {foo: "bar", foo: "baz"};)但是在ES6中,這種情況是不存在的。
- Makes eval() safer. There are some differences in the way eval() behaves in strict mode and in non-strict mode. Most significantly, in strict mode, variables and functions declared inside of an eval() statement are not created in the containing scope (they are created in the containing scope in non-strict mode, which can also be a common source of problems).
- [譯]:使eval() 更安全。在嚴(yán)格模式和非嚴(yán)格模式下,eval() 的行為方式有所不同。最顯而易見的是,在嚴(yán)格模式下,聲明在 eval() 語句內(nèi)的變量和函數(shù)不會(huì)在包含范圍內(nèi)創(chuàng)建(在非嚴(yán)格模式下它們會(huì)在包含范圍中被創(chuàng)建,這也是一個(gè)常見的問題源)。
- Throws error on invalid usage of delete. The delete operator (used to remove properties from objects) cannot be used on non-configurable properties of the object. Non-strict code will fail silently when an attempt is made to delete a non-configurable property, whereas strict mode will throw an error in such a case.
- [譯]:在 delete使用無效時(shí)拋出錯(cuò)誤。delete操作符(用于從對(duì)象中刪除屬性)不能用在對(duì)象不可配置的屬性上。當(dāng)試圖刪除一個(gè)不可配置的屬性時(shí),非嚴(yán)格代碼將默默地失敗(即不會(huì)報(bào)錯(cuò)),而嚴(yán)格模式將在這樣的情況下拋出異常。
Question6:
Consider the two functions below. Will they both return the same thing? Why or why not?
[譯]:思考以下兩個(gè)方法,它們的返回值一樣嗎?為什么?
function foo1() {return {bar: "hello"}; }function foo2() {return{bar: "hello"}; }Answer:
Surprisingly, these two functions will not return the same thing. Rather:
[譯]:令人驚訝的是,這兩個(gè)方法將返回不一樣的東西。確切點(diǎn)說:
console.log("foo1 returns:"); console.log(foo1()); console.log("foo2 returns:"); console.log(foo2());will yield:[譯]:將產(chǎn)生:
foo1 returns: Object {bar: "hello"} foo2 returns: undefined
Not only is this surprising, but what makes this particularly gnarly is that foo2() returns undefined without any error being thrown.
[譯]:驚訝的不只是這個(gè),更讓人困惑的是, foo2()返回undefined卻沒有任何錯(cuò)誤拋出。
The reason for this has to do with the fact that semicolons are technically optional in JavaScript (although omitting them is generally really bad form). As a result, when the line containing the return statement (with nothing else on the line) is encountered in foo2(), a semicolon is automatically inserted immediately after the return statement.
[譯]:原因與這樣一個(gè)事實(shí)有關(guān),即分號(hào)在JavaScript中是一個(gè)可選項(xiàng)(盡管省略它們通常是非常糟糕的形式)。其結(jié)果就是,當(dāng)碰到 foo2()中包含 return語句時(shí)(代碼行上沒有其他任何代碼),分號(hào)會(huì)立即自動(dòng)插入到返回語句之后。
No error is thrown since the remainder of the code is perfectly valid, even though it doesn’t ever get invoked or do anything (it is simply an unused code block that defines a property bar which is equal to the string "hello").
[譯]:也不會(huì)拋出錯(cuò)誤,因?yàn)榇a的其余部分是完全有效的,即使它沒有得到調(diào)用或做任何事情(相當(dāng)于它就是是一個(gè)未使用的代碼塊,定義了等同于字符串 "hello"的屬性 bar)。
This behavior also argues for following the convention of placing an opening curly brace at the end of a line in JavaScript, rather than on the beginning of a new line. As shown here, this becomes more than just a stylistic preference in JavaScript.
[譯]:受到這種行為的影響下,(我們)贊成將左括號(hào)放置于JavaScript代碼行的末尾而不是新代碼行開頭的約定。正如這里所示,在JavaScript中,這不僅僅是一個(gè)風(fēng)格偏好。
舉個(gè)例子:
都這樣寫就沒事了,本人一般用ESLint校驗(yàn)。
function foo1() {return {bar: "hello"}; }創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)
總結(jié)
以上是生活随笔為你收集整理的Some Essential JavaScript Questions And Answers(3)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Some Essential JavaS
- 下一篇: 大班教案《快乐的小公主》反思