Javascipt對象和數組

1. 對象的property

object.property

object["property"]

The important difference to note between these two syntaxes is that in the first, the property name is an identifier, and in the second, the property name is astring. We'll see why this is so important shortly.

最大的區別第一種用法是一個標識,第二種用法是用一個字符串。

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 . operatorto access a property of an object, however, the name of the property isexpressed as an identifier. Identifiers must be typed literally into your JavaScript program -- they are not a data type, 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 ofthe property is expressed as a string. Strings are JavaScript data types, so they can be manipulated and created while a program is running. So, for example,you could write the following code in JavaScript:

var addr ="";

for(i = 0; i < 4;i++) {

    addr += customer["address" + i] +'\n';

}

第二種用法的優點是字符串可以拼出來。

2. 尋找屬性的順序

We've learned that objects inherit properties from the prototype object of their constructor. How do they also inherit properties from the Object class? Remember that the prototype object is itself an object; it is created with the Object( ) constructor. This means the prototype object itself inherits properties from Object.prototype! So, an object of class Complex inherits properties from the Complex.prototype object, which itself inherits properties from Object.prototype. Thus, the Complex object inherits properties of both objects.When you look up a property in a Complex object, the object itself is searched first. If the property is not found, the Complex.prototype object is searched next. Finally, if the property is not found in that object, the Object.prototype object is searched.

 

Note that because the Complex prototype object is searched before the Object prototype object,properties of Complex.prototype hide any properties with the same name in Object.prototype. For example, in the class definition shown in Example 8-6, we defined a toString( ) method in the Complex.prototype object. Object.prototype also defines a method with this name, but Complex objects never see it because the definition of toString( ) in Complex.prototype is found first.

尋找屬性的順序。先找itself,找不到再找類的prototype  再找父類的

3. 數組類型和定義

typeof 數組 返回的是 object 

an array is nothing more than an object with a thin layer of extra functionality

var a = newArray(  );   // a.length == 0  (no elements defined)

a = newArray(10);     // a.length == 10 (emptyelements 0-9 defined)

a = newArray(1,2,3);  // a.length == 3  (elements 0-2 defined)

a = [4, 5];            // a.length == 2  (elements 0 and 1 defined)

a[5] = -1;             // a.length == 6  (elements 0, 1, and 5 defined)

a[49] = 0;             // a.length == 50 (elements 0, 1,5, and 49 defined)

4. 數組方法

數組方法

 join 默認用逗號連接

reverse sort concat slice splice push pop shift unshift toString toLocaleString

var a = [1,2,3];

a.concat(4, 5)          // Returns [1,2,3,4,5]

a.concat([4,5]);        // Returns [1,2,3,4,5]

a.concat([4,5],[6,7])   // Returns [1,2,3,4,5,6,7]

a.concat(4,[5,[6,7]])  // Returns [1,2,3,4,5,[6,7]]

var a = [1,2,3,4,5];

a.slice(0,3);    // Returns [1,2,3]

a.slice(3);      // Returns [4,5]

a.slice(1,-1);   // Returns [2,3,4]

a.slice(-3,-2);  // Returns [3]

var stack = [];       // stack: []

stack.push(1,2);      // stack: [1,2]     Returns 2

stack.pop(  );       // stack: [1]       Returns 2

stack.push(3);        // stack: [1,3]     Returns 2

stack.pop(  );       // stack: [1]       Returns 3

stack.push([4,5]);    // stack: [1,[4,5]] Returns 2

stack.pop(  )        // stack: [1]       Returns [4,5]

stack.pop(  );       // stack: []        Returns 1

var a =[1,2,3,4,5,6,7,8];

a.splice(4);    // Returns [5,6,7,8]; a is [1,2,3,4]

a.splice(1,2);  // Returns[2,3]; a is [1,4]  第一個參數是起始位置,第二個是長度

a.splice(1,1);  // Returns [4]; a is [1]

The first two arguments to splice( ) specify which array elements are to be deleted. These arguments may be followed by any number of additional arguments that specify elements to be inserted into the array, starting at the position specified by the first argument. For example:

var a = [1,2,3,4,5];

a.splice(2,0,'a','b');  // Returns []; a is [1,2,'a','b',3,4,5]

a.splice(2,2,[1,2],3);  // Returns ['a','b']; a is[1,2,[1,2],3,3,4,5]












發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章