Posted at

JS prototype サンプル

More than 1 year has passed since last update.


JS prototype サンプル


function ClassName(param){
this.param = param;
console.log('construct');
}

ClassName.prototype.action1 = function(){
console.log(this.param + 'アクション1');
}

ClassName.prototype.action2 = function(){
console.log(this.param + 'アクション2');
}

ClassName.prototype.action3 = function(){
this.action1();
this.action2();
}

var classLists = [];
console.log(1);
classLists.push(new ClassName('foo'));
console.log(2);
classLists.push(new ClassName('bar'));
console.log(3);

for(var i = 0; i < classLists.length; i ++){
console.log(4);
classLists[i].action3();
console.log(5);
}


Console表示


1
construct
2
construct
3
4
fooアクション1
fooアクション2
5
4
barアクション1
barアクション2
5