Function.prototype.new = function () {
// Create a new object that inherits from the
// constructor's prototype.
var that = Object.create(this.prototype);
// Invoke the constructor, binding –this- to
// the new object.
var other = this.apply(that, arguments);
// If its return value isn't an object,
// substitute the new object.
return (typeof other === 'object' && other) || that;
});
這是來自JavaScript的構造函數實例的一個替代實現:The Good Parts。 我的問題是爲什麼我們需要var other = ... 我們不能只返回變量嗎?用Javascript查看構造函數調用的另一種方法
您可能想要了解'&&'和'||'運算符的用途:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators#Logical_operators –
謝謝爲鏈接。它確實有幫助,但它沒有回答我的問題。變量'that'引用新創建的對象。那麼爲什麼還要檢查'other'是什麼類型呢?你可以直接返回'那個'嗎? – Meyyappan
我覺得你大部分都是因爲標題誤導而降低讚譽。你可能想要編輯標題,特別是你問的是什麼 –