原文: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. MVC与三层架构

    我们平时总是将三层架构与MVC混为一谈,殊不知它俩并不是一个概念.下面我来为大家揭晓我所知道的一些真相. 首先,它俩根本不是一个概念. 三层架构是一个分层式的软件体系架构设计,它可适用于任何一个项目. ...

  2. 我已提取并尝试使用启动脚本(./start navicat)来启动 Navicat Linux 版本号,但没有反应

    具体的安装教程,參考这个navicat_for_mysql_10.0.11在linux下的安装,介绍的非常具体 參考这个 :我可否在 64-bit Linux 执行 Navicat? 推荐navica ...

  3. 数据结构 - AVL木

    在计算机科学,AVL木是一个平衡树最早发明. 于AVL树节点,而不管是什么的两个子树之一的高度之间最大的区别,因此,它也被称为平衡树高.查找.O(log n). 插入和移除可能需要一个或更多次通过旋转 ...

  4. JAVA学习篇--JAVA两种编程模式控制

    在Drp项目中,解说了两种编程模式Model 1和Model2.以下是对这两种模式的简单理解.以及因为Model2是基于MVC架构的模式,就将我们易混淆的MVC与我们之前学的三层架构进行对照学习一下. ...

  5. git bash 出现vim的时候怎么退出

    如果是输出状态,首先Esc退出输入状态,然后Shift+;,再输入q!或wq!(不保存改动,wq!是保存文件的写入修改)退出

  6. 【Android进阶】Android调用WebService的实现

    最近想自己搞搞服务器,就从最简单的webservice开始吧 先上效果图 项目结构 开始贴代码,注释都有,有问题的请留言 MainActivity.java package com.example.w ...

  7. GLEW_ERROR_NO_GL_VERSION的解决方法

    关于 GLenum err = glewInit(); if (GLEW_OK != err) fprintf(stderr, "error initializaing GLew %s\n& ...

  8. C本学习笔记scanf

    一个.scanf功能介绍             这也是在stdio.h中声明的一个函数.因此使用前必须增加#include<stdio.h>. 调用scanf函数时,须要传入变量的地址作 ...

  9. android采用videoView播放视频(包装)

    //android播放视频.用法:于androidManifest.xml添加activity, // <activity android:name=".PlayVideo" ...

  10. [Python]新手写爬虫全过程(转)

    今天早上起来,第一件事情就是理一理今天该做的事情,瞬间get到任务,写一个只用python字符串内建函数的爬虫,定义为v1.0,开发中的版本号定义为v0.x.数据存放?这个是一个练手的玩具,就写在tx ...