本文1021字,閱讀大約須要5分鐘。
總括: 本文對new進行了一個簡單介紹,而後使用一個函數模擬實現了new操做符作的事情。前端
人生是沒有畢業的學校。app
new
是JS中的一個關鍵字,用來將構造函數實例化的一個運算符。例子:函數
function Animal(name) { this.name = name; } Animal.prototype.sayName = function() { console.log("I'm " + this.name); } var cat = new Animal('Tom'); console.log(cat.name); // Tom console.log(cat.__proto__ === Animal.prototype); // true cat.sayName(); // I'm Tom
從上面的例子能夠得出兩點結論:學習
new
操做符實例化了一個對象;因爲new
是關鍵字,咱們只能去聲明一個函數去實現new
的功能,首先實現上面的三個特性,初版代碼以下:測試
附:對原型原型鏈不熟悉的能夠先看理解Javascript的原型和原型鏈。this
// construct: 構造函數 function newFunction() { var res = {}; // 排除第一個構造函數參數 var construct = Array.prototype.shift.call(arguments); res.__proto__ = construct.prototype; // 使用apply執行構造函數,將構造函數的屬性掛載在res上面 construct.apply(res, arguments); return res; }
咱們測試下:spa
function newFunction() { var res = {}; var construct = Array.prototype.shift.call(arguments); res.__proto__ = construct.prototype; construct.apply(res, arguments); return res; } function Animal(name) { this.name = name; } Animal.prototype.sayName = function() { console.log("I'm " + this.name); } var cat = newFunction(Animal, 'Tom'); console.log(cat.name); // Tom console.log(cat.__proto__ === Animal.prototype); // true cat.sayName(); // I'm Tom
一切正常。new
的特性實現已經80%,但new
還有一個特性:prototype
function Animal(name) { this.name = name; return { prop: 'test' }; } var cat = new Animal('Tom'); console.log(cat.prop); // test console.log(cat.name); // undefined console.log(cat.__proto__ === Object.prototype); // true console.log(cat.__proto__ === Animal.prototype); // false
如上,若是構造函數return
了一個對象,那麼new
操做後返回的是構造函數return
的對象。讓咱們來實現下這個特性,最終版代碼以下:code
// construct: 構造函數 function newFunction() { var res = {}; // 排除第一個構造函數參數 var construct = Array.prototype.shift.call(arguments); res.__proto__ = construct.prototype; // 使用apply執行構造函數,將構造函數的屬性掛載在res上面 var conRes = construct.apply(res, arguments); // 判斷返回類型 return conRes instanceof Object ? conRes : res; }
測試下:對象
function Animal(name) { this.name = name; return { prop: 'test' }; } var cat = newFunction(Animal, 'Tom'); console.log(cat.prop); // test console.log(cat.name); // undefined console.log(cat.__proto__ === Object.prototype); // true console.log(cat.__proto__ === Animal.prototype); // false
以上代碼就是咱們最終對new
操做符的模擬實現。咱們再來看下官方對new
的解釋
引用MDN對new
運算符的定義:
new
運算符建立一個用戶定義的對象類型的實例或具備構造函數的內置對象的實例。
new
操做符會幹下面這些事:
{}
);this
的上下文 ;this
。4條都已經實現。還有一個更好的實現,就是經過Object.create
去建立一個空的對象:
// construct: 構造函數 function newFunction() { // 經過Object.create建立一個空對象; var res = Object.create(null); // 排除第一個構造函數參數 var construct = Array.prototype.shift.call(arguments); res.__proto__ = construct.prototype; // 使用apply執行構造函數,將構造函數的屬性掛載在res上面 var conRes = construct.apply(res, arguments); // 判斷返回類型 return conRes instanceof Object ? conRes : res; }
以上。
能力有限,水平通常,歡迎勘誤,不勝感激。
訂閱更多文章可關注公衆號「前端進階學習」,回覆「666」,獲取一攬子前端技術書籍