Created
October 24, 2012 00:51
-
-
Save yuka2py/3943047 to your computer and use it in GitHub Desktop.
コンストラクタを使った継承
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
コンストラクタを用いた継承のアプローチ。 | |
オブジェクト生成の度に、各オブジェクトのメソッドとして関数オブジェクトを生成されるのかしら? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Personを作成 | |
var Person = function(name) { | |
this.name = name; | |
//メソッドを定義 | |
this.printName = function() { | |
document.write(this.name + "<br>"); | |
}; | |
}; | |
//Employeeを作成 | |
var Employee = function(name, salary) { | |
//**POINT** Personのコンストラクタをコールして継承 | |
Person.apply(this, [name]); | |
this.salary = salary; | |
//employeeのメソッドを定義 | |
this.printSalary = function() { | |
document.write(this.salary + "<br>"); | |
}; | |
}; | |
//実行 | |
var e = new Employee("Yuka2py", 24); | |
e.printName(); //Yuka2py | |
e.printSalary(); //24 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment