, . foo , Foo.
Child, , Parent.apply(this,arguements);. , Child , Parent.appy Child (. ).
, constuctor , (instanceof constructor , - Child) extend init, "".
- , , - , - .
var extend = function(source,target,arg){
var fn=function(){},ret;
fn.prototype = source.prototype;
target.prototype = new fn();
target.prototype.constructor = target;
ret = new target(arg);
ret.extend_init=function(arg){
source.apply(this,arguments);
};
ret.extend_init(arg);
delete ret.extend_init;
return ret;
};
var Parent = function(arg){
this.name=(arg && arg.name)? arg.name:undefined;
this.age=(arg && arg.age)?arg.age:undefined;
};
Parent.prototype.whoAreYou = function(){
return "I am "+this.name+" and I'm "+this.age+
" years old.";
};
var Child = function(){
};
var t = extend(Parent,Child,{
name: "t",
age: 22});
console.log(t instanceof Child);
console.log(t instanceof Parent);
console.log(t.whoAreYou());
, , , , :
var extend = function(source,target){
var fn=function(){},orgProto=target.prototype,
thing;
fn.prototype = source.prototype;
target.prototype = new fn();
for(thing in orgProto){
if(orgProto.hasOwnProperty(thing)){
target.prototype[thing]=orgProto[thing];
}
}
target.prototype.constructor = target;
target.prototype.extend_init=function(){
source.apply(this,arguments);
return this;
};
return target;
};
var Parent = function(arg){
this.name=(arg && arg.name)? arg.name:undefined;
this.age=(arg && arg.age)?arg.age:undefined;
};
Parent.prototype.whoAreYou = function(){
return "I am "+this.name+" and I'm "+this.age+
" years old.";
};
var Child = function(){
};
Child.prototype.something=22;
namesAndAges = [
{name:"1",age:1},
{name:"2",age:2},
{name:"3",age:3},
{name:"4",age:4},
{name:"5",age:5}
];
var constr=extend(Parent,Child);
var persons=[];
for(var i = 0,len=namesAndAges.length;i<len;i++){
persons.push(new constr(namesAndAges[i])
.extend_init(namesAndAges[i]));
};
delete constr.prototype.extend_init;
console.log(persons);
, , , super, mix ins this : fooobar.com/questions/2287/...