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 相关文章推荐
QQ邮箱的一个文本编辑器代码
Mar 14 Javascript
jquery搜索框效果实现方法
Jan 16 Javascript
js实现仿百度风云榜可重复多次调用的TAB切换选项卡效果
Aug 31 Javascript
关于JS中prototype的理解
Sep 07 Javascript
javascript数据类型验证方法
Dec 31 Javascript
jQuery ajax调用后台aspx后台文件的两种常见方法(不是ashx)
Jun 28 Javascript
js删除数组元素、清空数组的简单方法(必看)
Jul 27 Javascript
详解js树形控件—zTree使用总结
Dec 28 Javascript
Node.js 中exports 和 module.exports 的区别
Mar 14 Javascript
JavaScript编写棋盘覆盖代码详解
Aug 28 Javascript
js阻止默认右键的下拉菜单方法
Jan 02 Javascript
小程序使用wxs解决wxml保留2位小数问题
Dec 13 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
PHP实现XML与数据格式进行转换类实例
2015/07/29 PHP
JavaScript事件代理和委托详解
2016/04/08 Javascript
javascript实现图片左右滚动效果【可自动滚动,有左右按钮】
2016/09/19 Javascript
jQuery.uploadify文件上传组件实例讲解
2016/09/23 Javascript
jquery控制页面的展开和隐藏实现方法(推荐)
2016/10/15 Javascript
AngularJS实现表单验证功能
2017/01/09 Javascript
整理关于Bootstrap导航的慕课笔记
2017/03/29 Javascript
jQuery实现常见的隐藏与展示列表效果示例
2018/06/04 jQuery
ES6实现图片切换特效代码
2020/01/14 Javascript
vue+Element中table表格实现可编辑(select下拉框)
2020/05/21 Javascript
vue用ant design中table表格,点击某行时触发的事件操作
2020/10/28 Javascript
[02:05:03]完美世界DOTA2联赛循环赛 LBZS VS Matador BO2 10.28
2020/10/28 DOTA
用Python编写一个简单的FUSE文件系统的教程
2015/04/02 Python
介绍Python中的fabs()方法的使用
2015/05/14 Python
Python变量和字符串详解
2017/04/29 Python
python SSH模块登录,远程机执行shell命令实例解析
2018/01/12 Python
Python continue继续循环用法总结
2018/06/10 Python
对python以16进制打印字节数组的方法详解
2019/01/24 Python
python三大神器之fabric使用教程
2019/06/10 Python
3种python调用其他脚本的方法
2020/01/06 Python
Django Xadmin多对多字段过滤实例
2020/04/07 Python
css3实现一款模仿iphone样式的注册表单
2013/03/20 HTML / CSS
CSS3 transition 实现通知消息轮播条
2020/10/14 HTML / CSS
台湾菁英交友:结识黄金单身的台湾人
2018/01/22 全球购物
Python的两道面试题
2013/06/29 面试题
自我鉴定模板
2013/10/29 职场文书
入党转预备思想汇报
2014/01/07 职场文书
土地转让协议书
2014/09/27 职场文书
初中家长评语和期望
2014/12/26 职场文书
实习科室评语
2015/01/04 职场文书
索赔员岗位职责
2015/02/15 职场文书
《狼牙山五壮士》读后感:宁死不屈,视死如归
2019/08/16 职场文书
JS ES6异步解决方案
2021/04/29 Javascript
Python使用scapy模块发包收包
2021/05/07 Python
Ajax异步刷新功能及简单案例
2021/11/20 Javascript
我的收音机情缘
2022/04/05 无线电