位置: 编程技术 - 正文

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

  • 海关缴款书上完税怎么办
  • 递延所得税抵消分录
  • 什么是协税护税员
  • 党建经费提取比例10%
  • 投资性房地产在非货币性资产交换怎么处理
  • 盘库应该由公司什么部门负责
  • 出租包装物租金是营业外收入吗
  • 单位注册表从哪里获取
  • 职工教育经费怎么花
  • 企业的成本核算包括
  • 高新企业研发费用占比要求
  • 事业编人员贷款
  • 卖二手车怎么做账务处理
  • 外出拓展训练活动所花的费用怎么做分录?
  • 收到股东借款的现金
  • 个人所得税核定征收计算公式
  • 人力资源外包差额计税税率
  • 贴现的利息能不能取出来
  • 财税2012年第15号文
  • 金税盘清卡晚了一天会怎么样
  • 营改增后发票
  • 土地出售涉及哪些税
  • 延期缴纳税款的条件是什么
  • 跟银行借款按月还款
  • 营改增服务业税负下降
  • 华为手机哪一款好用性价比高
  • 苹果手机录音怎么转换成mp3格式
  • 电脑屏发黄怎么调正常哪
  • 房产税有哪些种类
  • uefiu盘安装系统步骤win10
  • win11系统关闭防火墙怎么关
  • 内退人员
  • 提供劳务收入怎么交税
  • 出差补贴是必须的吗
  • 纳税申报的流程有哪三步
  • PHP中使用什么关键字声明变量的作用域为全局
  • bhvc.exe
  • 乱账怎么调整
  • vue运行报错
  • react moment
  • javascript百炼成仙免费
  • 报错专业怎么补救
  • php session用法
  • php分页函数封装
  • 消费税购置税价格一样
  • 住宿专票可以抵增值税吗
  • 一般纳税人费用专票如何做账
  • python缺省函数
  • 运输公司燃油费占比
  • 借调是原单位发工资吗
  • 无法连接配置的sql服务器
  • 企业享受政府补贴的具体方式
  • 欠款在公司注销怎么处理
  • 国债利息收入要征税吗
  • 解除合同补偿金需要缴纳个税吗
  • 企业如何降低存款利息
  • 会计核算的主要环节
  • 便签windows
  • 使用组策略可控制什么
  • apt-key
  • mac安装git客户端
  • centos查看inode
  • pp越狱助手下载安装苹果
  • centos6启动服务的命令
  • win7开机显示一堆英文
  • win8.0下载
  • w7开机界面
  • node.js console.log
  • Unity3D游戏开发标准教程吴亚峰于复兴人民邮电出版社
  • bootstrap技术教程
  • 关于ie浏览器下面说法正确的是
  • android的布局文件
  • jqgrid loadcomplete
  • 宾馆税务
  • 苗木税收政策
  • 如何电子税务局缴纳社保费用
  • 外购已税小汽车用于连续生产小汽车为啥可以抵扣
  • 提高税务管理水平,降低税务风险
  • 国税和地税是什么
  • 重庆税务查询企业信息查询系统
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

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

    友情链接: 武汉网站建设