JS继承

谈到JS的继承,好多人不免会用到复制法。所谓的复制法,如:Object.extend = function(){};
典型的例子有prototype.js。

 

除此之外,还有一种方法。就是用到Function.apply(apply 方法能劫持(<<Ajax in Action>> 书中用到"劫持"一语,很生动啊)另外一个对象的方法,
继承另外一个对象的属性。 )。
很陌生吧。呵,我也很陌生,以前是,现在还是。记得很久前,在一篇博文中看到过,所以,今天旧事重提,也顺便巩固下自己。

费话不多说,见过代码,就明白了。

function parentClass()
{
    this.parentMethod = function(){};
}

function childClass()
{
    parentClass.apply(this, arguments);
}

var cc = new childClass();

cc.parentMethod();

哈哈,理解了吧。

 

posted @ 2010-05-19 12:32  bndy  阅读(418)  评论(0编辑  收藏  举报