JavaScript Objects JS對象 Earlier in this tutorial we have seen that JavaScript has several built-in objects, like Strin " /> 趁老师睡着吃她的奶水,亚洲理论片在线中文字幕,中文字幕亚洲无线码高清不卡

天天躁日日躁狠狠躁AV麻豆-天天躁人人躁人人躁狂躁-天天澡夜夜澡人人澡-天天影视香色欲综合网-国产成人女人在线视频观看-国产成人女人视频在线观看

JS 建立對象的方法

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.
在教程的前面部分我們已經看過JS有一些內置的對象,像String,Date,Array和更多一些。除此之外我們可以建立屬于自己的對象。

An object is just a special kind of data, with a collection of properties and methods.
對象是特殊的數據,有著相關的一系列屬性和方法。

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.
讓我們說明一個例子:一個人為一個對象。屬性就是與對象關聯的值。人的屬性包含名字,身高,體重,年齡,膚色,眼睛的顏色等等。所有人都有這些屬性,但是值卻可能人與人都不同。對象還有方法。方法就是對象的動作行為。人的方法就可以是eat()[吃],sleep()[睡覺],work()[工作]等等。

Properties屬性
The syntax for accessing a property of an object is:
關聯一個對象的屬性語法為:

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

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:
你可以用下面的語法來調用一個方法:

objName.methodName() 

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

To call a method called sleep() for the personObj:
為personObj對象調用一個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:
模塊定義對象的構架

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.
注意模塊只是一個函數,函數里面你需要給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:
注意,這個方法只是對象的附加函數,接下來我們將必須寫入newlastname()函數

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()函數定義了person的新last name并分配給了person。使用"this"的話JS會明白你在描述哪個person。所以現在你可以寫:myMother.newlastname("Doe") 

JavaScript技術JS 建立對象的方法,轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: 少妇人妻偷人精品视蜜桃 | 无止侵犯高H1V3无止侵犯 | 久欠热视频精品首页 | 日韩a在线看免费观看视频 日韩a视频在线观看 | 三级视频黄色 | 暖暖视频免费观看高清完整版 | 久久re热在线视频精99 | 欧美阿v在线免播播放 | 成人国内精品久久久久影院 | 内射后入在线观看一区 | 国产爱豆剧果冻传媒在线 | 一区二一二 | 学校捏奶揉下面污文h | 热热久久超碰精品中文字幕 | 国产成人无码WWW免费视频在线 | 暖暖高清视频免费 | 性欧美13处14处破 | 亚洲午夜久久久无码精品网红A片 | 国内精品免费视频精选在线观看 | 国产爱豆果冻传媒在线观看视频 | 激情午夜307 | lesbabes性欧美| 狠狠啪 日日啪 | 纲手胸被爆羞羞免费 | 麻豆精品一区二正一三区 | 国产人妻777人伦精品HD | 国产精品2020观看久久 | 大胸美女被cao哭 | 手机在线看片欧美亚洲 | BT7086福利二区最新 | 亚洲第一页在线播放 | 无人区乱码1区2区3区网站 | 内射一区二区精品视频在线观看 | 久久成人免费观看全部免费 | 亚洲欧美高清在线精品一区 | 歪歪爽蜜臀AV久久精品人人槡 | 中国xxx视频 | 内射一区二区精品视频在线观看 | 免费精品国产人妻国语麻豆 | 国产欧美一区二区三区视频 | 国产中文视频无码成人精品 |