SOURCE

//es6 
function myNew(fn, ...args) {
    if (typeof fn !== 'function') throw 'first argrments need to be a function'
    let newObj = Object.create(fn.prototype);
    let hasRet = fn.apply(newObj, args);
    let isObject = typeof hasRet === 'object' && hasRet !== null;
    let isFunction = typeof hasRet === 'function';
    if (isObject || isFunction) {
        return hasRet
    }
    return newObj
}

//构造函数
function Person(name, sex) {
    this.name = name;
    this.sex = sex
}
Person.prototype.getName = function () {
    return this.name
}

const pp = new Person('pp', '男');
const mypp = myNew(Person, 'mypp', '男');
console.log('new',pp);
console.log('mynew',mypp);
console.log(pp.getName());
//共享原型上的方法
console.log(pp.getName === mypp.getName);//true
console 命令行工具 X clear

                    
>
console