supercalcクラスを継承したcalcクラスのメソッドを呼び出す例
calc.js (クラスを定義したjs)
//コンストラクタ function calc(width, height) { //supercalcを継承 for ( var prop in supercalc.prototype ) { if ( typeof(calc.prototype[prop]) == "undefined" ) { calc.prototype[prop] = supercalc.prototype[prop]; } } this.width = width; this.height = height; } //メソッド calc.prototype.area = function() { return this.width * this.height; };
supercalc.js (継承されるjs)
//コンストラクタ function supercalc() { } //メソッド calc.prototype.volume = function() { return this.width * this.height * 100; };
object.html(呼び出し元のHTML)
<html> <script type="text/javascript" src="calc.js"></script> <script type="text/javascript" src="supercalc.js"></script> <script> function click1(){ //オブジェクトの生成 var r = new calc(2, 2); //メソッドの呼び出し alert( r.area()); //継承したメソッドの呼び出し alert( r.volume()); } </script> <body> <input type='button' value='test' onclick='click1()'> <body> </html>