日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

JS面向对象编程实现

發布時間:2023/12/18 javascript 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 JS面向对象编程实现 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Function 在 中是一個很特殊的對象,其特殊性體現在它的多重身份。

Function 可以聲明普通的函數,這一點和其他語言中的函數概念是相同的。除此以外,Function還可以用作類型的聲明和實現、對象的構造函數,以及類引用。

Apply和Call方法可以將函數綁定到其它對象上執行。

使用for(…in…)語句可以遍歷對象的所有屬性和方法。如下面的例子就遍歷了test1對象的屬性和方法,如果是屬性剛輸出屬性,如果是方法剛執行方法。

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)

{

?????? //如果是函數類型剛執行該函數

?????? if(typeof(t[p])=="function")

?????? t[p]();

?????? //輸出其它類型的屬性

?????? else alert(t[p]);

}

1.?????? 類的聲明

(1)、使用this關鍵字

function test1()

{

?????? this.p1="p1";

?????? this.p2="p2";

?????? this.f1=function()

?????? {

????????????? alert("f1");

?????? }

?????? this.f2=function()

?????? {

????????????? alert("f2");

?????? }

}

在中,成員變量沒有私有與公有機制,但可以通過在類內部使用var來聲明局部變量。其作用域是類定義的內部。

?

(2)、使用ptototype方式的類聲明

如果要聲明一個類的實例屬性或方法,可以使用中的對象的prototype屬性。例如:

Test1.prototype.prop2=”prop2”;

Test1.prototype.method2=function(){

??? Alert(this.prop2);

}

使用prototype屬性聲明類,如下

function test()

{???????????

}

test.prototype={

?????? p1:"p1";

?????? p2:"p2";

?????? f1 : funciton ()

?????? {

????????????? alert("f2");

?????? }

}

2.?????? 繼承

本身并沒有提供繼承的語法支持,但是在仍然可以采用復制屬性和對象的方法實現繼承。

function test2(){}

for(p in test.prototype)

{

test2.prototype[p]=test.prototype[p];

}

test2.prototype.newMethod=function()

{

alert("newMethod");

}

另外可以參考Prototype框架實現繼承的方法:

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.?????? 多態

多態的實現可以采用和繼承類似的方法。首定義一個抽象類,其中可以,調 用一些虛方法,虛方法在抽象類中沒有定義,而是通過其具體實現類來實現的。例如:

object.extend=function (destination,source)

{

for(property in source)

{

??????? destination[property]=source[property];

}

return destiantion;

}

//定義一個抽象基類

function base() {}

base.prototype={???

initialize:function(){

??????? this.oninit();//調用了一個虛方法

}

}

?

function test()

{

//構造函數

}

?

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

?????????????????????????????????????????? ? }

轉載于:https://www.cnblogs.com/Frontview/archive/2008/11/07/1328631.html

總結

以上是生活随笔為你收集整理的JS面向对象编程实现的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。