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 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")
标签:
JS,建立对象的方法
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
暂无“JS 建立对象的方法”评论...
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新动态
2024年11月09日
2024年11月09日
- 雨林唱片《赏》新曲+精选集SACD版[ISO][2.3G]
- 罗大佑与OK男女合唱团.1995-再会吧!素兰【音乐工厂】【WAV+CUE】
- 草蜢.1993-宝贝对不起(国)【宝丽金】【WAV+CUE】
- 杨培安.2009-抒·情(EP)【擎天娱乐】【WAV+CUE】
- 周慧敏《EndlessDream》[WAV+CUE]
- 彭芳《纯色角3》2007[WAV+CUE]
- 江志丰2008-今生为你[豪记][WAV+CUE]
- 罗大佑1994《恋曲2000》音乐工厂[WAV+CUE][1G]
- 群星《一首歌一个故事》赵英俊某些作品重唱企划[FLAC分轨][1G]
- 群星《网易云英文歌曲播放量TOP100》[MP3][1G]
- 方大同.2024-梦想家TheDreamer【赋音乐】【FLAC分轨】
- 李慧珍.2007-爱死了【华谊兄弟】【WAV+CUE】
- 王大文.2019-国际太空站【环球】【FLAC分轨】
- 群星《2022超好听的十倍音质网络歌曲(163)》U盘音乐[WAV分轨][1.1G]
- 童丽《啼笑姻缘》头版限量编号24K金碟[低速原抓WAV+CUE][1.1G]