天天看點

JAVASCRIPT基礎學習篇(6)--ECMAScript Basic2(EcmaScript 引用類型)

第二章  Reference Types引用類型:

    可以使用:var o = new Object()或var o = new Object來建立一個對象

    js中類與java中相似,即Object類作為所有類的基類,所有類都具有Object類所擁有的屬性

    All the properties and methods of the Object class are also present in the other classes, and so to understand the Object class is to understand all the others better.

    The Object class has the following properties:(兩個屬性)

        (1)constructor — A reference value (pointer) to the function that created the object. For the

    Object class, this points to the native Object() function.

        (2)prototype — A reference value to the object prototype for this object. Prototypes are discussed

    further in Chapter 3. For the all classes, this returns an instance of Object by default.

    有關這兩個屬性的介紹及應用分别在以下貼子中說明:

    http://blog.csdn.net/luweifeng1983/archive/2009/02/20/3915189.aspx

    http://blog.csdn.net/luweifeng1983/archive/2009/02/20/3915284.aspx

    The Object class also has several methods:(成員方法)

        (1)hasOwnProperty(property) — Determines if a given property exists for the object. The

    property must be specified as a string (for example, o.hasOwnProperty(“name”)).

        (2)isPrototypeOf(object) — Determines if the object is a prototype of another object.

        (3)propertyIsEnumerable(property) — Determines if a given property can be enumerated by

    using the for...in statement (discussed later in this chapter).

        (4)toString() — Returns a primitive string representation of the object. For the Object class,

    this value is undefined in ECMA-262 and, as such, differs in each implementation.

        (5)valueOf() — Returns the most appropriate primitive value of this object. For many classes,

    this returns the same value as toString().

一、The Boolean class

    var oBooleanaobject = new Boolean(true);

    Boolean類重寫了toString()和valueOf(),可以使用它們傳回對應的基本類型的值

    需要注意的是以下表達式:

    var oFalseObject = new Boolean(false);

    var bResult = oFalseObject && true; //outputs true

    這是因為oFalseObject為對象,對象傳回值為true是以結果為true

二、The Number class

    var oNumberObject = new Number(55);

    var iNumber = oNumberObject.valueOf();

    除了從Object類繼承來的方法之外,Number類還定義了其它方法

    1、toFixed()

    var oNumberObject = new Number(99);

    alert(oNumberObject.toFixed(2)); //outputs “99.00”

    注意上面參數表示小數點位數,可以從0-20,其它值将出錯

    2、    以下方法轉換成指數形式:

    var oNumberObject = new Number(99);

    alert(oNumberObject.toExponential(1)); //outputs “9.9e+1” 表示9.9 x 101

    3、toPrecision傳回 NumberObject 的字元串表示,包含 num 個有效數字。如果 num 足夠大,能夠包括 NumberObject 整數部分的所有數字,

    那麼傳回的字元串将采用定點計數法。否則,采用指數計數法,即小數點前有一位數字,小數點後有 num-1 位數字。必要時,該數字會被舍入或用 0 補足。

    var oNumberObject = new Number(99);

    alert(oNumberObject.toPrecision(1)); //outputs “1e+2”

    var oNumberObject = new Number(99);

    alert(oNumberObject.toPrecision(2)); //outputs “99”

    var oNumberObject = new Number(99);

    alert(oNumberObject.toPrecision(3)); //outputs “99.0”

