`
yingxiongwudi
  • 浏览: 58728 次
  • 性别: Icon_minigender_1
  • 来自: 郑州
社区版块
存档分类
最新评论

JavaScript面向对象编程(转载)

阅读更多
JavaScript面向对象编程[一] 构造函数

暂时放弃js框架吧
开始写javascript的时候都是自己写,后来发现了prototype.js框架,发现很好用,就一直用的,他的对象创建方法被修改了,但很好用,再后来又转用jquery框架,受此框架影响,也不用自己创建类了,渐渐的竟然忘记了如何自己定义类了,猛的给一个一般方法,竟然看着别扭,混淆了很多东西,忘记了很多东西。今天回头整理下。
一下方法参考prototype.js

程序代码
  
    //类的定义
   
    //方法一:类的一般定义方法
    function player1(_name)
    {
        this.name = _name;
        this.say = function() {alert(this.name);};
    }
   
    var p1 = new player1('llinzzi1');
    p1.say();
   
   
    //方法二:prototype定义方法
    var player2 = function() {}
    player2.prototype = {
        name:'',
        say:function(){
            alert(this.name);
        }
    }
   
    var p2 = new player2();
    p2.name = 'llinzzi2';
    p2.say();
   
   
    //方法三:上面的方法结构美观,便捷,但构建函数不能带参数,修改方法
    var player3 = function() {
        this.init.apply(this, arguments);
    }
    player3.prototype = {
        init:function(_name){
            this.name = _name;
        },
        say:function(){
            alert(this.name);
        }
    }
   
    var p3 = new player3('llinzzi3');
    p3.say();
   
    //类的继承
   
    //方法一
    var player4 = function(){
        this.init.apply(this, arguments);
    }
    player4.prototype = new player3;
    player4.prototype.shout = function(){
        alert(this.name.toUpperCase());
    }
   
    var p4 = new player4('llinzzi4');
    p4.shout();
   
   
    //方法二 上面的方法不能采用{}的方法,修改方法
    Object.extend = function(destination, source) {
      for (var property in source)
        destination[property] = source[property];
      return destination;
    };
   
    var player5 = function(){
        this.init.apply(this, arguments);
    }
    Object.extend(Object.extend(player5.prototype,player3.prototype),{
        shout:function(){
            alert(this.name.toUpperCase());
        }
   
    });
  
    var p5 = new player5('llinzzi5');
    p5.shout();
   
   
   
   
    //再从prototype.js抄一端浏览器判断代码

    Browser = {
        IE:     !!(window.attachEvent && !window.opera),
        Opera:  !!window.opera,
        WebKit: navigator.userAgent.indexOf('AppleWebKit/') > -1,
        Gecko:  navigator.userAgent.indexOf('Gecko') > -1 && navigator.userAgent.indexOf('KHTML') == -1,
        MobileSafari: !!navigator.userAgent.match(/Apple.*Mobile.*Safari/)
    }
   
    alert(Browser.MobileSafari);

/////////////////////////////////////////////////
JavaScript面向对象编程[二] 事件处理


如果采用上篇文章的方法构建的类,如果要处理事件按通常的写法会造成回调函数作用域丢失。


程序代码


        var foo = function(){ this.init.apply(this,arguments);};
       
        foo.prototype = {
            init:function(_name){
                 this.name = _name;
                 this.addEvent('click',document,this.say);
            },
            say:function(_ev){
                alert(this.name);
                this.stop();
            },
            stop:function(){
                 this.removeEvent('click',document,this.say);
          },
            addEvent:function(_event,_element,_fn){
                if (_element.addEventListener) {
                    _element.addEventListener(_event, _fn, false);
                } else if (_element.attachEvent) {
                    _element.attachEvent("on" +_event,_fn);
                }
            },
            removeEvent:function(_event,_element,_fn){
                if (_element.removeEventListener) {
                    _element.removeEventListener(_event, _fn, false);
                } else if (_element.detachEvent) {
                    _element.detachEvent("on" +_event, _fn);
                }
            }
        }

        var a = new foo('llinzzi');




以上代码可以观察到this.say的alert方法已经调用,但内部的this.name已经不存在了,因为找不到this了。并且如果要像say里面传值似乎也比较麻烦。


解决this丢失和传值问题的代码,并解决传this后不能注销事件的问题。


程序代码

        var foo = function(){ this.init.apply(this,arguments);};
       
        foo.prototype = {
            init:function(_name){
                 this.name = _name;
                 this.addEvent('click',document,this.say,this,'hello world!');
            },
            say:function(_ev,_word){
                alert(this.name +':' +_word);
                this.stop();
            },
            stop:function(){
                 this.removeEvent('click',document,this.say);
          },
            addEvent:function(_event,_element,_fn,_scope,_args){
                var args = this._toArray(arguments),
                    ev = args.shift(),
                    element = args.shift(),
                    fn = args.shift(),
                    scope = args.length>0?args.shift():window;
                element['e'+ev+fn] =  function(_ev){
                    _ev == _ev || window.event;
                    args.unshift(_ev);
                    fn.apply(scope,args);
                };
                if (element.addEventListener) {
                    element.addEventListener(ev, element['e'+ev+fn], false);
                } else if (element.attachEvent) {
                    element.attachEvent("on" +ev, element['e'+ev+fn]);
                }
            },
            removeEvent:function(_event,_element,_fn){
                if (_element.removeEventListener) {
                    _element.removeEventListener(_event, _element['e'+_event+_fn], false);
                } else if (_element.detachEvent) {
                    _element.detachEvent("on" +_event, _element['e'+_event+_fn]);
                }
                try {
                    delete _element['e'+_event+_fn];
                }catch(_ex){
                    _element['e'+_event+_fn] = null;
                }
            },
            _toArray:function(iterable){
                if (!iterable) return [];
                if (iterable.toArray) return iterable.toArray();
                var length = iterable.length || 0, results = new Array(length);
                while (length--) results[length] = iterable[length];
                return results;
            }
        }


        var a = new foo('llinzzi');




下一次会分享一下在用面向对象编程过程中的一些心得,有关设计模式的一些整理。
/////////////////////////////////////////////////
JavaScript面向对象编程[三] 自定义事件


上一篇给foo类增加了
addEvent和removeEvent方便事件的注册与注销

这次总结下自定义事件的几种方法


程序代码


    <script type="text/javascript">
        <!--

    var foo = function(){ this.init.apply(this,arguments);};

    foo.prototype = {
        init:function(_name){
            this.name = _name;
            this.addEvent('click',document,this.say,this,'hello world!');
        },
        say:function(_ev,_word){
            alert(this.name +':' +_word);
            this.stop();
            this.onSay(_word);
        },
        stop:function(){
            this.removeEvent('click',document,this.say);
        },
        addEvent:function(_event,_element,_fn,_scope,_args){
            var args = Array.prototype.slice.call(arguments, 0); 
            ev = args.shift(),
            element = args.shift(),
            fn = args.shift(),
            scope = args.length>0?args.shift():window;
            element['e'+ev+fn] =  function(_ev){
                _ev == _ev || window.event;
                args.unshift(_ev);
                fn.apply(scope,args);
            };
            if (element.addEventListener) {
                element.addEventListener(ev, element['e'+ev+fn], false);
            } else if (element.attachEvent) {
                element.attachEvent("on" +ev, element['e'+ev+fn]);
            }
        },
        removeEvent:function(_event,_element,_fn){
            if (_element.removeEventListener) {
                _element.removeEventListener(_event, _element['e'+_event+_fn], false);
            } else if (_element.detachEvent) {
                _element.detachEvent("on" +_event, _element['e'+_event+_fn]);
            }
            try {
                delete _element['e'+_event+_fn];
            }catch(_ex){
                _element['e'+_event+_fn] = null;
            }
        },
        onSay:function(){}

    }


var a = new foo('llinzzi');
a.onSay = function(_word){
    alert('事件:'+this.name +'刚说了'+ _word);
}





HTML代码
<html>
<head>
<title>自定义事件</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" >
<meta name="keywords" content="" >
<meta name="description" content="" >
</head>
<body>



<script type="text/javascript">
<!--

var foo = function(){ this.init.apply(this,arguments);};

foo.prototype = {
init:function(_name){
this.name = _name;
this.addEvent('click',document,this.say,this,'hello world!');
},
say:function(_ev,_word){
alert(this.name +':' +_word);
this.stop();
this.onSay(_word);
},
stop:function(){
this.removeEvent('click',document,this.say);
},
addEvent:function(_event,_element,_fn,_scope,_args){
var args = Array.prototype.slice.call(arguments, 0); 
ev = args.shift(),
element = args.shift(),
fn = args.shift(),
scope = args.length>0?args.shift():window;
element['e'+ev+fn] =  function(_ev){
_ev == _ev || window.event;
args.unshift(_ev);
fn.apply(scope,args);
};
if (element.addEventListener) {
element.addEventListener(ev, element['e'+ev+fn], false);
} else if (element.attachEvent) {
element.attachEvent("on" +ev, element['e'+ev+fn]);
}
},
removeEvent:function(_event,_element,_fn){
if (_element.removeEventListener) {
_element.removeEventListener(_event, _element['e'+_event+_fn], false);
} else if (_element.detachEvent) {
_element.detachEvent("on" +_event, _element['e'+_event+_fn]);
}
try {
delete _element['e'+_event+_fn];
}catch(_ex){
_element['e'+_event+_fn] = null;
}
},
onSay:function(){}

}


var a = new foo('llinzzi');
a.onSay = function(_word){
alert('事件:'+this.name +'刚说了'+ _word);
}

// -->
</script>
</body>
</html>

 
[Ctrl+A 全部选择 提示:你可先修改部分代码,再按运行]


增加了个onSay的事件,在类定义的时候只定义成空函数,然后在需要的时候调用,定义具体的方法是在类实例化后,给实例中的onSay

缺点是只能定义一次的onSay的回调,如果多次定义后面的会把前面的覆盖掉,修改一下。

程序代码


    var foo = function(){ this.init.apply(this,arguments);};

    foo.prototype = {
        init:function(_name){
            this.name = _name;
            this.addEvent('click',document,this.say,this,'hello world!');
        },
        say:function(_ev,_word){
            alert(this.name +':' +_word);
            this.stop();
            this._onSay(_word);
        },
        stop:function(){
            this.removeEvent('click',document,this.say);
        },
        addEvent:function(_event,_element,_fn,_scope,_args){
            var args = Array.prototype.slice.call(arguments, 0); 
            ev = args.shift(),
            element = args.shift(),
            fn = args.shift(),
            scope = args.length>0?args.shift():window;
            element['e'+ev+fn] =  function(_ev){
                _ev == _ev || window.event;
                args.unshift(_ev);
                fn.apply(scope,args);
            };
            if (element.addEventListener) {
                element.addEventListener(ev, element['e'+ev+fn], false);
            } else if (element.attachEvent) {
                element.attachEvent("on" +ev, element['e'+ev+fn]);
            }
        },
        removeEvent:function(_event,_element,_fn){
            if (_element.removeEventListener) {
                _element.removeEventListener(_event, _element['e'+_event+_fn], false);
            } else if (_element.detachEvent) {
                _element.detachEvent("on" +_event, _element['e'+_event+_fn]);
            }
            try {
                delete _element['e'+_event+_fn];
            }catch(_ex){
                _element['e'+_event+_fn] = null;
            }
        },
        _onSay:function(){
            if(!this._onSayArray) return;   
            for(var i=0; i<this._onSayArray.length; i++){
                this._onSayArray[i].apply(this,arguments);
            }
        },
        addOnSay:function(_fn){
            if(!this._onSayArray) this._onSayArray = new Array();
            this._onSayArray.push(_fn);
        }
    }


var a = new foo('llinzzi');
a.addOnSay(function(_word){
    alert('事件1:'+this.name +'刚说了'+ _word);
});
a.addOnSay(function(_word){
    alert('事件2:'+this.name +'刚说了'+ _word);
});





HTML代码
<html>
<head>
<title>自定义事件</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" >
<meta name="keywords" content="" >
<meta name="description" content="" >
</head>
<body>



<script type="text/javascript">
<!--

var foo = function(){ this.init.apply(this,arguments);};

foo.prototype = {
init:function(_name){
this.name = _name;
this.addEvent('click',document,this.say,this,'hello world!');
},
say:function(_ev,_word){
alert(this.name +':' +_word);
this.stop();
this._onSay(_word);
},
stop:function(){
this.removeEvent('click',document,this.say);
},
addEvent:function(_event,_element,_fn,_scope,_args){
var args = Array.prototype.slice.call(arguments, 0); 
ev = args.shift(),
element = args.shift(),
fn = args.shift(),
scope = args.length>0?args.shift():window;
element['e'+ev+fn] =  function(_ev){
_ev == _ev || window.event;
args.unshift(_ev);
fn.apply(scope,args);
};
if (element.addEventListener) {
element.addEventListener(ev, element['e'+ev+fn], false);
} else if (element.attachEvent) {
element.attachEvent("on" +ev, element['e'+ev+fn]);
}
},
removeEvent:function(_event,_element,_fn){
if (_element.removeEventListener) {
_element.removeEventListener(_event, _element['e'+_event+_fn], false);
} else if (_element.detachEvent) {
_element.detachEvent("on" +_event, _element['e'+_event+_fn]);
}
try {
delete _element['e'+_event+_fn];
}catch(_ex){
_element['e'+_event+_fn] = null;
}
},
_onSay:function(){
if(!this._onSayArray) return;
for(var i=0; i<this._onSayArray.length; i++){
this._onSayArray[i].apply(this,arguments);
}
},
addOnSay:function(_fn){
if(!this._onSayArray) this._onSayArray = new Array();
this._onSayArray.push(_fn);
}
}


var a = new foo('llinzzi');
a.addOnSay(function(_word){
alert('事件1:'+this.name +'刚说了'+ _word);
});
a.addOnSay(function(_word){
alert('事件2:'+this.name +'刚说了'+ _word);
});

// -->
</script>
</body>
</html>

 
[Ctrl+A 全部选择 提示:你可先修改部分代码,再按运行]


实例通过addOnSay方法来增加事件响应,可以增加多个。

缺点,代码繁琐,如果要增加addOnSayAfter addOnSayBefore 就要增加很多代码。

再修改一下


程序代码


    var foo = function(){ this.init.apply(this,arguments);};

    foo.prototype = {
        init:function(_name){
            this.name = _name;
            this.addEvent('click',document,this.say,this,'hello world!');
        },
        say:function(_ev,_word){
            this.fireEvent('saybefore',_word);
            alert(this.name +':' +_word);
            this.stop();
            this.fireEvent('sayafter',_word);
        },
        stop:function(){
            this.removeEvent('click',document,this.say);
        },
        addEvent:function(_event,_element,_fn,_scope,_args){
            var args = Array.prototype.slice.call(arguments, 0); 
            ev = args.shift(),
            element = args.shift(),
            fn = args.shift(),
            scope = args.length>0?args.shift():window;
            element['e'+ev+fn] =  function(_ev){
                _ev == _ev || window.event;
                args.unshift(_ev);
                fn.apply(scope,args);
            };
            if (element.addEventListener) {
                element.addEventListener(ev, element['e'+ev+fn], false);
            } else if (element.attachEvent) {
                element.attachEvent("on" +ev, element['e'+ev+fn]);
            }
        },
        removeEvent:function(_event,_element,_fn){
            if (_element.removeEventListener) {
                _element.removeEventListener(_event, _element['e'+_event+_fn], false);
            } else if (_element.detachEvent) {
                _element.detachEvent("on" +_event, _element['e'+_event+_fn]);
            }
            try {
                delete _element['e'+_event+_fn];
            }catch(_ex){
                _element['e'+_event+_fn] = null;
            }
        },
        fireEvent:function(){
            var args = Array.prototype.slice.call(arguments, 0); 
            var _event = args.shift();
            if(!this.cusEvents) return;
            if(!this.cusEvents[_event]) return;
            for(var i=0; i<this.cusEvents[_event].length; i++){
                this.cusEvents[_event][i].apply(this,args);
            }
        },
        catchEvent:function(_event,_fn){
            if(!this.cusEvents) this.cusEvents = {}
            if(!this.cusEvents[_event]) this.cusEvents[_event] = new Array();
            this.cusEvents[_event].push(_fn);
        }
    }


var a = new foo('llinzzi');

a.catchEvent('saybefore',function(_word){
    alert('事件1:'+this.name +'想说'+ _word);
});

a.catchEvent('sayafter',function(_word){
    alert('事件2:'+this.name +'刚说了'+ _word);
});





HTML代码
<html>
<head>
<title>自定义事件</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" >
<meta name="keywords" content="" >
<meta name="description" content="" >
</head>
<body>



<script type="text/javascript">
<!--

var foo = function(){ this.init.apply(this,arguments);};

foo.prototype = {
init:function(_name){
this.name = _name;
this.addEvent('click',document,this.say,this,'hello world!');
},
say:function(_ev,_word){
this.fireEvent('saybefore',_word);
alert(this.name +':' +_word);
this.stop();
this.fireEvent('sayafter',_word);
},
stop:function(){
this.removeEvent('click',document,this.say);
},
addEvent:function(_event,_element,_fn,_scope,_args){
var args = Array.prototype.slice.call(arguments, 0); 
ev = args.shift(),
element = args.shift(),
fn = args.shift(),
scope = args.length>0?args.shift():window;
element['e'+ev+fn] =  function(_ev){
_ev == _ev || window.event;
args.unshift(_ev);
fn.apply(scope,args);
};
if (element.addEventListener) {
element.addEventListener(ev, element['e'+ev+fn], false);
} else if (element.attachEvent) {
element.attachEvent("on" +ev, element['e'+ev+fn]);
}
},
removeEvent:function(_event,_element,_fn){
if (_element.removeEventListener) {
_element.removeEventListener(_event, _element['e'+_event+_fn], false);
} else if (_element.detachEvent) {
_element.detachEvent("on" +_event, _element['e'+_event+_fn]);
}
try {
delete _element['e'+_event+_fn];
}catch(_ex){
_element['e'+_event+_fn] = null;
}
},
fireEvent:function(){
var args = Array.prototype.slice.call(arguments, 0); 
var _event = args.shift();
if(!this.cusEvents) return;
if(!this.cusEvents[_event]) return;
for(var i=0; i<this.cusEvents[_event].length; i++){
this.cusEvents[_event][i].apply(this,args);
}
},
catchEvent:function(_event,_fn){
if(!this.cusEvents) this.cusEvents = {}
if(!this.cusEvents[_event]) this.cusEvents[_event] = new Array();
this.cusEvents[_event].push(_fn);
}
}


var a = new foo('llinzzi');

a.catchEvent('saybefore',function(_word){
alert('事件1:'+this.name +'想说'+ _word);
});

a.catchEvent('sayafter',function(_word){
alert('事件2:'+this.name +'刚说了'+ _word);
});

// -->
</script>
</body>
</html>


 
[Ctrl+A 全部选择 提示:你可先修改部分代码,再按运行]


最后如果将catchEvent fireEvent addEvent removeEvent 单独放在一个类.EventHeloper
将addEvent和catchEvent整合
EventHeloper.addEvent(element/object,dom event/custom event,callback) 这样还是很方便的。


程序代码


    EventHelper = {
        Events:{Dom:{},Custom:{}},
        addEvent:function(_object,_event,_fn,_scope,_args){
            var args = Array.prototype.slice.call(arguments, 0), 
                obj = args.shift(),
                ev = args.shift(),
                fn = args.shift(),
                scope = args.length>0?args.shift():window;
            var eType = obj.nodeType?'Dom':'Custom';
            var fun;
            if(eType=='Dom'){
                sfn = function(_ev){
                    _ev == _ev || window.event;
                    args.unshift(_ev);
                    fn.apply(scope,args);
                }
                if (obj.addEventListener) {
                    obj.addEventListener(ev,sfn, false);
                } else if (obj.attachEvent) {
                    obj.attachEvent("on" +ev,sfn);
                }
            }else {
                sfn = function(){
                    var __sargs = Array.prototype.slice.call(arguments, 0);
                    var _sargs = __sargs.concat(args);
                    fn.apply(scope,_sargs);
                }   
            }
            fun = { fn:fn,sfn:sfn };
            this.Events[eType][obj] = this.Events[eType][obj] || {};
            this.Events[eType][obj][ev] = this.Events[eType][obj][ev] || new Array();
            this.Events[eType][obj][ev].push(fun);
        },
        removeEvent:function(_object,_event,_fn){
            var obj = _object,
                ev = _event,
                fn = _fn;
            var eType = obj.nodeType?'Dom':'Custom';
            if(!this.Events[eType][obj]) return;

            var fun;
            for( var i=0; i<this.Events[eType][obj][ev].length;i++){
                if(fn == this.Events[eType][obj][ev][i].fn){
                    fun = this.Events[eType][obj][ev][i].sfn;
                    this.Events[eType][obj][ev].splice(i,1);
                    break;
                }
            }
            if(eType=='Dom'){
                if (obj.removeEventListener) {
                    obj.removeEventListener(ev, fun, false);
                } else if (obj.detachEvent) {
                    obj.detachEvent("on" + ev, fun);
                }
            }

        },
        fireEvent:function(_object,_event){
            var args = Array.prototype.slice.call(arguments, 0), 
                obj = args.shift(),
                ev = args.shift();
            var eType = 'Custom';
            if((!this.Events[eType][obj]) || (!this.Events[eType][obj][ev])) return;
            for(var i=0; i<this.Events[eType][obj][ev].length; i++){
                this.Events[eType][obj][ev][i].sfn.apply(window,args);
            }
        }
    }





修改一下演示的例子

HTML代码
<html>
<head>
<title>自定义事件</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" >
<meta name="keywords" content="" >
<meta name="description" content="" >
</head>
<body>



<script type="text/javascript">
<!--

EventHelper = {
Events:{Dom:{},Custom:{}},
addEvent:function(_object,_event,_fn,_scope,_args){
var args = Array.prototype.slice.call(arguments, 0), 
obj = args.shift(),
ev = args.shift(),
fn = args.shift(),
scope = args.length>0?args.shift():window;
var eType = obj.nodeType?'Dom':'Custom';
var fun;
if(eType=='Dom'){
sfn = function(_ev){
_ev == _ev || window.event;
args.unshift(_ev);
fn.apply(scope,args);
}
if (obj.addEventListener) {
obj.addEventListener(ev,sfn, false);
} else if (obj.attachEvent) {
obj.attachEvent("on" +ev,sfn);
}
}else {
sfn = function(){
var __sargs = Array.prototype.slice.call(arguments, 0);
var _sargs = __sargs.concat(args);
fn.apply(scope,_sargs);
}
}
fun = { fn:fn,sfn:sfn };
this.Events[eType][obj] = this.Events[eType][obj] || {};
this.Events[eType][obj][ev] = this.Events[eType][obj][ev] || new Array();
this.Events[eType][obj][ev].push(fun);
},
removeEvent:function(_object,_event,_fn){
var obj = _object,
ev = _event,
fn = _fn;
var eType = obj.nodeType?'Dom':'Custom';
if(!this.Events[eType][obj]) return;

var fun;
for( var i=0; i<this.Events[eType][obj][ev].length;i++){
if(fn == this.Events[eType][obj][ev][i].fn){
fun = this.Events[eType][obj][ev][i].sfn;
this.Events[eType][obj][ev].splice(i,1);
break;
}
}
if(eType=='Dom'){
if (obj.removeEventListener) {
obj.removeEventListener(ev, fun, false);
} else if (obj.detachEvent) {
obj.detachEvent("on" + ev, fun);
}
}

},
fireEvent:function(_object,_event){
var args = Array.prototype.slice.call(arguments, 0), 
obj = args.shift(),
ev = args.shift();
var eType = 'Custom';
if((!this.Events[eType][obj]) || (!this.Events[eType][obj][ev])) return;
for(var i=0; i<this.Events[eType][obj][ev].length; i++){
this.Events[eType][obj][ev][i].sfn.apply(window,args);
}
}
}



var foo = function(){ this.init.apply(this,arguments);};

foo.prototype = {
init:function(_name){
this.name = _name;
EventHelper.addEvent(document,'click',this.say,this,'hello world!');
},
say:function(_ev,_word){
EventHelper.fireEvent(this,'saybefore',_word);
alert(this.name +':' +_word);
this.stop();
EventHelper.fireEvent(this,'sayafter',_word);
},
stop:function(){
EventHelper.removeEvent(document,'click',this.say);
}
}


var a = new foo('llinzzi');

function sayBefore(_word,_word2){
alert('事件1:'+this.name +'想说'+ _word+_word2);
}
function sayAfter(_word){
alert('事件2:'+this.name +'刚说了'+ _word);
}

EventHelper.addEvent(a,'saybefore',sayBefore,a,'外部的数据');
//EventHelper.removeEvent(a,'saybefore',sayBefore);
EventHelper.addEvent(a,'sayafter',sayBefore,a);



// -->
</script>
</body>
</html>



 
[Ctrl+A 全部选择 提示:你可先修改部分代码,再按运行]


上面例子的 EventHelper.addEvent方法可以增加自定义事件或者原生的事件
EventHelper.addEvent(document,'click',function(){alert('hello');}); // 原生事件
EventHelper.addEvent(foo,'say',function(){alert('hello');}); // 自定义时间

对应的EventHelper.removeEvent也可以移除自定义事件。


分享到:
评论

相关推荐

    Dojo 教程 笔记 (转载)

    Dojo 是一个强大的JavaScript工具库,它采用了面向对象的编程方式,对于熟悉JavaScript面向对象编程的开发者来说尤其有用。在开始学习Dojo之前,建议先掌握JavaScript的基础知识,特别是面向对象的概念,这将有助于...

    JS 拼图游戏 面向对象,注释完整。

    根据提供的文件信息,我们可以了解到这是一...以上内容涵盖了文档描述和部分代码内容中所体现的知识点,这不仅包括了基本的HTML和JavaScript编程概念,还包括了面向对象编程的具体实现,以及一个简单的游戏逻辑的设计。

    华为面试试题,很经典,转载

    【Java】在Java部分,面试可能涵盖基础语法、面向对象编程、集合框架、多线程、异常处理、IO流、JVM内存模型以及设计模式等方面。例如,可能会问到如何优化代码性能,如何处理并发问题,或者对Java 8的新特性如...

    不明飞行物:互联网转载

    3. **对象与原型**:JavaScript的面向对象编程基于原型,通过__proto__属性或Object.create方法实现对象间的继承。ES6引入的class语法糖使得面向对象编程更接近传统语言,但底层仍基于原型。 4. **异步编程**:...

    java J2EE成长之路

    它不仅包含了Java的基础知识,如面向对象编程的概念(如重载和覆盖、运行时类型与面向对象的关系),还涉及到了J2EE的关键技术,如EJB(Enterprise JavaBeans)和JSP(JavaServer Pages)。 1. **Java基础**:在...

    项目综合案例采用两年所学的内容

    学的课程有java初级 Java高级 java的面向对象 java的面向过程 java的封装 继承 多态 jdbc JavaScript初级内容 Html Css 这个学期开始前端的第二部分 jquery的基础操作 jquery的选择器 事件 效果 Css3 Html5新的属性 ...

    httpSniffer源代码

    3. **C#编程基础**:熟悉C#的语法、类库和面向对象编程概念。 4. **数据解析技术**:掌握正则表达式、XML、JSON等解析技术,用于提取网页中的关键信息。 5. **视频流格式**:了解常见的视频编码格式(如H.264、VP9)...

    JavaScript 解析读取XML文档 实例代码

    这展示了面向对象编程的概念在JavaScript中的应用。 7. 文章最后提到,使用XML可以避免使用表格(TABLE)来布局网页,这是因为在某些情况下,表格可能会导致显示效果不理想。因此,建议使用XML结合JavaScript来灵活...

    高效前端:Web高效编程与优化实践_机械工业出版社; 第1版 (2018年1月1日) 完整版-未加密

    0.2不等于0.3 203Effective前端20:明白WebAssembly与程序编译 209Effective前端21:理解JS与多线程 221Effective前端22:学会JS与面向对象 231Effective前端23:了解SQL 248Effective前端24:学习常用的前端算法与...

    JAVA文章精选542个(txt) 免费分享

    阐述了Java中的方法重载(Overloading)和方法覆盖(Overriding)概念,以及运行时类型识别(多态性)在面向对象编程中的作用。 10. **SSL與數位認證.txt**: SSL(Secure Sockets Layer)和数字认证是网络安全的...

    转载 - 26本 Ruby/Rails 相关英文图书简评

    Ruby 是一种面向对象的、动态类型的编程语言,以其简洁、优雅的语法和强大的元编程能力而闻名。Rails,全称为 Ruby on Rails,是基于 Ruby 语言的一个开源 Web 应用框架,它遵循“约定优于配置”(Convention over ...

    The Ruby Programming Language PDF

    Ruby是一种流行的开源、动态的面向对象脚本语言,由松本行弘(Yukihiro "Matz" Matsumoto)设计。它以简洁易读的语法和强大的开发效率而闻名,广泛用于Web开发、系统管理自动化、以及各种应用程序的原型设计和开发。...

    ASP 博客代码

    其中,C#是一种面向对象的编程语言,是ASP.NET的首选语言之一。C# 2.0版本引入了诸如匿名方法、迭代器、 partial类等特性,使得代码更加简洁和灵活。 **XML(可扩展标记语言):** XML是一种标记语言,主要用于...

    arthas命令总结(转载)

    1. AOP(面向切面编程):通过`advice`命令,可以在运行时动态插入代码,实现AOP功能。 2. OQL(Object Query Language):Arthas内置的查询语言,用于查询对象、类、集合等信息。 3. AScript:支持JavaScript和...

    本项目是基于SpringBoot的线上宠物物资购买系统,为个人毕业设计,未经允许禁止转载.zip

    1. **Spring Framework**: 提供了依赖注入、AOP(面向切面编程)、事务管理等核心功能。 2. **Spring Boot Auto Configuration**: 根据类路径中的jar和存在的bean自动配置应用。 3. **Spring MVC**: 实现了Model-...

    j2ee在线购物网实例源码

    3. **Spring框架**:Spring提供了依赖注入(DI)和面向切面编程(AOP),简化了业务组件的管理。 4. **Hibernate**:作为ORM(对象关系映射)工具,用于数据库操作,将Java对象与SQL数据库表进行映射。 5. **EJB...

Global site tag (gtag.js) - Google Analytics