[譯] Javascript初學者需要知道的十件事

來源:互聯網
上載者:User

原文:http://www.quora.com/JavaScript/What-are-the-top-ten-things-a-beginner-must-know-about-JavaScript

 

 

1. Javascript沒有類的概念。一般使用原型鏈繼承(prototypal inheritance)來類比類。

 

2. 除了null和undefined之外的任何資料類型都能表現成Object (behave like an object),包括Number類型和Function類型。

var n = 42;function f() { alert("foo"); };alert("n is " + n.toString()); // "n is 42"alert(f.name + " is a function"); // "f is a function"

注意,是“表現為object”,而不是“是object”。事實上,number, string和boolean是基本類型(primitives),除了這三個之外的都可以算作object,比如一個Regex也是一個object。 當需要訪問基本類型變數的屬性時,那些基本類型變數將被臨時轉換成object。 例如:

"foobar".big();// is equivalent tonew String("foobar").big();3.14.toFixed();// is equivalent tonew Number(3.14).toFixed()

另外,不能強行給基本類型變數(number, string, boolean)加上私人屬性。

var a = "mystring",    b = new String( "mystring" );Object.defineProperty( b, 'foo', { value: 42, enumerable: false });console.log(b.foo); // 42Object.defineProperty( a, 'foo', { value: 42, enumerable: false });// TypeError: Object.defineProperty called on non-object// trying another way:a.foo = 42;// remember, this is equivalent to:// new Number(a).foo = 42;// …so the 'foo' property is defined on the wrapper, not on 'a'console.log(a.foo); // undefined

 

3. 如果變數名前不加上var,那麼這個變數就是全域的。

function setGlobal() {  a = 42;}function setLocal() {  var b = 23;}setGlobal();alert(a); // 42setLocal();alert(b); // ReferenceError: b is not defined

 

4. this指標是由調用函數賦予的, 而不是由被調函數自身定義的。 例如:

var a = {}, b = {};a.foo = 42;b.foo = 18;a.alertFoo = function() { alert(this.foo); };a.alertFoo(); // 42a.alertFoo.call(b); // 18

 

5. 嚴格的相等判斷應該使用===。例如 :

0 == false 為真, 但是 0 === false 為假。

 

6. 0, undefined, null, "", NaN 都是假值 (falsy)。

 這些值全都等同於false,但他們之間不能相互替換。

 

7. 變數聲明會被提升到當前範圍的頂端。 例如:下述代碼中,你認為調用foo函數會返回什嗎?

var a = 2;function foo() {    return a;    var a = 5;}

它將返回undefined。 上述代碼等同於下述代碼:

var a = 2;function foo() {    var a; // 'a' declaration is moved to top    return a;    a = 5;}

另一個例子: 下述代碼中,調用函數foo後變數a的值是什嗎?

var a = 42;function foo() {    a = 12;    return a;    function a(){}}

答案是42。因為foo函數中變相聲明了a變數,因此a成了局部變數了。

function foo() {    function a() {} // local 'a' is a function    a = 12; // local 'a' is now a number (12)    return a; // return the local 'a' (12)}

 

8. 參數在函式宣告中可以省略, 例如:

function hello(name, age) {  alert("Hello "+name+", you’re "+age+" years old!");}hello("Anon", 42); // "hello Anon, you’re 42 years old!"hello("Baptiste"); // "hello Baptiste, you’re undefined years old!"hello("Bulat", 24, 42); // "hello Bulat, you’re 24 years old!"

 

9. 對於字串,使用雙引號""和單引號''的效果是一樣的。

 

10. Javascript代碼可以在瀏覽器環境之外運行, 比如在Terminal或者HTTP伺服器上。(例如 Node.js)

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.