复制代码 代码如下:Object.extend = function(destination, source) { for (property in source) { destination[property] = source[property]; } return destination; } 除此以外,还有一种不太常见的方法: Function.apply.
apply 方法能劫持(<<Ajax in Action>> 书中用到"劫持"一语,很生动啊)另外一个对象的方法, 继承另外一个对象的属性。 示范代码如下: 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=[] for(var key in this){ if (typeof(this[key])!="function") msg.push([key,":",this[key]].join("")) } alert(msg.join("
")) } }
function Student(name,age,grade,school){ //学生类 Person.apply(this,arguments) Print.apply(this,arguments) this.grade=grade //年级 this.school=school //学校 }