Javascript学习4 - 对象和数组

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

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

1object.property
2object["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:

1var addr = "";
2for(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:

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


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

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

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


posted @ 2009-09-29 13:10  shipfi  阅读(225)  评论(0编辑  收藏  举报