javascript
JS面向对象编程实现
Function 在 中是一個(gè)很特殊的對(duì)象,其特殊性體現(xiàn)在它的多重身份。
Function 可以聲明普通的函數(shù),這一點(diǎn)和其他語(yǔ)言中的函數(shù)概念是相同的。除此以外,Function還可以用作類(lèi)型的聲明和實(shí)現(xiàn)、對(duì)象的構(gòu)造函數(shù),以及類(lèi)引用。
Apply和Call方法可以將函數(shù)綁定到其它對(duì)象上執(zhí)行。
使用for(…in…)語(yǔ)句可以遍歷對(duì)象的所有屬性和方法。如下面的例子就遍歷了test1對(duì)象的屬性和方法,如果是屬性剛輸出屬性,如果是方法剛執(zhí)行方法。
function test1()
{
?????? this.p1="p1";
?????? this.p2="p2";
?????? this.f1=function()
?????? {
????????????? alert("f1");
?????? }
?????? this.f2=function()
?????? {
????????????? alert("f2");
?????? }
}
var obj1=new test1();
//遍歷t的所有屬性
for(p in t)
{
?????? //如果是函數(shù)類(lèi)型剛執(zhí)行該函數(shù)
?????? if(typeof(t[p])=="function")
?????? t[p]();
?????? //輸出其它類(lèi)型的屬性
?????? else alert(t[p]);
}
1.?????? 類(lèi)的聲明
(1)、使用this關(guān)鍵字
function test1()
{
?????? this.p1="p1";
?????? this.p2="p2";
?????? this.f1=function()
?????? {
????????????? alert("f1");
?????? }
?????? this.f2=function()
?????? {
????????????? alert("f2");
?????? }
}
在中,成員變量沒(méi)有私有與公有機(jī)制,但可以通過(guò)在類(lèi)內(nèi)部使用var來(lái)聲明局部變量。其作用域是類(lèi)定義的內(nèi)部。
?
(2)、使用ptototype方式的類(lèi)聲明
如果要聲明一個(gè)類(lèi)的實(shí)例屬性或方法,可以使用中的對(duì)象的prototype屬性。例如:
Test1.prototype.prop2=”prop2”;
Test1.prototype.method2=function(){
??? Alert(this.prop2);
}
使用prototype屬性聲明類(lèi),如下
function test()
{???????????
}
test.prototype={
?????? p1:"p1";
?????? p2:"p2";
?????? f1 : funciton ()
?????? {
????????????? alert("f2");
?????? }
}
2.?????? 繼承
本身并沒(méi)有提供繼承的語(yǔ)法支持,但是在仍然可以采用復(fù)制屬性和對(duì)象的方法實(shí)現(xiàn)繼承。
function test2(){}
for(p in test.prototype)
{
test2.prototype[p]=test.prototype[p];
}
test2.prototype.newMethod=function()
{
alert("newMethod");
}
另外可以參考Prototype框架實(shí)現(xiàn)繼承的方法:
object.extend=function (destination,source)
{
for(property in source)
{
??????? destination[property]=source[property];
}
return destiantion;
}
function test2() {}
test2.prototype=object.extend ({newMethod :function ()
{alert (“newMethod”);}
},
Test.prototype
);
3.?????? 多態(tài)
多態(tài)的實(shí)現(xiàn)可以采用和繼承類(lèi)似的方法。首定義一個(gè)抽象類(lèi),其中可以,調(diào) 用一些虛方法,虛方法在抽象類(lèi)中沒(méi)有定義,而是通過(guò)其具體實(shí)現(xiàn)類(lèi)來(lái)實(shí)現(xiàn)的。例如:
object.extend=function (destination,source)
{
for(property in source)
{
??????? destination[property]=source[property];
}
return destiantion;
}
//定義一個(gè)抽象基類(lèi)
function base() {}
base.prototype={???
initialize:function(){
??????? this.oninit();//調(diào)用了一個(gè)虛方法
}
}
?
function test()
{
//構(gòu)造函數(shù)
}
?
test.prototype=object.extend(
?????????????????????????????????????????? ?{
????????????????????????????????????????????????? ?prop:"prop",
????????????????????????????????????????????????? ?oninit :function()
????????????????????????????????????????????????? ?{
???????????????????????????????????????????????????????? ?alert(this.prop)
????????????????????????????????????????????????? ?}
?????????????????????????????????????????? ?},base.prototype
?????????????????????????????????????????? ?);
?
function test2()
{
}
?
test2.prototype=object.extend(
?????????????????????????????????????????? ? {
????????????????????????????????????????????????? ? prop2: "prop2";
????????????????????????????????????????????????? ? oninit:function()
????????????????????????????????????????????????? ? {
???????????????????????????????????????????????????????? ? alert(this.prop2);
????????????????????????????????????????????????? ? }
?????????????????????????????????????????? ? },base.prototype
?????????????????????????????????????????? ? }
轉(zhuǎn)載于:https://www.cnblogs.com/Frontview/archive/2008/11/07/1328631.html
總結(jié)
以上是生活随笔為你收集整理的JS面向对象编程实现的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: vue获取商品数据接口_基于 reque
- 下一篇: java js关键字_JavaScri