JS 建立对象的方法


Posted in Javascript onApril 21, 2007

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 相关文章推荐
javascript实现跳转菜单的具体方法
Jul 05 Javascript
js转化毫秒为时间格式代码
Apr 10 Javascript
js实现支持手机滑动切换的轮播图片效果实例
Apr 29 Javascript
JS实现网页每隔3秒弹出一次对话框的方法
Nov 09 Javascript
使用JavaScript实现alert的实例代码
Jul 06 Javascript
详解使用React全家桶搭建一个后台管理系统
Nov 04 Javascript
Angular17之Angular自定义指令详解
Jan 21 Javascript
如何使用puppet替换文件中的string
Dec 06 Javascript
深入理解 ES6中的 Reflect用法
Jul 18 Javascript
JS异步宏队列微队列原理详解
Sep 09 Javascript
vue3为什么要用proxy替代defineProperty
Oct 19 Javascript
JavaScript原型链详解
Nov 07 Javascript
如何做到打开一个页面,过几分钟自动转到另一页面
Apr 20 #Javascript
用javascript将数据库中的TEXT类型数据动态赋值到TEXTAREA中
Apr 20 #Javascript
在textarea中显示html页面的javascript代码
Apr 20 #Javascript
textarea的value是html文件源代码,存成html文件的代码
Apr 20 #Javascript
在textarea中屏蔽js的某个function的javascript代码
Apr 20 #Javascript
用javascript实现改变TEXTAREA滚动条和按钮的颜色,以及怎样让滚动条变得扁平
Apr 20 #Javascript
让textarea控件的滚动条怎是位与最下方
Apr 20 #Javascript
You might like
SONY SRF-22W(33W)的电路分析和维修案例
2021/03/02 无线电
配置支持SSI
2006/11/25 PHP
Http 1.1 Etag 与 Last-Modified提高php效率
2008/01/10 PHP
PHPMYADMIN导入数据最大为2M的解决方法
2012/04/23 PHP
使用PHP静态变量当缓存的方法
2013/11/13 PHP
php微信开发之批量生成带参数的二维码
2016/06/26 PHP
js数字输入框(包括最大值最小值限制和四舍五入)
2009/11/24 Javascript
JavaScript中使用stopPropagation函数停止事件传播例子
2014/08/27 Javascript
jQuery实现拖动调整表格单元格大小的代码实例
2015/01/13 Javascript
javascript实现带节日和农历的日历特效
2015/02/01 Javascript
jQuery+slidereveal实现的面板滑动侧边展出效果
2015/03/14 Javascript
js正则匹配出所有图片及图片地址src的方法
2015/06/08 Javascript
angularjs自定义ng-model标签的属性
2016/01/21 Javascript
js中实现字符串和数组的相互转化详解
2016/01/24 Javascript
简单实现Bootstrap标签页
2020/08/09 Javascript
JS仿淘宝搜索框用户输入事件的实现
2017/06/19 Javascript
nodejs判断文件、文件夹是否存在及删除的方法
2017/11/10 NodeJs
layui 表格的属性的显示转换方法
2018/08/14 Javascript
微信小程序 冒泡事件原理解析
2019/09/27 Javascript
Vue 数据响应式相关总结
2021/01/28 Vue.js
[01:55]《走出家门看比赛》——DOTA2 2015国际邀请赛同城线下观战
2015/07/18 DOTA
深入浅出学习python装饰器
2017/09/29 Python
利用numpy和pandas处理csv文件中的时间方法
2018/04/19 Python
python实现简单登陆系统
2018/10/18 Python
python django下载大的csv文件实现方法分析
2019/07/19 Python
彻底搞懂 python 中文乱码问题(深入分析)
2020/02/28 Python
python缩进长度是否统一
2020/08/02 Python
详解Python openpyxl库的基本应用
2021/02/26 Python
英语翻译系毕业生求职信
2013/09/29 职场文书
在求职信中如何凸显个人优势
2013/10/30 职场文书
关于圣诞节的广播稿
2014/01/26 职场文书
小学三八妇女节活动方案
2014/03/16 职场文书
安全生产月活动总结
2014/05/04 职场文书
2015年安全生产目标责任书
2015/01/29 职场文书
2015年教研工作总结
2015/05/23 职场文书
Golang 实现 WebSockets 之创建 WebSockets
2022/04/24 Golang