国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 語(yǔ)言 > JavaScript > 正文

JS之建立對(duì)象的方法

2024-05-06 15:45:19
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

無(wú)論是什么語(yǔ)言,不斷變化的語(yǔ)言都離不開(kāi)它的起源,只有深刻理解它的本質(zhì),才能自由運(yùn)用,感興趣的小伙伴們可以和錯(cuò)新技術(shù)頻道小編一起來(lái)參考一下JS之建立對(duì)象的方法吧!

Objects?are?useful?to?organize?information.
對(duì)于組織信息來(lái)講對(duì)象是非常有用的?

JavaScript?Objects
JS對(duì)象
Earlier?in?this?tutorial?we?have?seen?that?JavaScript?has?several?built-in?objects,?like?String,?Date,?Array,?and?more.?In?addition?to?these?built-in?objects,?you?can?also?create?your?own.
在教程的前面部分我們已經(jīng)看過(guò)JS有一些內(nèi)置的對(duì)象,像String,Date,Array和更多一些。除此之外我們可以建立屬于自己的對(duì)象。

An?object?is?just?a?special?kind?of?data,?with?a?collection?of?properties?and?methods.
對(duì)象是特殊的數(shù)據(jù),有著相關(guān)的一系列屬性和方法。

Let's?illustrate?with?an?example:?A?person?is?an?object.?Properties?are?the?values?associated?with?the?object.?The?persons'?properties?include?name,?height,?weight,?age,?skin?tone,?eye?color,?etc.?All?persons?have?these?properties,?but?the?values?of?those?properties?will?differ?from?person?to?person.?Objects?also?have?methods.?Methods?are?the?actions?that?can?be?performed?on?objects.?The?persons'?methods?could?be?eat(),?sleep(),?work(),?play(),?etc.
讓我們說(shuō)明一個(gè)例子:一個(gè)人為一個(gè)對(duì)象。屬性就是與對(duì)象關(guān)聯(lián)的值。人的屬性包含名字,身高,體重,年齡,膚色,眼睛的顏色等等。所有人都有這些屬性,但是值卻可能人與人都不同。對(duì)象還有方法。方法就是對(duì)象的動(dòng)作行為。人的方法就可以是eat()[吃],sleep()[睡覺(jué)],work()[工作]等等。

Properties屬性
The?syntax?for?accessing?a?property?of?an?object?is:
關(guān)聯(lián)一個(gè)對(duì)象的屬性語(yǔ)法為:

objName.propName?

You?can?add?properties?to?an?object?by?simply?giving?it?a?value.?Assume?that?the?personObj?already?exists?-?you?can?give?it?properties?named?firstname,?lastname,?age,?and?eyecolor?as?follows:
你可以通過(guò)賦值來(lái)給對(duì)象添加屬性。假設(shè)personObj已經(jīng)存在?-?你可以給對(duì)象添加姓和名以及下面的年紀(jì)和眼睛顏色:

personObj.firstname="John"

personObj.lastname="Doe"
personObj.age=30
personObj.eyecolor="blue"document.write(personObj.firstname)?

The?code?above?will?generate?the?following?output:
上面的代碼就會(huì)輸出:

John?

Methods方法
An?object?can?also?contain?methods.
一個(gè)對(duì)象還可以包括方法

You?can?call?a?method?with?the?following?syntax:
你可以用下面的語(yǔ)法來(lái)調(diào)用一個(gè)方法:

objName.methodName()?

Note:?Parameters?required?for?the?method?can?be?passed?between?the?parentheses.
方法所需要的參數(shù)寫(xiě)在括號(hào)之間

To?call?a?method?called?sleep()?for?the?personObj:
為personObj對(duì)象調(diào)用一個(gè)sleep()方法

personObj.sleep()?


--------------------------------------------------------------------------------

Creating?Your?Own?Objects
建立你自己的對(duì)象
There?are?different?ways?to?create?a?new?object:
建立新的對(duì)象有兩種不同的方法

1.?Create?a?direct?instance?of?an?object
直接建立

