javascript summary11-13

Enhanced typeof function
o.toString()  // May invoke a customized toString(  ) method for the object 

Instead, we have to refer explicitly to the default toString( ) function as the Object.prototype.toString object and use the apply( ) method of the function to invoke it on the desired object:

Object.prototype.toString.apply(o);  // Always invokes the default toString(  ) 

We can use this technique to define a function that provides enhanced "type of" functionality:

// An enhanced "type of" function. Returns a string that describes the

// type of x. Note that it returns "Object" for any user-defined object types.

function Typeof(x) {

    // Start with the typeof operator

    var t = typeof x;

    // If the result is not vague, return it

    if (t != "object")  return t;

    // Otherwise, x is an object. Get its class value to try to

    // find out what kind of object it is.

    var c = Object.prototype.toString.apply(x);  // Returns "[object class]"

    c = c.substring(8, c.length-1);              // Strip off "[object" and "]"

    return c;

} 


Value of function
The valueOf( ) method is much like the toString( ) method, but it is called when JavaScript needs to convert an object to some primitive type other than a string -- typically, a number. Where possible, the function should return a primitive value that somehow represents the value of the object referred to by the this keyword.

function Complex(real, imaginary) {

    this.x = real;       // The real part of the number

    this.y = imaginary;  // The imaginary part of the number

}

// Return the real portion of a complex number. This function
// is invoked when a Complex object is treated as a primitive value.

Complex.prototype.valueOf = function(  ) { return this.x; }

var a = new Complex(5,4);   // the value of a is 5
var b = new Complex(2,1);   // the value of b is 2

One note of caution about defining a valueOf( ) method: the valueOf( ) method can, in some circumstances, take priority over the toString( ) method when converting an object to a string. Thus, when you define a valueOf( ) method for a class, you may need to be more explicit about calling the toString( ) method when you want to force an object of that class to be converted to a string. To continue with the Complex example:
alert("c = " + c);             // Uses valueOf(  ); displays "c = 3"
alert("c = " + c.toString(  ));  // Displays "c = {3,3}"

Array Index
Note that array indexes must be integers greater than or equal to 0 and less than 232 -1. If you use a number that is too large, a negative number, or a floating-point number (or a boolean, an object, or other value), JavaScript converts it to a string and uses the resulting string as the name of an object property, not as an array index. Thus, the following line creates a new property named "-1.23"; it does not define a new array element:

a[-1.23] = true; 


Contiguous Array

var fruits = ["mango", "banana", "cherry", "pear"];
for(var i = 0; i < fruits.length; i++)
    alert(fruits[i]); 

This example assumes, of course, that elements of the array are contiguous and begin at element 0. If this were not the case, we would want to test that each array element was defined before using it:

for(var i = 0; i < fruits.length; i++)
    if (fruits[i] != undefined) alert(fruits[i]); 

Converting Numbers to Strings
The toString( ) method of the Number object (primitive numbers are converted to Number objects so that this method can be called) takes an optional argument that specifies a radix, or base, for the conversion. If you do not specify the argument, the conversion is done in base 10. But you can also convert numbers in other bases (between 2 and 36).[3] For example:
var n = 17;

binary_string = n.toString(2);        // Evaluates to "10001"

octal_string = "0" + n.toString(8);   // Evaluates to "021"

hex_string = "0x" + n.toString(16);   // Evaluates to "0x11"

你可能感兴趣的:(JavaScript,C++,c,prototype,C#)