天天看点

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.

继续阅读