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
我觉得你大部分都是因为标题误导而降低赞誉。你可能想要编辑标题,特别是你问的是什么 –