JS OO 开发简单实例

//================================== JS OO 开发简单实例 ==================================
//========== 对像扩展 ====================================================================

 

var OO1 = {
a :
function(){ alert("OO1a"); },
b :
function(){ alert("OO1b"); }
};
OO1.c
= function(){ alert("OO1c"); }
OO1.d
= function(){ alert("OO1d"); }
OO1[
"e"] = function(){ alert("OO1e"); }
OO1[
"f"] = function(){ alert("OO1f"); }
//OO1.a(); OO1.b(); OO1.c(); OO1.d(); OO1.e(); OO1.f();
//
不能原生扩展 如下会出错
//
OO1.prototype.g = function(){ alert("OO1g"); }
//
OO1.prototype.h = function(){ alert("OO1h"); }

 

//========== 函数扩展 ====================================================================

 

 

var OO2 = function() {
this.a = function() { alert("OO4a"); }
this.b = function() { alert("OO4b"); }
};
OO2.c
= function(){ alert("OO2c"); }
OO2.d
= function(){ alert("OO2d"); }
OO2[
"e"] = function(){ alert("OO2e"); }
OO2[
"f"] = function(){ alert("OO2f"); }
OO2.prototype.g
= function(){ alert("OO2g"); }
OO2.prototype.h
= function(){ alert("OO2h"); }
OO2.prototype
= {
i :
function(){ alert("OO2i"); },
j :
function(){ alert("OO2j"); }
}
//OO2.c(); OO2.d(); OO2.e(); OO2.f();

//函数原生扩展和this扩展需要实例化后访问
var testOO2 = new OO2()
//testOO2.a(); testOO2.b(); testOO2.g(); testOO2.h(); testOO2.i(); testOO2.j();

 

//========== 系统原生对象扩展 ============================================================

 

 

var OO3 = Function;
OO3.prototype[
"e"] = function(){ alert("Function.e"); } // 等同于 Function.prototype["e"] = function(){ alert("Function.e"); }
OO3.prototype["f"] = function(){ alert("Function.f"); }
OO3.prototype.g
= function(){ alert("Function.g"); } // 等同于 Function.prototype.g = function(){ alert("Function.g"); }
OO3.prototype.h = function(){ alert("Function.h"); }
//OO3.e(); OO3.f(); OO3.g(); OO3.h();

var testOO3 = function(){}; // 等同于 var testOO3 = new OO3();
//
testOO3.e(); testOO3.f(); testOO3.g(); testOO3.h();

//========== OO1.Class 是命名空间 ============================================================
OO1.Class = {}

OO1.Class.Session
= {
set :
function(){ alert("Session.set"); },
get :
function(){ alert("Session.get"); }
}

//OO1.Class.Session.set(); OO1.Class.Session.get();
var session = OO1.Class.Session;
//session.set(); session.get();

 

 

//========== extend =========================================================================

Object.prototype.extend = function() {
var source, key, len = arguments.length, destination = this,
isbool
= typeof arguments[len-1] == 'boolean' , override = isbool ? arguments[len-1] : true;
if (len == 0) return null;
for (var i = 0,len = isbool ? len - 1 : len; i < len; i++) {
source
= arguments[i];
for (key in source) { if (override || !(key in destination)) destination[key] = source[key]; };
};
return destination;
};

var oo5 = function () {}
oo5.o
= function(){ alert("OO5o"); }
oo5.p
= function(){ alert("OO5p"); }

var oo5 = oo5.extend({
x :
function(){ alert("OO5x"); },
y :
function(){ alert("OO5y"); }
},{
m :
function(){ alert("OO5m"); },
n :
function(){ alert("OO5n"); }
},{
x :
function(){ alert("OO5x1"); },
y :
function(){ alert("OO5y1"); }
},OO1,
false);

//oo5.x();oo5.y();oo5.m();oo5.n();oo5.a();oo5.b();oo5.c();oo5.d();oo5.o();oo5.p();
posted @ 2010-07-08 15:57  熊哥  阅读(2542)  评论(0编辑  收藏  举报