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 相关文章推荐
jquery Mobile入门—外部链接切换示例代码
Jan 08 Javascript
jQuery JSON实现无刷新三级联动实例探讨
May 28 Javascript
javascript中数组的sort()方法的使用介绍
Dec 18 Javascript
Jquery对数组的操作技巧整理
Mar 25 Javascript
JS实现往下不断流动网页背景的方法
Feb 27 Javascript
简单总结JavaScript中的String字符串类型
May 26 Javascript
基于jQuery下拉选择框插件支持单选多选功能代码
Jun 07 Javascript
bootstrap模态框垂直居中效果
Dec 03 Javascript
AngularJS实现tab选项卡的方法详解
Jul 05 Javascript
ionic3 懒加载
Aug 16 Javascript
create-react-app安装出错问题解决方法
Sep 04 Javascript
JavaScript变速动画函数封装添加任意多个属性
Apr 03 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程序
2006/10/09 PHP
PHP安装攻略:常见问题解答(二)
2006/10/09 PHP
php开发环境配置记录
2011/01/14 PHP
wamp服务器访问php非常缓慢的解决过程
2015/07/01 PHP
php提交post数组参数实例分析
2015/12/17 PHP
laravel 5.4 + vue + vux + element的环境搭配过程介绍
2018/04/26 PHP
翻译整理的jQuery使用查询手册
2007/03/07 Javascript
jQuery select控制插件
2009/08/17 Javascript
jQuery遮罩层实现方法实例详解(附遮罩层插件)
2015/12/08 Javascript
JavaScript数组方法总结分析
2016/05/06 Javascript
ionic js 模型 $ionicModal 可以遮住用户主界面的内容框
2016/06/06 Javascript
jQuery实现鼠标选中文字后弹出提示窗口效果【附demo源码】
2016/09/05 Javascript
JS正则表达式学习之贪婪和非贪婪模式实例总结
2016/12/26 Javascript
关于express与koa的使用对比详解
2018/01/25 Javascript
使用vue-infinite-scroll实现无限滚动效果
2018/06/22 Javascript
React-router4路由监听的实现
2018/08/07 Javascript
13 个npm 快速开发技巧(推荐)
2019/07/04 Javascript
简单了解vue中父子组件如何相互传递值(基础向)
2019/07/12 Javascript
原生js实现碰撞检测
2020/03/12 Javascript
python插入排序算法的实现代码
2013/11/21 Python
Python多层嵌套list的递归处理方法(推荐)
2016/06/08 Python
python中for循环输出列表索引与对应的值方法
2018/11/07 Python
Appium+python自动化之连接模拟器并启动淘宝APP(超详解)
2019/06/17 Python
Python操作excel的方法总结(xlrd、xlwt、openpyxl)
2019/09/02 Python
Python如何在DataFrame增加数值
2020/02/14 Python
python时间time模块处理大全
2020/10/25 Python
HTML5仿手机微信聊天界面
2016/03/18 HTML / CSS
HTML5是什么 HTML5是什么意思 HTML5简介
2012/10/26 HTML / CSS
俄罗斯大型在线书店:Читай-город
2019/10/10 全球购物
开办饭店创业计划书
2013/12/28 职场文书
文案策划求职信
2014/04/14 职场文书
《李广射虎》教学反思
2014/04/27 职场文书
药店促销活动策划方案
2014/08/24 职场文书
Python编写可视化界面的全过程(Python+PyCharm+PyQt)
2021/05/17 Python
Java基础之线程锁相关知识总结
2021/06/30 Java/Android
MySql子查询IN的执行和优化的实现
2021/08/02 MySQL