function objectFactory() {
let newObject = null;
let constructor = Array.prototype.shift.call(arguments);
let result = null;
// 判断参数是否是一个函数
if (typeof constructor !== "function") {
console.error("type error");
return;
}
// 新建一个空对象,对象的原型为构造函数的 prototype 对象
// newObject = Object.create(constructor.prototype);
newObject.prototype = constructor.prototype;
// 将 this 指向新建对象,并执行函数
result = constructor.apply(newObject, arguments);
// 判断返回对象
let flag = result && (typeof result === "object" || typeof result === "function");
// 判断返回结果
return flag ? result : newObject;
}
// 使用方法
const obj = objectFactory(function(name, age) {
// var obj = {
// 'name': 'tom',
// 'age': 12
// }
this.name = name
var res = 123;
// return obj;
// return res;
}, '张三',18);
console.log(typeof(obj))
console.log(obj)
// console.log(obj.getPrototype)
const _proto = Object.getPrototypeOf(obj)
console.log(_proto)
console