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

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

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

object.propertyobject["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:

var addr = "";for(i = ; i < ; i++) {    addr += customer["address" + i] + '\n';} 

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:

var o = {};o.hasOwnProperty("undef");     // false: the property is not definedo.hasOwnProperty("toString");  // false: toString is an inherited propertyMath.hasOwnProperty("cos");    // true: the Math object has a cos property 

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

var o = { x: };o.propertyIsEnumerable("x");        // true: property exists and is enumerableo.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:

var o = {}Object.prototype.isPrototypeOf(o);        // true: o.constructor == ObjectObject.isPrototypeOf(o);                  // falseo.isPrototypeOf(Object.prototype);        // falseFunction.prototype.isPrototypeOf(Object); // true: Object.constructor==Function

Javascript学习4 - 对象和数组的更多相关文章

  1. JavaScript学习04 对象

    JavaScript学习04 对象 默认对象 日期对象Date, 格式:日期对象名称=new Date([日期参数]) 日期参数: 1.省略(最常用): 2.英文-数值格式:月 日,公元年 [时:分: ...

  2. 深夜重温JavaScript中的对象和数组

    这一块实际上已经学过了,因为没有学好,在工作过程中遇到一些对象或者数组的操作,会去百度查找,浪费了许多宝贵的时间,所以特地再拐过头来重新学习. 对象 基本概念: 对象这种基本的数据结构还有其他很多种叫 ...

  3. 转载——JavaScript学习笔记:取数组中最大值和最小值

    转载自:http://www.w3cplus.com/javascript/calculate-the-max-min-value-from-an-array.html. 取数组中最大值 可以先把思路 ...

  4. JavaScript内置对象之数组

    一.JavaScript对象之数组 1.创建数组的方式 (1)使用Array构造函数 语法:new Array() 小括号()说明: -预先知道数组要保存的项目数量 -向Array构造函数中传递数组应 ...

  5. JavaScript学习笔记-对象

    枚举对象的属性:通常用for(...in...)来循环遍历,由于 for in 总是要遍历整个原型链,因此如果一个对象的继承层次太深的话会影响性能 for(var i in foo){ if(foo. ...

  6. JavaScript学习笔记——对象知识点

    javascript对象的遍历.内存分布和封装特性 一.javascript对象遍历 1.javascript属性访问 对象.属性 对象[属性] //字符串格式 //javascript属性的访问方法 ...

  7. JavaScript学习笔记3之 数组 & arguments(参数对象)& 数字和字符串转换 & innerText/innerHTML & 鼠标事件

    一.Array数组 1.数组初始化(Array属于对象类型) /*关于数组的初始化*/ //1.创建 Array 对象--方法1: var arr1=[]; arr1[0]='aa';//给数组元素赋 ...

  8. 学习笔记:javascript内置对象:数组对象

    1.数组对象的创建   1.设置一个长度为0的数组  var myarr=new array(); 2.设置一个长度为n的数组  var myarr=new arr(n); 3.声明一个赋值的指定长度 ...

  9. JavaScript学习笔记——对象分类

    对象的分类 一.对象的分类 1.内置对象 Global Math 2.本地对象 Array Number String Boolean Function RegExp 3.宿主对象 DOM BOM 二 ...

随机推荐

  1. SCM文章10课时:定时器中断

    JP3遇见P0口. #include<reg51.h> #define uchar unsigned char uchar flag,num; uchar code smg[10] = { ...

  2. JDBC连接数据库 prepareStatement

    import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import ...

  3. 最短路径:Dijkstra,Bellman,SPFA,Floyd该算法的实施

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMzQ4NzA1MQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...

  4. Insecure default in Elasticsearch enables remote code execution

    Elasticsearch has a flaw in its default configuration which makes it possible for any webpage to exe ...

  5. Swing动画之游戏角色

    一.动画效果:实现了飞机飞行的动画效果,也实现了飞机的移动. 二.实现原理: 1.飞机飞行 的效果:事实上也还是重写paintComponent,依照一定的时间间隔更换图片就有了飞行的效果,动画就是更 ...

  6. android 处理器crash刊物

    日志记录程序是为了方便各种异常情况,为了方便日后的维修方案进行维修,程序无法百分百健康,完美,有必要保存在日志中代码.易于维护.Java了一个接口UncaughtExceptionHandler,Th ...

  7. 一起学习android图像缩放资源 (27)

    效果图: 在平时载入图片时,我会使用SetImageBitmap.setImageResource.BitmapFactory.decodeResource来设置一张图 片通过以上方法来设置图片时.会 ...

  8. 红帽/CentOS ext4无法格式化大分区 补充ext4格式化方式

    普通情况下,XFS出现丢数据的情况为海量小文件IO场景.在该场景下,inode占用教大. 通过上文的方式进行格式化,inode数量较小.通过大量測试,能够使用例如以下方法提升mkfs.ext4后文件系 ...

  9. C和指针 (pointers on C)——第十一章:动态内存分配(下)习题

    1.编写calloc,内部用malloc. void *calloc (size_t n, size_t size) { char * memory; memory =(char*) malloc(n ...

  10. 人活系列Streetlights (秩)

    人活着系列之Streetlights Time Limit: 1000MS Memory limit: 65536K 题目描写叙述 人活着假设是为了家庭,亲情----能够说是在这个世界上最温暖人心的, ...