javascript summary11-13

[b]Enhanced typeof function[/b]
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;

}


[b]Value of function[/b]
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: [b]the valueOf( ) method can, in some circumstances, take priority over the toString( ) method [/b]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}"

[b]Array Index[/b]
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. [color=red][b]Thus, the following line creates a new property named "-1.23"; it does not define a new array element: [/b][/color]

a[-1.23] = true; 


[b]Contiguous Array[/b]

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]);

[b]Converting Numbers to Strings[/b]
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"
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值