引用類型

Object類型
Array類型
重排序方法: compare正則表達式

升序:數組

function compare(value1, value2){
    if (value1<value2){
        return -1;
    }
    if (value1>value2){
        return 1;
    } else{
       return 0;
    }
}

var values = [0,1,5,10,15];
values.sort(compare);
console.log(values); // [0,1,5,10,15]

降序:app

function compare(value1, value2){
    if (value1<value2){
        return 1;
    }
    if (value1>value2){
        return -1;
    } else{
       return 0;
    }
}

slice函數

slice(start, end); slice()方法返回從參數指定位置開始到當前數組末尾的全部項。若是有兩個參數,該方法返回起死和結束位置之間的項,但不包括結束位置的項。this

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

console.log(colors2); // green, blue, yellow, purple
console.log(colors3); // green, blue, yellow

splice編碼

splice()有刪除,插入,替換的功能prototype

刪除
須要兩個參數,要刪除的第一項的位置和要刪除的項數。指針

var colors = ["red", "green", "blue"];
var removed = colors.splice(0,1);
console.log(colors); // greeen, blue
console.log(removed); // red

插入
須要三個參數:起始位置、0(要刪除的項數)和要插入的項code

var colors = ["red", "green", "blue"];
var removed = colors.splice(1,0,"yellow", "orange");
console.log(colors); // ["red", "yellow", "orange", "green", "blue"]
console.log(removed); // 返回空

替換
須要三個參數:起始位置、要刪除的項數和要插入的任意數量的項。對象

var colors = ["red", "green", "blue"];
var removed = colors.splice(1,1,"yellow", "orange");
console.log(colors);  // ["red", "yellow", "orange", "blue"]
console.log(removed); // ["green"]

Date類型
RegExp類型

var pattern1 = /[bc]/i;
var pattern2 = new RegExp("[bc]at", "i");

pattern1和pattern2是兩個徹底等價的正則表達式。要注意的是,傳遞給RegExp構造函數的兩個參數都是字符串(不能把正則表達式字面量傳遞給RegExp構造函數)。因爲RegExp構造函數的模式參數是字符串,因此在某些狀況下要對字符串進行雙重轉義。

var pattern1 = /[bc]/i;
var pattern2 = new RegExp("\\[bc\\]at", "i");

RegExp實例方法

exec

exec接收一個參數,即要應用模式的字符串,而後返回包含第一個匹配信息的數組。

var text = "cat, bat, sat, fat";
var pattern1 = /.at/;

var matches = pattern1.exec(text);
console.log(matches); // ["cat"]

match
match是字符串執行匹配正則表達式規則的方法,他的參數是正則表達

var text = "cat, bat, sat, fat";
var pattern1 = /.at/;

var matches2 = text.match(pattern1);
console.log(matches2); // ["cat"]

test
test()接收一個字符串參數

var text = "000-00-0000";
var pattern = /\d{3}-\d{2}-\d{4}/;

if (pattern.test(text)){
    console.log("The pattern was matched"); // The pattern was matched
}

Function類型
函數內部屬性

把arguments轉爲數組

(function() {
    var slice = Array.prototype.slice,
        aArguments = slice.apply(arguments);

        console.log(aArguments);
})(10, 20, 30);

arguments.callee

該屬性是一個指針,指向擁有這個arguments對象的函數。當函數在嚴格模式下運行時,訪問arguments.callee會致使錯誤。
函數屬性和方法

length
length屬性表示函數但願接收的命名參數的個數。

function sayName(name){
    alert(name);
}

function sum(num1,num2){
    return num1 + num2;
}

function sayHi(){
    alert("hi");
}

console.log(sayName.length); //1
console.log(sum.length); //2
console.log(sayHi.length); //0

prototype

call, apply

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

console.log(callSum1(10,10)); // 20
console.log(callSum2(10,10)); //20

window.color = "red";
var o = {color:"blue"};

function sayColor(){
    console.log(this.color);
}

sayColor(); // red

sayColor.call(this); // red
sayColor.call(window); // red
sayColor.call(o); // blue

基本包裝類型

var value = "25";
var number = Number(value);
console.log(typeof number);  // number
console.log(number instanceof Number);// false

var obj = new Number(value);
console.log(typeof obj);// objectconsole.log(obj instanceof Number);// true

Boolean類型

var falseObject = new Boolean(false);
var result = falseObject && true; // true

//布爾表達式中的全部對象都會被轉換爲true, 所以falseObject對象在布爾表達式中表明的是true

console.log(result); // true

var falseValue = false;
result = falseValue && true;
console.log(result); //false

console.log(typeof falseObject); //object
console.log(typeof falseValue); // Boolean
console.log(falseObject instanceof Boolean); //true
console.log(falseValue instanceof Boolean); // false

Number類型

var numberObject = new Number(10);
var numberValue = 10;
console.log(typeof numberObject); // Object
console.log(typoef numberValue); // number
console.log(numberObject instanceof Number); // true
console.log(numberValue instanceof Number); // false

String類型
字符方法

charAt() charCodeAt()

charAt()方法以單字符字符串的形式返回給定位置的那個字符串。

charCodeAt()返回的是字符編碼。

var stringValue = "hello world";
console.log(stringValue.charAt(1)); // e
console.log(stringValue.charCodeAt(1)); // 101

字符串操做方法

concat()

concat()用於將一或多個字符串拼接起來。

var stringValue = "hello ";
var result = stringValue.concat("world");
console.log(result); // hello world
console.log(stringValue); // hello

slice(start, end)
end 表示字符串到哪裏結束。
若是傳入的是負數,slice()方法會將傳入的負值與字符串長度相加。

var str="Hello happy world!";
console.log(str.slice(6)); // happy world!
console.log(str.slice(6,11));// happy
console.log(str.slice(-3)); // ld!
console.log(str.slice(3, -4)); //lo happy wo

substring(start, end)
若是傳入的是負數, substring()會把全部字符參數都轉換爲0

var str="Hello happy world!";
console.log(str.substring(6)); // happy world!
console.log(str.substring(6,11));// happy
console.log(str.substring(-3)); // Hello happy world!
console.log(str.substring(3, -4)); //Hel

substr(start, length)
若是傳入的是負數,substr()方法將負的第一個參數加上字符串的長度,而將負的第二個參數轉換爲0

var str="Hello world!";
console.log(str.substr(3)); //lo world!
console.log(str.substr(3, 7)); //lo worl
console.log(str.substr(-3)); // ld!
console.log(str.substr(3, -3)); // 空字符串

字符串位置方法

indexOf() lastIndexOf()

var stringValue = "hello world";
console.log(stringValue.indexOf("o")); // 4
console.log(stringValue.lastIndexOf("o")); //7

這兩個方法均可以接收可選的第二個參數,表示從字符串中的哪一個位置開始搜索。

var stringValue = "hello world";
console.log(stringValue.indexOf("o", 6)); // 7
console.log(stringValue.lastIndexOf("o", 6)); //4

字符串的模式匹配方法

match()

var text = "cat, bat, sat, fat";
var pattern = /.at/;

var matches = text.match(pattern);
console.log(matches.index); //0
console.log(matches[0]); // cat
console.log(pattern.lastIndex); //0

search()

var text = "cat, bat, sat, fat";
var pos = text.search(/at/);
console.log(pos); // 1

replace()

var text = "cat, bat, sat, fat";
var result = text.replace("at", "ond");
console.log(result); // cond, bat, sat, fat

var result = text.replace(/at/g, "ond");
console.log(result); // cond, bond, sond, fond
相關文章
相關標籤/搜索