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限制输入字数,并提示剩余字数实现代码
Dec 24 Javascript
javascript 获取模态窗口的滚动位置代码
Aug 06 Javascript
js控制input输入字符解析
Dec 27 Javascript
js星星评分效果
Jul 24 Javascript
基于javascript实现listbox左右移动
Jan 29 Javascript
js实现楼层导航功能
Feb 23 Javascript
AngularJs 常用的过滤器
May 15 Javascript
jQuery实现返回顶部按钮和scroll滚动功能[带动画效果]
Jul 05 jQuery
js实现音乐播放控制条
Sep 09 Javascript
利用ES6实现单例模式及其应用详解
Dec 09 Javascript
JavaScript实现正则去除a标签并保留内容的方法【测试可用】
Jul 18 Javascript
Openlayers显示地理位置坐标的方法
Sep 28 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
《破坏领主》销量已超100万 未来将继续开发新内容
2020/03/08 其他游戏
php radio 单选框获取与保持值的实现代码
2010/05/15 PHP
php curl常见错误:SSL错误、bool(false)
2011/12/28 PHP
解决PhpMyAdmin中导入2M以上大文件限制的方法分享
2014/06/06 PHP
PHP 面向对象程序设计(oop)学习笔记 (四) - 异常处理类Exception
2014/06/12 PHP
喜大普奔!jQuery发布 3.0 最终版
2016/06/12 Javascript
js代码延迟一定时间后执行一个函数的实例
2017/02/15 Javascript
JS原型与原型链的深入理解
2017/02/15 Javascript
Bootstrap页面标题Page Header的实现方法
2017/03/22 Javascript
JS实现侧边栏鼠标经过弹出框+缓冲效果
2017/03/29 Javascript
JS 组件系列之 bootstrap treegrid 组件封装过程
2017/04/28 Javascript
详解Angular4中路由Router类的跳转navigate
2017/06/09 Javascript
React.Js添加与删除onScroll事件的方法详解
2017/11/03 Javascript
元素全屏的设置与监听实例
2017/11/28 Javascript
JavaScript的数据类型转换原则(干货)
2018/03/15 Javascript
用vuex写了一个购物车H5页面的示例代码
2018/12/04 Javascript
vue 实现 rem 布局或vw 布局的方法
2019/11/13 Javascript
C#返回当前系统所有可用驱动器符号的方法
2015/04/18 Python
python 3.6 +pyMysql 操作mysql数据库(实例讲解)
2017/12/20 Python
python验证码识别教程之灰度处理、二值化、降噪与tesserocr识别
2018/06/04 Python
python+selenium 定位到元素,无法点击的解决方法
2019/01/30 Python
Django框架models使用group by详解
2020/03/11 Python
使用python处理题库表格并转化为word形式的实现
2020/04/14 Python
canvas进阶之贝塞尔公式推导与物体跟随复杂曲线的轨迹运动
2018/01/10 HTML / CSS
html2canvas把div保存图片高清图的方法示例
2018/03/05 HTML / CSS
法国奢华女性时尚配饰网上商店:Monnier Frères
2016/08/27 全球购物
KIKO MILANO荷兰网上商店:意大利专业化妆品品牌
2017/05/12 全球购物
澳大利亚在线床零售商:Bedworks
2020/09/01 全球购物
中软国际Java程序员笔试题
2014/07/19 面试题
中秋节主持词
2014/04/02 职场文书
小学教师个人先进事迹材料
2014/05/17 职场文书
机关作风建设自查报告及整改措施
2014/10/21 职场文书
个人学习党的群众路线教育实践活动心得体会
2014/11/05 职场文书
运动会广播稿200字
2015/08/19 职场文书
《梅花魂》教学反思
2016/02/18 职场文书
Windows Server 2019 安装DHCP服务及相关配置
2022/04/28 Servers