位置: 编程技术 - 正文

JS 建立对象的方法(js对象创建方法)

编辑:rootadmin
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= 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= 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",,"blue") myMother=new person("Sally","Rally",,"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 建立对象的方法(js对象创建方法),希望有所帮助,仅作参考,欢迎阅读内容。

JS 建立对象的方法(js对象创建方法)

文章相关热门搜索词:js对象创建方法,js对象创建方法,js 建立对象的方法有哪些,js对象创建方法,js 建立对象的方法是什么,js 建立对象的方法有哪些,js 建立对象的方法有哪些,js对象创建方法,内容如对您有帮助,希望把文章链接给更多的朋友!

JS Timing 使用JS是可以让函数不直接执行的,而是在过了一个指定的时间间隔后才执行。这就叫做事件事件。WithJavaScript,itispossibletoexecutesomecodeNOTimmediatelyafterafunct

关于setEndPoint msdn给出的参考 关于setEndPointmsdn给出的参考是:TextRange.setEndPoint(sType,oTextRange)oTextRange是另一个TextRange对象sType是字符串类型有4种选择StartToEndStartToStartEndToStartEndToEnd"Atext

Javascript中的数学函数集合 在Javascript中,数学方法可以分成以下几类:constans(常数)、powerfunctions(乘方函数)、trigonometicfunctions(三角函数)、roundingfunctions(舍入函数)、rand

标签: js对象创建方法

本文链接地址:https://www.jiuchutong.com/biancheng/383952.html 转载请保留说明!

上一篇:如何做到打开一个页面,过几分钟自动转到另一页面(怎么做到开放)

下一篇:JS Timing

  • 一般纳税人出租不动产增值税税率
  • 所得税报表的营业成本
  • 库存商品转为固定资产说明
  • 一般纳税人混凝土税率
  • 软件平台服务公司的营业执照经营范围怎么写
  • 电子税务局申报的财务报表在哪里查询
  • 退休职工怎么填写单位吗
  • 发票税率开错了按什么申报
  • 权益法下被投资企业净资产增加
  • 出借包装物一次摊销金额计算
  • 仓库物料入库
  • 企业的完工产品是指
  • 计提业务招待费和办公费需要附件吗?
  • 增值税发票退票时间
  • 小规模企业所得税标准
  • 显示器件属于什么设备
  • 劳务服务公司业务范围
  • 网上申请的增值税专用纸质发票收到后如何确认已收到
  • 汇总记账凭证账务处理程序的优点包括
  • 商品盘点溢余短缺的核算方法有哪些?
  • 取得的国债利息收入可以抵扣吗
  • 专用发票验旧后还领不到发票
  • 营改增后城建税怎么算
  • 收到政府拨款怎么做账
  • mac如何登陆两个微信
  • 个体工商户公转私用途写什么
  • 企业注销时往来账目挂账怎么处理
  • linux批量操作工具
  • vue全家桶插件有哪些
  • php中的常用魔术有哪些
  • 应付国库集中支付结余在什么时候确认
  • php的中文名
  • web系统的安全现状
  • 建筑企业其他应付款包含什么内容
  • 销售返利账务处理及注意事项
  • vue 同局域网访问不到的问题及解决
  • 建筑业 成本
  • 残保基金什么时候申报
  • 公司吸收合并流程详细步骤
  • 事业单位人员收受财物
  • 背书转让操作
  • 企业不计提固定资产损失
  • 计入税金及附加的税种顺口溜
  • 疫情期间增值税减免政策截止时间
  • 土地出让金返还比例是多少
  • 个贷系统平账专户怎么做账
  • 公众号认证小额打款流程
  • 报销车辆通行费怎么做账
  • 财务费用在贷方表示增加还是减少
  • 企业垃圾桶
  • 史上最全的mysql基础教程
  • adsl用户之间共享电话线路
  • winxp关闭自动更新方法
  • linux vps 教程
  • linux i3桌面
  • win7的屏幕键盘
  • mac怎么预览cr3
  • 怎么提高局域网安全
  • linux删除一个非空子目录命令
  • win10任务栏电源图标打不开
  • win8怎么打开磁盘管理
  • uibutton设置圆角
  • js必须掌握的
  • 批处理语言 从入门到精通
  • 页里面 CDATA的作用说明
  • xcopy /s /e
  • dw中css规则定义中文
  • node转go
  • pm2启动nodejs
  • unity做app
  • jquery mobile教程
  • node.js的安装步骤
  • 用shell脚本创建用户
  • pythonyield理解与用法
  • 法律服务所与律师事务所区别
  • 江苏省国家税务局电子税务局官网
  • 河北税务登录密码是多少
  • 浙江税务开票系统
  • 湖北省叉车考试题库
  • 增值税专用发票和普通发票的区别
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设