0
0

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 3 years have passed since last update.

JavaScriptで親クラスのメソッドを呼び出す方法

Last updated at Posted at 2020-09-21

#前提

Javaだと親クラスのhogeメソッドを
super.hogeって感じで呼び出せるけど
Vanilla.JSはどーすんのって話。
ECMAScript2015なんて知りません。
なお、Vanilla.JSにクラスの概念がないことは知ってます。

#こうすればいい

main.js

function SuperClazz() {};
SuperClazz.prototype.hoge = function(param) {
	console.log("SuperClazz hoge: " + param);
};

function SubClazz() {
	SuperClazz.call(this);
};

SubClazz.prototype = Object.create(SuperClazz.prototype);
SubClazz.prototype.constructor = SubClazz;

SubClazz.prototype.hoge = function(param) {
	// ↓これ
	SuperClazz.prototype.hoge.call(this, param);
	console.log("SubClazz hoge: " + param);
};

// 実行
const subClazz = new SubClazz();
subClazz.hoge("I Love Vanilla JS");

#実行

C>node main
SuperClazz hoge: I Love Vanilla JS
SubClazz hoge: I Love Vanilla JS

はいOK

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?