基本类型及方法--《js高级程序设计》
Boolean
var falseObject = new Boolean(false);
var falseValue = false;
alert(typeof falseObject); //object
alert(typeof falseValue); //boolean
alert(falseObject instanceof Boolean); //true
alert(falseValue instanceof Boolean); //false
Date
//Date.now() is in ECMAScript 5
//Prior to that, use +new Date()
var now = (typeof Date.now == "function" ? Date.now() : +new Date());
alert("Right now: " + now);
apply call
function sum(num1, num2){
return num1 + num2;
}
function callSum1(num1, num2){
return sum.apply(this, arguments);
}
function callSum2(num1, num2){
return sum.apply(this, [num1, num2]);
}
alert(callSum1(10,10)); //20
alert(callSum2(10,10)); //20
function outer(){
inner();
}
function inner(){
alert(inner.caller); //返回outer代码
}
outer();
function outer(){
inner();
}
function inner(){
alert(arguments.callee.caller); //返回outer代码
}
outer();
call
window.color = "red";
var o = { color: "blue" };
function sayColor(){
alert(this.color);
}
sayColor(); //red
sayColor.call(this); //red
sayColor.call(window); //red
sayColor.call(o); //blue
Number
var numberObject = new Number(10);
var numberValue = 99;
//toString() using a radix
alert(numberObject.toString()); //"10"
alert(numberObject.toString(2)); //"1010"
alert(numberObject.toString(8)); //"12"
alert(numberObject.toString(10)); //"10"
alert(numberObject.toString(16)); //"a"
//toFixed() 把数字转换为字符串,结果的小数点后有指定位数的数字
alert(numberObject.toFixed(2)); //outputs "10.00"
//toPrecision() 把数字格式化为指定的长度
numberObject = new Number(99);
alert(numberObject.toPrecision(1)); //"1e+2"
alert(numberObject.toPrecision(2)); //"99"
alert(numberObject.toPrecision(3)); //"99.0"
//toExponential() 把对象的值转换为指数计数法
var num = new Number(10000);
console.log(num.toExponential(1)) //1.0e+4
alert(typeof numberObject); //object
alert(typeof numberValue); //number
alert(numberObject instanceof Number); //true
alert(numberValue instanceof Number); //false
RegExp
var text = "this has been a short summer";
var pattern = /(.)hort/g;
/*
* Note: Opera doesn't support input, lastMatch, lastParen, or multiline.
* Internet Explorer doesn't support multiline.
*/
if (pattern.test(text)){
alert(RegExp.input); //this has been a short summer
alert(RegExp.leftContext); //this has been a
alert(RegExp.rightContext); // summer
alert(RegExp.lastMatch); //short
alert(RegExp.lastParen); //s
alert(RegExp.multiline); //false
}
var text = "this has been a short summer";
var pattern = /(.)hort/g;
/*
* Note: Opera doesn't support short property names.
* Internet Explorer doesn't support multiline.
*/
if (pattern.test(text)){
alert(RegExp.$_); //this has been a short summer
alert(RegExp["$`"]); //this has been a
alert(RegExp["$'"]); // summer
alert(RegExp["$&"]); //short
alert(RegExp["$+"]); //s
alert(RegExp["$*"]); //false
}
var text = "this has been a short summer";
var pattern = /(..)or(.)/g;
if (pattern.test(text)){
alert(RegExp.$1); //sh
alert(RegExp.$2); //t
}
var text = "mom and dad and baby";
var pattern = /mom( and dad( and baby)?)?/gi;
var matches = pattern.exec(text);
alert(matches.index); //0
alert(matches.input); //"mom and dad and baby"
alert(matches[0]); //"mom and dad and baby"
alert(matches[1]); //" and dad and baby"
alert(matches[2]); //" and baby"
var text = "cat, bat, sat, fat";
var pattern1 = /.at/;
var matches = pattern1.exec(text);
alert(matches.index); //0
alert(matches[0]); //"cat"
alert(pattern1.lastIndex);//0
matches = pattern1.exec(text);
alert(matches.index); //0
alert(matches[0]); //"cat"
alert(pattern1.lastIndex);//0
var pattern2 = /.at/g;
var matches = pattern2.exec(text);
alert(matches.index); //0
alert(matches[0]); //"cat"
alert(pattern2.lastIndex);//3
matches = pattern2.exec(text);
alert(matches.index); //5
alert(matches[0]); //"bat"
alert(pattern2.lastIndex); //8
var pattern1 = /\[bc\]at/i;
alert(pattern1.global); //false
alert(pattern1.ignoreCase); //true
alert(pattern1.multiline); //false
alert(pattern1.lastIndex); //0
alert(pattern1.source); //"\[bc\]at"
var pattern2 = new RegExp("\\[bc\\]at", "i");
alert(pattern2.global); //false
alert(pattern2.ignoreCase); //true
alert(pattern2.multiline); //false
alert(pattern2.lastIndex); //0
alert(pattern2.source); //"\[bc\]at"