JavaScript 语法基础 —— 新建对象

function Person(name) {
  this.name = name;
}
Person.prototype.getName = function() {
  return this.name;
};

var objectFactory = function() {
  var obj = new Object(), // 从 Object.prototype 上克隆一个空的对象
    Constructor = [].shift.call(arguments); // 取得外部传入的构造器,此例是 Person
  obj.__proto__ = Constructor.prototype; // 指向正确的原型
  var ret = Constructor.apply(obj, arguments);
  return typeof ret === "object" ? ret : obj;
};

var a = objectFactory(Person, "sven");
console.log(a.name); // 输出:sven
console.log(a.getName()); // 输出:sven
console.log(Object.getPrototypeOf(a) === Person.prototype);
// 输出:true

var a = objectFactory(A, 'sven' ); 
var a = new A( 'sven' );