先用比較官方的描述解釋一下 JavaScript:它是一種直譯式腳本語言,屬於動態類型、弱類型、基於原型的語言,內建支持類型。它的解譯器被稱為 JavaScript 引擎,是瀏覽器的一部分,廣泛用於客戶端腳本,最早出現在 HTML 網頁上,用來為網頁增加動態功能。
以下程式碼可證明 String、Number 是物件,而 string、number 是不同的資料格式:
var str = "abc"; var Str = new String("abc"); var num = 100; var Num = new Number(100); console.log(typeof str, typeof Str, typeof num, typeof Num); // string object number object
當我們用 new 建立物件(透過建構函式)或用 Object.create 建立物件時,這個物件就會有原型與原型鏈。比如建立一個建構函式 _obj 並 new 出 obj,此時原型鏈就是:obj → _obj → Object.prototype → null。
function _obj(){}; _obj.prototype.name = "野獸"; //每個函數都包含一個prototype屬性,這個屬性指向一個對象的引用,這個對象就是“原型對象”。 _obj.prototype.age = 21; _obj.prototype.sayHello = function(){ console.log("Hello "+this.name); }; var obj = new _obj(); console.log(obj); //_obj{} 而其的__proto__裡包含了_obj裡的各個屬性,包括name,age,sayHello console.log(obj.__proto__ == _obj.prototype); //true obj.sayHello(); // Hello 野獸 console.log(Object.prototype.__proto__); // null 當我們追究到Object(也就是object的源頭時)其指向的是null
值得注意的是:Object.create(null) 的原型鏈直接為 null。
當我們用 var 宣告變數,其實就是在該作用域所指向的物件上新增屬性及屬性值。JavaScript 不存在塊級作用域,當前作用域內宣告的變數只有在當前作用域及其內部函式中可用,函式外部呼叫則會報錯 not defined。
var firstNum = 1; ~function(){ var secondNum = 2; console.log("在裡面打印:"+firstNum ,secondNum); // 在裡面打印:1 2 }(); console.log("在外面打印:"+firstNum ,secondNum); // Uncaught ReferenceError: secondNum is not defined(…)
作用域也有作用域鏈:
var firstNum = 1; console.log("在最外層打印:"+firstNum); // 在最外層打印:1 ~function(){ var secondNum = 2; console.log("在中間層打印:"+firstNum,secondNum); // 在中間層打印:1 2 ~function(){ var thirdNum = 3; console.log("在最裡層打印:"+firstNum,secondNum,thirdNum); // 在最裡層打印:1 2 3 }(); console.log("在中間層打印:"+firstNum,secondNum,thirdNum); // Uncaught ReferenceError: thirdNum is not defined(…) }(); console,log("在最外層打印:"+firstNum,secondNum,thirdNum); // 由於上面已經報錯,而浏覽器執行JavaScript又是單線程的,所以壓根沒執行到這句...
也就是說,當前作用域宣告的變數在其子作用域中一路可用。
函式的執行依賴於變數作用域,而這個作用域是在函式定義時決定的,並非呼叫時。為了實現這種詞法作用域,JavaScript 函式物件的內部狀態不僅包含邏輯程式碼,還必須引用當前的作用域鏈,這種特性就是「閉包」。
function counter(){ var num = 0; return { count : function(){ return num++; }, reset : function(){ return num = 0; } } }; var firstNum = counter(); var secondNum = counter(); console.log(firstNum.count()); // 0 console.log(secondNum.count()); // 0 firstNum.reset(); console.log(firstNum.count()); // 0 num已被重置,所以返回的為0 console.log(secondNum.count()); // 1 num未被重置,所以返回的是1 console.log(firstNum,secondNum); // 都為 Object{ count:function(),reset:function(),__proto__} 並且並不能在其中找到counter裡var的n,這也實現了函數裡的私有變量,只將需要暴露的兩個方法給暴露在外。
閉包常用的兩個作用:讀取函式內部的變數值;讓這些變數值始終保存著(在記憶體中)。同時須注意:閉包須慎用,因為函式內部變數都保存在記憶體中,會導致記憶體消耗較大。
在 JavaScript 中,this 通常指向我們正在執行的函式本身,或指向該函式所屬的物件。
全域的 this → 指向 Window
函式中的 this → 指向函式所在的物件
物件中的 this → 指向其本身
console.log(this); // Window {external: Object, chrome: Object, document: document, global: Window, cr: Object…} 全局下執行console.log,所以此處指向的是Window ~function seeThis(){ console.log(this); // Window {external: Object, chrome: Object, document: document, global: Window, cr: Object…} 由於我是在全局下寫的這個函數,所以此處指向的還是Window }(); var obj = { name:"野獸", showThis:function(){ console.log(this); // Object {name: "野獸",showThis:function(),__proto__} 此處打印的是對象本身 } }; obj.showThis();
在 JavaScript 函式體內,arguments 像陣列一樣(並非真的陣列),有 length 屬性,可代表傳給函式的參數個數。簡單來說,arguments 就是函式執行時所傳的實際參數,例如 arguments[0] 表示傳入的第一個參數。
function argumentsTest(){ console.log(arguments[0]?arguments[0]:"Hello World",arguments[1]?arguments[1]:"你好 世界") }; argumentsTest(); // Hello World 你好 世界 argumentsTest("This is firstValue => arguments[0].","This is secondValue => arguments[1]."); // This is firstValue => arguments[0]. This is secondValue => arguments[1].
暫且分享這些 JavaScript 常用基礎知識,希望對大家進一步學習掌握 JavaScript 程式設計有所幫助。