日期:2014-05-16 浏览次数:20334 次
<script> var A = function () { this.alert = function () { alert('A'); } } var B = function () { this.alert = function () { alert('B'); } } var A = new A(); var B = new B(); A.alert(); </script>
<script> Aspects = new Object(); Aspects.addBefore = function(obj, fname, before) { var oldFunc = obj[fname]; obj[fname] = function() { return oldFunc.apply(this, before(arguments, oldFunc, this)); }; }; Aspects.addAfter = function(obj, fname, after) { var oldFunc = obj[fname]; obj[fname] = function() { return after(oldFunc.apply(this, arguments), arguments, oldFunc, this); }; }; Aspects.addAround = function(obj, fname, around) { var oldFunc = obj[fname]; obj[fname] = function() { return around(arguments, oldFunc, this); }; }; var A = function (){ this.alert = function (){ alert('A'); } } var B = function (){ this.alert = function (){ alert('B'); } } var a = new A(); var b = new B(); Aspects.addAfter(a,"alert",b.alert);//将b.alert方法动态增加给a a.alert(); </script>
------解决方案--------------------
楼主找找 js 继承
写一个类,继承A
------解决方案--------------------
方法1
var A=function()
{
B.apply(this,arguments);
this.alert = function ()
{
alert('A');
}
}
var B=function()
{
this.alert = function ()
{
alert('B');
}
}
这种方法缺点是A函数把B函数里的同名方法覆盖了
方法2
var A=function()
{
this.alert = function ()
{
alert('A');
}
}
var B=function()
{
this.alert = function ()
{
alert('B');
}
}
A.prototype=new B();
new A().alert();
A.prototype.alert.apply();
方法3
var A=function()
{
this.alert = function ()
{
alert('A');
}
}
var B=function()
{
this.alert = function ()
{
alert('B');
}
}
function c(){
var a=new A();
var b=new B();
a.alert();
b.alert();
}
var s=new c();
------解决方案--------------------
不必要那样复杂。
<script> Function.prototype.$Asynch = function ( Method ) { this (); Method (); } var A = function () { this.alert = function () { alert('A'); } } var B = function () { this.alert = function () { alert('B'); } } var A = new A(); var B = new B(); A.alert.$Asynch( B.alert ) </script>
------解决方案--------------------
JS继承
------解决方案--------------------
var A = function () { this.alert = function () { alert('A'); } } var B = function () { this.alert = function () { a