function Dog(name) {
this.name = name
this.say = function () {
console.log('name = ' + this.name)
}
}
function Cat(name) {
this.name = name
this.say = function () {
console.log('name = ' + this.name)
}
}
function _new(fn, ...arg) {
const obj = {};
obj.__proto__ = fn.prototype;
fn.apply(obj, arg)
return Object.prototype.toString.call(obj) == '[object Object]'? obj : {}
}
var dog = _new(Dog,'aaa')
dog.say()
console.log(dog instanceof Dog)
console.log(dog instanceof Cat)
console.log('\n')
var cat = _new(Cat, 'bbb');
cat.say()
console.log(cat instanceof Cat)