我的书中的部分函数
var GLOBAL = {};
GLOBAL.namespace = function(str){
var arr = str.split("."),o = GLOBAL;
for (i=(arr[0] == "GLOBAL") ? 1 : 0; i<arr.length; i++) {
o[arr[i]]=o[arr[i]] || {};
o=o[arr[i]];
}
}
GLOBAL.namespace("Dom");
GLOBAL.Dom.getElementsByClassName = function(str,root,tag){
if(root){
root = typeof root == "string" ? document.getElementById(root) : root;
} else {
root = document.body;
}
tag = tag || "*";
var els = root.getElementsByTagName(tag),arr = [];
for(var i=0,n=els.length;i<n;i++){
for(var j=0,k=els[i].className.split(" "),l=k.length;j<l;j++){
if(k[j] == str){
arr.push(els[i]);
break;
}
}
}
return arr;
}
GLOBAL.namespace("Event");
GLOBAL.Event.on = function(node,eventType,handler,scope){
node = typeof node == "string" ? document.getElementById(node) : node;
scope = scope || node;
if(document.all){
node.attachEvent("on"+eventType,function(){handler.apply(scope,arguments)});
} else {
node.addEventListener(eventType,function(){handler.apply(scope,arguments)},false);
}
}
function extend(subClass,superClass){
var F = function(){};
F.prototype = superClass.prototype;
subClass.prototype = new F();
subClass.prototype.constructor = subClass;
subClass.superclass = superClass.prototype;
if(superClass.prototype.constructor == Object.prototype.constructor){
superClass.prototype.constructor = superClass;
}
}
function Animal(name){
this.name = name;
this.type = "animal";
}
Animal.prototype = {
say : function(){
alert("I'm a(an) " + this.type + " , my name is " + this.name);
}
}
function Bird(name){
this.constructor.superclass.constructor.apply(this,arguments);
this.type = "bird"
}
extend(Bird,Animal);
Bird.prototype.fly = function(){
alert("I'm flying");
}
var canary = new Bird("xiaocui");
canary.say(); // I’m a(an) bird , my name is xiaocui
canary.fly(); // I’m flying