Javascript的繼承

  1. Javascript的繼承
  2. 繼承1
    1. 宣告+定義
    2. 呼叫執行
    3. 顯示
  3. 繼承2
    1. 宣告+定義
    2. 呼叫執行
    3. 顯示
  4. 繼承3
    1. 宣告+定義
    2. 呼叫執行
    3. 顯示

Javascript的繼承

原文連結: https://darkblack01.blogspot.com/2016/05/javascript_4.html
移植時的最後更新日期: 2016-05-04T12:03:00.074+08:00

繼承1

宣告+定義

function Animal(name) {
this.name = name;
this.age = 18;
this.sayName = function() {
console.log(this.name + ' and ' + this.age);
};
};

function Human() {
};

//繼承: Human is a Animal
Human.prototype = new Animal('bad student');

呼叫執行

var student = new Human('good student');
student.sayName();

顯示

bad student and 18

繼承2

宣告+定義

function Animal(name) {
this.name = name;
this.age = 18;
this.sayName = function() {
console.log(this.name + ' and ' + this.age);
};
};

function Human() {
};

//繼承: Human is a Animal
Human.prototype = new Animal();

呼叫執行

var student = new Human('good student');
student.sayName();

顯示

undefined student and 18

繼承3

宣告+定義

function Animal(name) {
this.name = name;
this.age = 18;
this.sayName = function() {
console.log(this.name + ' and ' + this.age);
};
};

function Human(name) {
this.name = name
};

//繼承: Human is a Animal
Human.prototype = new Animal('bad student');

呼叫執行

var student = new Human('good student');
student.sayName();

顯示

good student and 18