1、對象的繼承,一般的做法是複製:Object.extend
prototype.js的實現方式是:
Object.extend = function(destination, source) { for (property in source) { destination[property] = source[property]; } return destination; } |
除此之外,還有種方法,就是:Function.apply(當然使用Function.call也是可以的)
apply方法能劫持另外一個對象的方法,繼承另外一個對象的屬性
Function.apply(obj,args)方法能接收兩個參數
obj:這個對象將代替Function類裏this對象
args:這個是數組,它將作爲參數傳給Function(args-->arguments)
apply示範代碼如下:
<script> function Person(name,age){ //定義一個類,人類 this.name=name; //名字 this.age=age; //年齡 this.sayhello=function(){alert("hello")}; } function Print(){ //顯示類的屬性 this.funcName="Print"; this.show=function(){ var msg=[]; |