<script>
//創建構造函數
function Father(name, age) {
this.name = name;
this.age = age;
this.sayhello = function() {
console.log('你好呀~');
};
};
Father.prototype.say = function() {
console.log('我想說...');
};
function Son(uname, age) {
//繼承Father的屬性只需要通過call方法調用Father的構造函數並將this引用改爲Son即可.
Father.call(this, uname, age);
};
//通過改變son的原型對象指向一個Father的示例,然後通過Father中的對象原型__proto__共享Father原型對象prototype上的共有方法
Son.prototype = new Father(); //這裏不能讓Song的原型對象指向Father的原型對象,否則在Son的原型對象上添加Son特有的方法Father上也會添加,因爲指向的是同一個原型對象
//因爲改變了了Son的原型對象指向,所以重新讓構造函數指回原來的構造函數
Son.prototype.constructor = Son;
Son.prototype.bye = function() {
console.log(88);
};
var son = new Son('趙雲', 18);
</script>