Skip to content

Instantly share code, notes, and snippets.

@yuka2py
Created October 24, 2012 00:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yuka2py/3943047 to your computer and use it in GitHub Desktop.
Save yuka2py/3943047 to your computer and use it in GitHub Desktop.
コンストラクタを使った継承
コンストラクタを用いた継承のアプローチ。
オブジェクト生成の度に、各オブジェクトのメソッドとして関数オブジェクトを生成されるのかしら?
//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