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();

转载于:https://www.cnblogs.com/livexy/archive/2010/07/08/1773780.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值