当前位置: 首页 > news >正文

Javascript学习4 - 对象和数组

原文: Javascript学习4 - 对象和数组

在Javascript中,对象和数组是两种基本的数据类型,而且它们也是最重要的两种数据类型。
对象是已命名的值的一个集合,而数组是一种特殊对象,它就像数值的一组有序集合。

4.1 关联数组的对象 Objects as Associative Arrays
    对于对象,其属性相当于已命名的字符串值的一个集合。可以使用数组存取运算符[]来提取属性。
    对象可以使用"."来存取一个对象属性,而数组更常用的存取属性运算符是[].下面两个表达式等效:

1 object.property
2 object[ " property " ]


    以上两种方式的重要区别是:前者的属性名是标识符,后者的属性名却是一个字符串。★
    以下原文说明了它的重要性:
        In C, C++, Java, and similar strongly typed languages, an object can have only a fixed number of properties, and the names of these properties must be defined in advance. Since JavaScript is a loosely typed language, this rule does not apply: a program can create any number of properties in any object. When you use the . operator to access a property of an object, however, the name of the property is expressed as an identifier. Identifiers must be typed literally into your JavaScript program; they are not a datatype, so they cannot be manipulated by the program
    On the other hand, when you access a property of an object with the [] array notation, the name of the property is expressed as a string. Strings are JavaScript datatypes, so they can be manipulated and created while a program is running. So, for example, you can write the following code in JavaScript:

1 var  addr  =   "" ;
2 ExpandedBlockStart.gifContractedBlock.gif for (i  =   0 ; i  <   4 ; i ++ {
3    addr += customer["address" + i] + '\n';
4}


4.2 通用的object属性和方法
    ① constructor属性
        引用该属性初始化对象的构造。
    ② toString()方法
        把对象转换成字符串时,就会调用这个方法
    ③ toLocalString()方法
        返回一个本地化字符串表示
    ④ valueOf()方法
        与toString()方法很像,它是当Javascript把一个对象转换为某种基本数据类型,即数字而非字符串时,调用的方法
        默认的valueOf并不做什么有意义的事情。
    ⑤ hasOwnProperty()方法
        The hasOwnProperty() method returns true if the object locally defines a noninherited property with the name specified by the single string argument. Otherwise, it returns false. For example:

1 ExpandedBlockStart.gif ContractedBlock.gif var  o  =   {} ;
2 o.hasOwnProperty( " undef " );      //  false: the property is not defined
3 o.hasOwnProperty( " toString " );   //  false: toString is an inherited property
4 Math.hasOwnProperty( " cos " );     //  true: the Math object has a cos property


        也即是说,如果参数中指定的字符串,相当于对象的一个属性,该属性在本类中实现,返回true,在继承类中实现,返回false.
    ⑥ propertyIsEnumerable() 方法
        如果字符串参数所指定的名字,相当于对象的一个非继承的属性;且属性可以在一个for/in循环中枚举。返回true.

1 ExpandedBlockStart.gif ContractedBlock.gif var  o  =   { x:1 } ;
2 o.propertyIsEnumerable( " x " );         //  true: property exists and is enumerable
3 o.propertyIsEnumerable( " y " );         //  false: property doesn't exist


        Note that all user-defined properties of an object are enumerable.(一个对象的所有用户定义的属性都是可以枚举的。) Nonenumerable properties are typically inherited properties (see Chapter 9 for a discussion of property inheritance), so this method almost always returns the same result as hasOwnProperty().
    ⑦ isPrototypeOf()方法
        The isPrototypeOf() method returns true if the object to which the method is attached is the prototype object of the argument. Otherwise, it returns false. For example:

1 ExpandedBlockStart.gif ContractedBlock.gif var  o  =   {}
2 Object.prototype.isPrototypeOf(o);         //  true: o.constructor == Object
3 Object.isPrototypeOf(o);                   //  false
4 o.isPrototypeOf(Object.prototype);         //  false
5 Function.prototype.isPrototypeOf(Object);  //  true: Object.constructor==Function


相关文章:

  • Ubuntu 14.04下安装GitLab指南
  • 黄渊普:媒体视角--O2O与传统零售
  • Makefile学习之make 的运行【转】
  • [原]unity3d刀光剑影(二)
  • C#设计模式之外观
  • MDK调试错误之HardFault_Handler
  • Dom4j
  • Java基础work4
  • NIO入门系列之第9章:字符集
  • 新语言学习
  • Leetcode | Permutations II
  • C#开发微信门户及应用(10)--在管理系统中同步微信用户分组信息
  • 跳前端坑前,先看看这个!!
  • AWR报告导出
  • Outlook 2010如何更改脱机缓存数据OST文件位置?
  • Babel配置的不完全指南
  • C语言笔记(第一章:C语言编程)
  • JavaWeb(学习笔记二)
  • k个最大的数及变种小结
  • Laravel 菜鸟晋级之路
  • Linux各目录及每个目录的详细介绍
  • Web设计流程优化:网页效果图设计新思路
  • 安装python包到指定虚拟环境
  • 动态规划入门(以爬楼梯为例)
  • 前端学习笔记之原型——一张图说明`prototype`和`__proto__`的区别
  • 深入浅出Node.js
  • 提醒我喝水chrome插件开发指南
  • 一起来学SpringBoot | 第三篇:SpringBoot日志配置
  • Android开发者必备:推荐一款助力开发的开源APP
  • 宾利慕尚创始人典藏版国内首秀,2025年前实现全系车型电动化 | 2019上海车展 ...
  • ​​​​​​​Installing ROS on the Raspberry Pi
  • (1)(1.11) SiK Radio v2(一)
  • (1)(1.13) SiK无线电高级配置(五)
  • (env: Windows,mp,1.06.2308310; lib: 3.2.4) uniapp微信小程序
  • (react踩过的坑)antd 如何同时获取一个select 的value和 label值
  • (ZT)出版业改革:该死的死,该生的生
  • (六)Hibernate的二级缓存
  • (深入.Net平台的软件系统分层开发).第一章.上机练习.20170424
  • (转)GCC在C语言中内嵌汇编 asm __volatile__
  • (转载)Google Chrome调试JS
  • (转载)虚幻引擎3--【UnrealScript教程】章节一:20.location和rotation
  • ***linux下安装xampp,XAMPP目录结构(阿里云安装xampp)
  • .class文件转换.java_从一个class文件深入理解Java字节码结构
  • .NET CORE 3.1 集成JWT鉴权和授权2
  • .NET 中什么样的类是可使用 await 异步等待的?
  • .Net7 环境安装配置
  • .net使用excel的cells对象没有value方法——学习.net的Excel工作表问题
  • .skip() 和 .only() 的使用
  • ?
  • @ModelAttribute 注解
  • @Resource和@Autowired的区别
  • @取消转义
  • [ HTML + CSS + Javascript ] 复盘尝试制作 2048 小游戏时遇到的问题
  • [] 与 [[]], -gt 与 > 的比较
  • [HDU3710]Battle over Cities