function foo(name,age){
this.name = name;
this.age = age;
this.habit = 'games';
return {}
}
foo.prototype.sex = '男';
foo.prototype.a =function(){
console.log('函数');
}
var person = new foo('wgf','18');
console.log(person.name)
console.log(person.age)
// person.a()
///模拟new
//1.访问构造函数变量与内部函数
//2.访问构造函数的原型
function objFactory(){
var obj = new Object();
var Constructor = [].shift.call(arguments);
obj.__proto__ = Constructor.prototype;
Constructor.apply(obj,arguments);
return obj;
}
var person1 = objFactory(foo,'wgf1','250')
console.log(person1.name)
console.log(person1.age)
console.log(person1.sex)
//3返回值处理
function objFactory(){
var obj = new Object();
var Constructor = [].shift.call(arguments);
obj.__proto__ = Constructor.prototype;
var result = Constructor.apply(obj,arguments);
return typeof result == 'object' ? result : obj ;
}
var person2 = objFactory(foo,'wgf2','251')
console.log(person2.name)
console.log(person2.age)
console.log(person2.sex)
console