2018-02-05

數(shù)據(jù)類型

五種簡單數(shù)據(jù)類型(基本數(shù)據(jù)類型):undefined、null、boolean、string、number
一種復(fù)雜類型:object object本質(zhì)上是由一組無序的名值對組成的
注意 typeof null 結(jié)果是object,typeof 的結(jié)果有七種結(jié)果,加上 function

Number 精度

0.1+0.2 != 0.3 返回結(jié)果是 true ,解決辦法是 (a*10+b*10)/10

數(shù)值轉(zhuǎn)換

Number() 可以用于任何數(shù)據(jù)類型
parseInt() parseFloat() 專門用于把字符串轉(zhuǎn)換成數(shù)值
parseInt()轉(zhuǎn)換空字符串會返回 NaN(Number()對空字符返回 0)

var num1 = parseInt("10", 2); //2 (按二進(jìn)制解析)
var num2 = parseInt("10", 10); //10 (按十進(jìn)制解析)

布爾值

!! 的作用 因為無論是undefined或者null ,!underfined 或者 !null 返回的都是true,此時!!underfined 或者 !!null 是false; 也屬于一種強(qiáng)制的類型轉(zhuǎn)換。

判斷數(shù)組

instanceof 操作符的問題在于,它假定只有一個全局執(zhí)行環(huán)境。如果網(wǎng)頁中包含多個框架,那實際上就存在兩個以上不同的全局執(zhí)行環(huán)境,從而存在兩個以上不同版本的 Array 構(gòu)造函數(shù)。如果你從一個框架向另一個框架傳入一個數(shù)組,那么傳入的數(shù)組與在第二個框架中原生創(chuàng)建的數(shù)組分別具有各自不同的構(gòu)造函數(shù)。
為了解決這個問題,ECMAScript 5 新增了 Array.isArray()方法。這個方法的目的是最終確定某個值到底是不是數(shù)組,而不管它是在哪個全局執(zhí)行環(huán)境中創(chuàng)建的。

數(shù)組slice

var colors = ["red", "green", "blue", "yellow", "purple"];
var colors2 = colors.slice(1);
var colors3 = colors.slice(1,4);
alert(colors2); //green,blue,yellow,purple
alert(colors3); //green,blue,yellow
alert(color); //red,green,blue,yellow,purple

數(shù)組 splice

var colors = ["red", "green", "blue"];
var removed = colors.splice(0,1); // 刪除第一項
alert(colors); // green,blue
alert(removed); // red,返回的數(shù)組中只包含一項
removed = colors.splice(1, 0, "yellow", "orange"); // 從位置 1 開始插入兩項
alert(colors); // green,yellow,orange,blue
alert(removed); // 返回的是一個空數(shù)組
removed = colors.splice(1, 1, "red", "purple"); // 插入兩項,刪除一項
alert(colors); // green,red,purple,orange,blue
alert(removed); // yellow,返回的數(shù)組中只包含一項

函數(shù)內(nèi)部屬性 argument.callee

function factorial(num){
if (num <=1) {
return 1;
} else {
return num * factorial(num-1)
}
}
定義階乘函數(shù)一般都要用到遞歸算法;如上面的代碼所示,在函數(shù)有名字,而且名字以后也不會變的情況下,這樣定義沒有問題。但問題是這個函數(shù)的執(zhí)行與函數(shù)名 factorial 緊緊耦合在了一起。為了消除這種緊密耦合的現(xiàn)象,可以像下面這樣使用 arguments.callee。
function factorial(num){
if (num <=1) {
return 1;
} else {
return num * arguments.callee(num-1)
}
}
在這個重寫后的 factorial()函數(shù)的函數(shù)體內(nèi),沒有再引用函數(shù)名 factorial。這樣,無論引用
函數(shù)時使用的是什么名字,都可以保證正常完成遞歸調(diào)用。例如:
var trueFactorial = factorial;
factorial = function(){
return 0;
};
alert(trueFactorial(5)); //120
alert(factorial(5)); //0
在此,變量 trueFactorial 獲得了 factorial 的值,實際上是在另一個位置上保存了一個函數(shù)的指針。然后,我們又將一個簡單地返回 0 的函數(shù)賦值給 factorial 變量。如果像原來的 factorial()那樣不使用 arguments.callee,調(diào)用 trueFactorial()就會返回 0??墒?,在解除了函數(shù)體內(nèi)的代碼與函數(shù)名的耦合狀態(tài)之后,trueFactorial()仍然能夠正常地計算階乘;至于 factorial(),它現(xiàn)在只是一個返回 0 的函數(shù)。`

字符串方法

var stringValue = "hello world";
alert(stringValue.slice(3)); //"lo world"
alert(stringValue.substring(3)); //"lo world"
alert(stringValue.substr(3)); //"lo world"
alert(stringValue.slice(3, 7)); //"lo w"
alert(stringValue.substring(3,7)); //"lo w"
alert(stringValue.substr(3, 7)); //"lo worl"

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容