最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

JS 建立對象的方法

 更新時間:2007年04月21日 00:00:00   作者:  
Objects are useful to organize information.
對于組織信息來講對象是非常有用的 

JavaScript Objects
JS對象
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)看過JS有一些內(nèi)置的對象,像String,Date,Array和更多一些。除此之外我們可以建立屬于自己的對象。

An object is just a special kind of data, with a collection of properties and methods.
對象是特殊的數(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.
讓我們說明一個例子:一個人為一個對象。屬性就是與對象關(guān)聯(lián)的值。人的屬性包含名字,身高,體重,年齡,膚色,眼睛的顏色等等。所有人都有這些屬性,但是值卻可能人與人都不同。對象還有方法。方法就是對象的動作行為。人的方法就可以是eat()[吃],sleep()[睡覺],work()[工作]等等。

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

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:
你可以通過賦值來給對象添加屬性。假設(shè)personObj已經(jīng)存在 - 你可以給對象添加姓和名以及下面的年紀和眼睛顏色:

personObj.firstname="John"

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

The code above will generate the following output:
上面的代碼就會輸出:

John 

Methods方法
An object can also contain methods.
一個對象還可以包括方法

You can call a method with the following syntax:
你可以用下面的語法來調(diào)用一個方法:

objName.methodName() 

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

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

personObj.sleep() 


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

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

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

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

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:
給對象建立一個方法也十分的簡單。下面的代碼就加了一個eat()方法

personObj.eat=eat 

2. Create a template of an object
建立一個對象模塊

The template defines the structure of an object:
模塊定義對象的構(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.
注意模塊只是一個函數(shù),函數(shù)里面你需要給this.propertyName分配東西。所有都是"this"的原因是你接下來會一下子有不止一個person(是哪個person你必須清楚)。

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

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對象,這也可以在模塊里完成:

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:
注意,這個方法只是對象的附加函數(shù),接下來我們將必須寫入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會明白你在描述哪個person。所以現(xiàn)在你可以寫:myMother.newlastname("Doe") 

相關(guān)文章

  • javascript學習筆記整理(概述、變量、數(shù)據(jù)類型簡介)

    javascript學習筆記整理(概述、變量、數(shù)據(jù)類型簡介)

    這篇文章主要介紹了javascript學習筆記整理(概述-變量-數(shù)據(jù)類型),需要的朋友可以參考下
    2015-10-10
  • Js從頭學起(基本數(shù)據(jù)類型和引用類型的參數(shù)傳遞詳細分析)

    Js從頭學起(基本數(shù)據(jù)類型和引用類型的參數(shù)傳遞詳細分析)

    Js中所有函數(shù)的參數(shù)傳遞都是按值傳遞的,也就是把函數(shù)外面的值復制給函數(shù)內(nèi)部的參數(shù),就和把值從一個變量復制到另一個變量一樣。下面舉幾個特別的例子
    2012-02-02
  • JSON對象 詳解及實例代碼

    JSON對象 詳解及實例代碼

    這篇文章主要介紹了JSON對象 詳解的相關(guān)資料,并附簡單實例代碼,幫助大家學習參考,需要的朋友可以參考下
    2016-10-10
  • 深入理解JavaScript系列(49):Function模式(上篇)

    深入理解JavaScript系列(49):Function模式(上篇)

    這篇文章主要介紹了深入理解JavaScript系列(49):Function模式(上篇),本文講解了回調(diào)函數(shù)、配置對象、返回函數(shù)、偏應用、Currying等內(nèi)容,需要的朋友可以參考下
    2015-03-03
  • element?$notify?懸浮通知使用詳解

    element?$notify?懸浮通知使用詳解

    這篇文章主要介紹了element?$notify?懸浮通知使用詳解,主要講解如何使用this.$notify做一個懸浮通知,本文通過示例代碼給大家介紹的非常詳細,感興趣的朋友一起看看吧
    2020-06-06
  • Angularjs 設(shè)置全局變量的方法總結(jié)

    Angularjs 設(shè)置全局變量的方法總結(jié)

    這篇文章主要介紹了Angularjs 設(shè)置全局變量的方法總結(jié)的相關(guān)資料,需要的朋友可以參考下
    2016-10-10
  • JavaScript箭頭(arrow)函數(shù)詳解

    JavaScript箭頭(arrow)函數(shù)詳解

    本文講的是從初學者的角度詳解Javascript ES6中的箭頭函數(shù)(Arrow Functions)的基礎(chǔ)知識, ES6可以使用“箭頭”定義函數(shù),注意是函數(shù),不要使用這種方式定義類(構(gòu)造器)
    2017-06-06
  • JavaScript入門之對象與JSON詳解

    JavaScript入門之對象與JSON詳解

    JSON是JavaScript中對象的字面量,是對象的表示方法,通過使用JSON,可以減少中間變量,使代碼的結(jié)構(gòu)更加清晰,也更加直觀。使用JSON,可以動態(tài)的構(gòu)建對象,而不必通過類來進行實例化,大大的提高了編碼的效率
    2011-10-10
  • JavaScript閉包詳解

    JavaScript閉包詳解

    本文詳細介紹了javascript閉包,十分的詳盡,推薦給有需要的小伙伴參考下。
    2015-02-02
  • 關(guān)于JavaScript的Array數(shù)組方法詳解

    關(guān)于JavaScript的Array數(shù)組方法詳解

    這篇文章主要介紹了關(guān)于JavaScript的Array數(shù)組方法詳解,數(shù)組是一個固定長度的存儲相同數(shù)據(jù)類型的數(shù)據(jù)結(jié)構(gòu),數(shù)組中的元素被存儲在一段連續(xù)的內(nèi)存空間中,它是最簡單的數(shù)據(jù)結(jié)構(gòu)之一,需要的朋友可以參考下
    2023-05-05

最新評論

陈巴尔虎旗| 正镶白旗| 铁岭县| 沙雅县| 通化市| 安阳市| 织金县| 长沙县| 枞阳县| 大名县| 平南县| 同江市| 夏河县| 定南县| 陇南市| 丹阳市| 且末县| 临安市| 河池市| 新闻| 荥经县| 天门市| 东乡县| 霍城县| 永济市| 怀宁县| 建瓯市| 西充县| 揭东县| 阳东县| 长葛市| 剑阁县| 达日县| 手游| 万载县| 泰和县| 潢川县| 衡东县| 萍乡市| 邯郸市| 府谷县|