红皮书(4):援用范例

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]

降序:

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

slice

slice(start, end); slice()要领返回从参数指定位置最先到当前数组末端的一切项。假如有两个参数,该要领返回起死和完毕位置之间的项,但不包括完毕位置的项。

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()有删除,插进去,替代的功用

删除
须要两个参数,要删除的第一项的位置和要删除的项数。

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

插进去
须要三个参数:肇端位置、0(要删除的项数)和要插进去的项

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);
console.log(number instanceof Number);// false

var obj = new Number(value);
console.log(typeof obj);
console.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

Global对象

URI编码要领
Global对象的encodeURI()和encodeURIComponent()要领能够对URI(Uniform Resources Identifiers,通用资本标识符)举行编码,以便发送给浏览器。

var url = "http://www.baidu.com/";
console.log(encodeURI(url));
console.log(encodeURIComponent(url));

encodeURI()和encodeURIComponent()要领对象的两个要领分别是decodeURI()和decodeURIComponent()

Math对象

random()要领

Math.random()要领返回介于0和1之间一个随机数,不包括0和1。关于某些站点来讲,这个要领异常有用,由于能够应用它来随机显现一些名言和消息事宜。套用下面的公式,就能够应用Math.random()从某个整数范围内随机挑选一个值。

值=Math.floor(Math.random()*能够值的总数+第一个能够的值)

比方:假如想挑选一个1到10之间的数值,能够像下面这边编写代码:

var num = Math.floor(Math.random()*10+1);
function selectFrom(lowerValue,upperValue){
    var choice = upperValue - lowerValue + 1;
    return Math.floor(Math.random()*choice+lowerValue);
}
var num = selectFrom(2,10);
console.log(num);
var colors = ["red", "green", "blue", "yellow", "black", "purple", "brown"];
var color = colors[selectFrom(0, colors.length-1)];
console.log(color);
    原文作者:小渝人儿
    原文地址: https://segmentfault.com/a/1190000000372925
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