三、The String class

    1、valueOf() and toString()

    var oStringObject = new String(“hello world”);

    Both valueOf() and toString() return the String primitive value for a String object:

    alert(oStringObject.valueOf() == oStringObject.toString()); //outputs “true”

    2、length屬性

    The String class has one property, length, which gives the number of characters in the string:

    var oStringObject = new String(“hello world”);

    alert(oStringObject.length); //outputs “11“

    3、charAt()與charCodeAt()

    除了從Object類繼承來的方法之外,String類還定義了其它方法

    var oStringObject = new String(“hello world”);

    alert(oStringObject.charAt(1)); //outputs “e”

    If instead of the actual character you want the character code, then calling charCodeAt() is the appropriate choice

    var oStringObject = new String(“hello world”);

    alert(oStringObject.charCodeAt(1)); //outputs “101”

    4、concat()與+

    var oStringObject = new String(“hello “);

    var sResult = oStringObject.concat(“world”);

    alert(sResult); //outputs “hello world”

    alert(oStringObject); //outputs “hello “

    使用+号運算符

    var oStringObject = new String(“hello “);

    var sResult = oStringObject + “world”;

    alert(sResult); //outputs “hello world”

    alert(oStringObject); //outputs “hello “

    5、indexOf()與lastIndexOf()

    var oStringObject = new String(“hello world”);

    alert(oStringObject.indexOf(“o”)); //outputs “4”

    alert(oStringObject.lastIndexOf(“o”)); //outputs “7”

    6、The next method is localeCompare(), which helps sort string values. This method takes one argument,

    the string to compare to, and it returns one of three values:

        If the String object should come alphabetically before the string argument, a negative number

    is returned (most often this is –1, but it is up to each implementation as to the actual value).

        If the String object is equal to the string argument, 0 is returned.

        If the String object should come alphabetically after the string argument, a positive number is

    returned (most often this is 1, but once again, this is implementation-specific).

    Example:

    var oStringObject = new String(“yellow”);

    alert(oStringObject.localeCompare(“brick”)); //outputs “1”

    alert(oStringObject.localeCompare(“yellow”)); //outputs “0”

    alert(oStringObject.localeCompare (“zoo”)); //outputs “-1”

    更好的使用localeCompare()的方式如下:

    var oStringObject1 = new String(“yellow”);

    var oStringObject2 = new String(“brick”);

    var iResult = sTestString.localeCompare(“brick”);

    if(iResult < 0) {

    alert(oStringObject1 + “ comes before “ + oStringObject2);

    } else if (iResult > 0) {

    alert(oStringObject1 + “ comes after “ + oStringObject2);

    } else {

    alert(“The two strings are equal”);

    }

    7、取子串的兩個方法slice()和substring()

    var oStringObject = new String(“hello world”);

    alert(oStringObject.slice(3)); //outputs “lo world”

    alert(oStringObject.substring(3)); //outputs “lo world”

    alert(oStringObject.slice(3, 7)); //outputs “lo w”

    alert(oStringObject.substring(3,7)); //outputs “lo w”

    這兩個方法在參數為正數的時候是一樣的,不同在于當參數為負數的時候

    var oStringObject= new String(“hello world”);

    alert(oStringObject.slice(-3)); //outputs “rld”

    alert(oStringObject.substring(-3)); //outputs “hello world”

    alert(oStringObject.slice(3, -4)); //outputs “lo w”

    alert(oStringObject.substring(3,-4)); //outputs “hel”

    了解:slice(-3)認為是slice(7)而substring(-3)當作是substring(0)

    slice(3, -4)當作是slice(3, 7),而substring(3,-4)當作是substring(3,0)

    這裡要注意的是substring(3,0)與substring(0,3)是一樣的,因為substring方法總是把兩個參數中小的那個作為起始位,大的作為終止位。

    8、toLowerCase(), toLocaleLowerCase(), toUpperCase(), and toLocaleUpperCase().

    var oStringObject= new String(“Hello World”);

    alert(oStringObject.toLocaleUpperCase()); //outputs “HELLO WORLD”

    alert(oStringObject.toUpperCase()); //outputs “HELLO WORLD”

    alert(oStringObject.toLocaleLowerCase()); //outputs “hello world”

    alert(oStringObject.toLowerCase()); //outputs “hello world”

    Remember, all the methods and properties for the String class also apply to String primitive values because they are pseudo-objects.

繼續閱讀