The?following?code?creates?an?instance?of?an?object?and?adds?four?properties?to?it:
下面的代碼可以直接建立一個(gè)對(duì)象并給它加上四個(gè)屬性:

personObj=new?Object()
personObj.firstname="John"

personObj.lastname="Doe"
personObj.age=50
personObj.eyecolor="blue"?

Adding?a?method?to?the?personObj?is?also?simple.?The?following?code?adds?a?method?called?eat()?to?the?personObj:
給對(duì)象建立一個(gè)方法也十分的簡(jiǎn)單。下面的代碼就加了一個(gè)eat()方法

personObj.eat=eat?

2.?Create?a?template?of?an?object
建立一個(gè)對(duì)象模塊

The?template?defines?the?structure?of?an?object:
模塊定義對(duì)象的構(gòu)架

function?person(firstname,lastname,age,eyecolor)
{
this.firstname=firstname
this.lastname=lastname
this.age=age
this.eyecolor=eyecolor
}?

Notice?that?the?template?is?just?a?function.?Inside?the?function?you?need?to?assign?things?to?this.propertyName.?The?reason?for?all?the?"this"?stuff?in?is?that?you're?going?to?have?more?than?one?person?at?a?time?(which?person?you're?dealing?with?must?be?clear).?That's?what?"this"?is:?the?instance?of?the?object?at?hand.
注意模塊只是一個(gè)函數(shù),函數(shù)里面你需要給this.propertyName分配東西。所有都是"this"的原因是你接下來(lái)會(huì)一下子有不止一個(gè)person(是哪個(gè)person你必須清楚)。

Once?you?have?the?template,?you?can?create?new?instances?of?the?object,?like?this:
一旦你有了模塊,你就可以這樣直接建立新的對(duì)象了:

myFather=new?person("John","Doe",50,"blue")
myMother=new?person("Sally","Rally",48,"green")?

You?can?also?add?some?methods?to?the?person?object.?This?is?also?done?inside?the?template:
你也可以加一些方法給person對(duì)象,這也可以在模塊里完成:

function?person(firstname,lastname,age,eyecolor)
{
this.firstname=firstname
this.lastname=lastname
this.age=age
this.eyecolor=eyecolorthis.newlastname=newlastname
}?

Note?that?methods?are?just?functions?attached?to?objects.?Then?we?will?have?to?write?the?newlastname()?function:
注意,這個(gè)方法只是對(duì)象的附加函數(shù),接下來(lái)我們將必須寫(xiě)入newlastname()函數(shù)

function?newlastname(new_lastname)
{
this.lastname=new_lastname
}?

The?newlastname()?function?defines?the?person's?new?last?name?and?assigns?that?to?the?person.?JavaScript?knows?which?person?you're?talking?about?by?using?"this.".?So,?now?you?can?write:?myMother.newlastname("Doe").
newlastname()函數(shù)定義了person的新last?name并分配給了person。使用"this"的話JS會(huì)明白你在描述哪個(gè)person。所以現(xiàn)在你可以寫(xiě):myMother.newlastname("Doe")?
以上就是JS之建立對(duì)象的方法?,更多的內(nèi)容請(qǐng)繼續(xù)關(guān)注錯(cuò)新技術(shù)頻道的其它相關(guān)文章吧,感謝大家對(duì)錯(cuò)新技術(shù)頻道網(wǎng)的支持!

發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 福贡县| 潜江市| 沾化县| 乐清市| 萨迦县| 公安县| 西华县| 诸暨市| 财经| 太保市| 金溪县| 阿图什市| 宁津县| 绥宁县| 来凤县| 郑州市| 泰顺县| 康马县| 东山县| 高唐县| 普格县| 芜湖县| 海原县| 钦州市| 翁牛特旗| 丹寨县| 东乡族自治县| 武定县| 游戏| 长岭县| 湖北省| 绥中县| 临江市| 霍城县| 长武县| 凤城市| 岗巴县| 岗巴县| 和硕县| 榆社县| 娄烦县|