new操作符的实现

107 阅读1分钟


function _new() {
  var constructor = [].shift.call(arguments);
  if (!constructor) {
    throw new Error('first params is not a function!');
  }
  var args = [].slice.call(arguments);
  var context = Object.create(context.prototype);
  var result = constructor.apply(obj, args);
  return (result !== null && typeof result === 'object') || typeof result === 'function'
    ? result
    : context;
}