0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

JS prototype サンプル

0
Posted at

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
0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?