js中原型的详细研究

时间:2012-07-23 06:47:16

标签: javascript prototype

请考虑以下代码:

//for extend classes
function extend(subClass,superClass){
  var F=function(){};
  F.prototype=superClass.prototype;
  subClass.prototype=new F();
  subClass.prototype.constructor=subClass;
}
//basic sender object
function Sender(){}
Sender.prototype = {
  ...
};
//interface
//extend method 1:
function BackendAdmin(){}
BackendAdmin.prototype.Sender = function(){};
extend(BackendAdmin.Sender,Sender); //will generate prototype of undefined error
//extend method 2:
function BackendAdmin(){
  this.Sender = function(){};
  extend(this.Sender,Sender);// Seems can not be extended
}
// instance
var myadmin=new BackendAdmin();
myadmin.Sender.StartLoading(); // do not have method for method 2.

在这种情况下,我想在BackendAdmin中创建一个发送者实例,其中包括基本发送者类的方法,如果需要,我还需要在BackendAdmin中覆盖发送者的方法。但是,似乎它在两种情况下都不起作用,除非我使用这样的东西:

this.sender = new Sender();

如何使用扩展功能?

0 个答案:

没有答案