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 - 对象和数组的更多相关文章
- JavaScript学习04 对象
JavaScript学习04 对象 默认对象 日期对象Date, 格式:日期对象名称=new Date([日期参数]) 日期参数: 1.省略(最常用): 2.英文-数值格式:月 日,公元年 [时:分: ...
- 深夜重温JavaScript中的对象和数组
这一块实际上已经学过了,因为没有学好,在工作过程中遇到一些对象或者数组的操作,会去百度查找,浪费了许多宝贵的时间,所以特地再拐过头来重新学习. 对象 基本概念: 对象这种基本的数据结构还有其他很多种叫 ...
- 转载——JavaScript学习笔记:取数组中最大值和最小值
转载自:http://www.w3cplus.com/javascript/calculate-the-max-min-value-from-an-array.html. 取数组中最大值 可以先把思路 ...
- JavaScript内置对象之数组
一.JavaScript对象之数组 1.创建数组的方式 (1)使用Array构造函数 语法:new Array() 小括号()说明: -预先知道数组要保存的项目数量 -向Array构造函数中传递数组应 ...
- JavaScript学习笔记-对象
枚举对象的属性:通常用for(...in...)来循环遍历,由于 for in 总是要遍历整个原型链,因此如果一个对象的继承层次太深的话会影响性能 for(var i in foo){ if(foo. ...
- JavaScript学习笔记——对象知识点
javascript对象的遍历.内存分布和封装特性 一.javascript对象遍历 1.javascript属性访问 对象.属性 对象[属性] //字符串格式 //javascript属性的访问方法 ...
- JavaScript学习笔记3之 数组 & arguments(参数对象)& 数字和字符串转换 & innerText/innerHTML & 鼠标事件
一.Array数组 1.数组初始化(Array属于对象类型) /*关于数组的初始化*/ //1.创建 Array 对象--方法1: var arr1=[]; arr1[0]='aa';//给数组元素赋 ...
- 学习笔记:javascript内置对象:数组对象
1.数组对象的创建 1.设置一个长度为0的数组 var myarr=new array(); 2.设置一个长度为n的数组 var myarr=new arr(n); 3.声明一个赋值的指定长度 ...
- JavaScript学习笔记——对象分类
对象的分类 一.对象的分类 1.内置对象 Global Math 2.本地对象 Array Number String Boolean Function RegExp 3.宿主对象 DOM BOM 二 ...
随机推荐
- 【原创】leetCodeOj --- Find Peak Element 解题报告
题目地址: https://oj.leetcode.com/problems/find-peak-element/ 题目内容: A peak element is an element that is ...
- spring来源理解-BeanFactory子类XmlBeanFactory创建过程
BeanFactory 1:BeanFactory什么: 官方解释The root interface for accessing a Spring bean container,翻译成中文sprin ...
- HUNNU Contest 区间最值
区间求最值 Time Limit: 3000ms, Special Time Limit:7500ms, Memory Limit:32768KB Total submit users: 68, Ac ...
- c#错误捕获如何定位到某一行?
e.ToString()在调试版本的程序(并且有pdb文件)的情况下会输出源代码行.
- Windows 7 USB DVD Download Tool 制作的U盘无法启动安装Windows7 SP1
以前用此工具安装Windows7一直正常,未遇到不能启动安装的问题.Windows7 SP1出来后,用此工具制作安装多台机器均不能引导安装(品牌机和兼容机均是如此 ),要么停留在光标闪烁的状态,要么就 ...
- JAVA Socket超时浅析(转)
套接字或插座(socket)是一种软件形式的抽象,用于表达两台机器间一个连接的“终端”.针对一个特定的连接,每台机器上都有一个“套接字”,可以想象它们之间有一条虚拟的“线缆”.JAVA有两个基于数据流 ...
- base 64 编解码器
base 64 编解码 1. base64的编码都是按字符串长度,以每3个8bit的字符为一组, 2. 然后针对每组.首先获取每一个字符的ASCII编码. 3. 然后将ASCII编码转换成8bit的二 ...
- 读书时间《JavaScript高级程序设计》六:事件
Javascript与HTML之间的交互是通过事件实现的. 1. 事件流 事件流描述的是从页面中接收事件的顺序. <!DOCTYPE html> <html> <head ...
- POJ 1915 Knight Moves(BFS+STL)
Knight Moves Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 20913 Accepted: 9702 ...
- JXL组件生成报告错误(两)
JXL组件生成报告 1.详细报错例如以下: usage: java org.apache.catalina.startup.Catalina [ -config {pathname} ] [ -non ...