案例 借用父建構函式繼承屬性和方法

2022-08-02 19:15:16 字數 1399 閱讀 2546

//借用父建構函式繼承屬性

//1. 父建構函式

function

father(uname, age)

father.prototype.money

=function

() ;

//2 .子建構函式

function

son(uname, age, score)

= father.prototype; 這樣直接賦值會有問題,如果修改了子原型物件,父原型物件也會跟著一起變化

son.prototype

=new

father();

//如果利用物件的形式修改了原型物件,別忘了利用constructor 指回原來的建構函式

son.prototype.constructor

=son;

//這個是子建構函式專門的方法

son.prototype.exam

=function

()

varson

=new

son(

'劉德華',

借用構造函式呼叫繼承

function father name,age 這樣直接呼叫,那麼father中的this只的是 window。因為其實這樣呼叫的 window.father 李四 20 name 和age 屬性就新增到了window屬性上 father 李四 20 alert name window.name ...

借用建構函式實現繼承

在子型別建構函式的內部呼叫超型別的建構函式 function supertype function subtype var instance1 new subtype instance1.colors.push yellow console.log instance1.colors var inst...

繼承方式二 借用建構函式

function supertype function subtype var instance1 newsubtype instance1.colors.push black console.log instance1.colors red blue green black var instanc...