小编Dan*_*hua的帖子

克罗克福德的"新"方法

希望有人可以帮我打破Crockford JS Good Parts的一小段代码:

Function.method('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;
});
Run Code Online (Sandbox Code Playgroud)

我不理解的部分是他使用apply调用模式创建一个对象:

var other = this.apply(that, arguments);
Run Code Online (Sandbox Code Playgroud)

如何执行函数将创建新对象?

如果该功能将是:

var f = function (name) { …
Run Code Online (Sandbox Code Playgroud)

javascript invocation apply

3
推荐指数
1
解决办法
189
查看次数

标签 统计

apply ×1

invocation ×1

javascript ×1