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

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

1Javascript学习4 - 对象和数组object.property
2Javascript学习4 - 对象和数组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:

1Javascript学习4 - 对象和数组var addr = "";
2


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 true: the Math object has a cos property


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

1 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 true: Object.constructor==Function


相关文章:

  • 2021-12-19
  • 2021-12-19
  • 2021-12-17
  • 2022-12-23
  • 2021-12-19
  • 2022-12-23
  • 2022-12-23
  • 2021-06-21
猜你喜欢
  • 2021-05-26
  • 2021-06-30
  • 2021-12-19
  • 2021-12-19
  • 2022-12-23
  • 2022-02-10
  • 2021-07-25
相关资源
相似解决方案