function Otaku(name, age) {
this.name = name
this.age = age
this.habit = 'Games'
return 'Otaku Factory'
}
var person = new Otaku('Kevin', 18)
console.log(person.name) // 'Kevin'
console.log(person.age) // 18
console.log(person.habit) // 'Games'
// Otaku.prototype.strength = 60
// Otaku.prototype.sayYourName = function() {
// console.log('I am' + this.name)
// }
// var person = objectFactory(Otaku, 'Kevin', 18)
// console.log(person.name) // 'Kevin'
// console.log(person.age) // 18
// console.log(person.habit) // 'Games'
// person.sayYourName() // I am Kevin
function objectFactory() {
var obj = new Object()
var Constructor = [].shift.call(arguments)
if(typeof Constructor !== 'function') {
throw new Error('first argument must be a factory function')
}
obj.__proto__ = Constructor.prototype
var ret = Constructor.apply(obj, arguments)
return typeof ret === 'object' ? ret : obj
}
console