最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

JavaScript中各種引用類型的常用操作方法小結(jié)

 更新時(shí)間:2016年05月05日 16:26:09   作者:小渝人兒  
這篇文章主要介紹了JavaScript中各種引用類型的常用操作方法小結(jié),基本上都用實(shí)際代碼進(jìn)行展示,是整理得比較全面的學(xué)習(xí)筆記,需要的朋友可以參考下

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()方法返回從參數(shù)指定位置開(kāi)始到當(dāng)前數(shù)組末尾的所有項(xiàng)。如果有兩個(gè)參數(shù),該方法返回起死和結(jié)束位置之間的項(xiàng),但不包括結(jié)束位置的項(xiàng)。

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()有刪除,插入,替換的功能

刪除:
需要兩個(gè)參數(shù),要?jiǎng)h除的第一項(xiàng)的位置和要?jiǎng)h除的項(xiàng)數(shù)。

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

插入:
需要三個(gè)參數(shù):起始位置、0(要?jiǎng)h除的項(xiàng)數(shù))和要插入的項(xiàng)

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

替換:
需要三個(gè)參數(shù):起始位置、要?jiǎng)h除的項(xiàng)數(shù)和要插入的任意數(shù)量的項(xiàng)。

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是兩個(gè)完全等價(jià)的正則表達(dá)式。要注意的是,傳遞給RegExp構(gòu)造函數(shù)的兩個(gè)參數(shù)都是字符串(不能把正則表達(dá)式字面量傳遞給RegExp構(gòu)造函數(shù))。由于RegExp構(gòu)造函數(shù)的模式參數(shù)是字符串,所以在某些情況下要對(duì)字符串進(jìn)行雙重轉(zhuǎn)義。

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

RegExp實(shí)例方法
exec

exec接收一個(gè)參數(shù),即要應(yīng)用模式的字符串,然后返回包含第一個(gè)匹配信息的數(shù)組。

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

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

match
match是字符串執(zhí)行匹配正則表達(dá)式規(guī)則的方法,他的參數(shù)是正則表達(dá)

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

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

test
test()接收一個(gè)字符串參數(shù)

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類型
函數(shù)內(nèi)部屬性
把a(bǔ)rguments轉(zhuǎn)為數(shù)組

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

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

該屬性是一個(gè)指針,指向擁有這個(gè)arguments對(duì)象的函數(shù)。當(dāng)函數(shù)在嚴(yán)格模式下運(yùn)行時(shí),訪問(wèn)arguments.callee會(huì)導(dǎo)致錯(cuò)誤。

函數(shù)屬性和方法
length
length屬性表示函數(shù)希望接收的命名參數(shù)的個(gè)數(shù)。

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 

//布爾表達(dá)式中的所有對(duì)象都會(huì)被轉(zhuǎn)換為true, 因此falseObject對(duì)象在布爾表達(dá)式中代表的是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()方法以單字符字符串的形式返回給定位置的那個(gè)字符串。

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

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

字符串操作方法
concat()

concat()用于將一或多個(gè)字符串拼接起來(lái)。

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

slice(start, end)
end 表示字符串到哪里結(jié)束。
如果傳入的是負(fù)數(shù),slice()方法會(huì)將傳入的負(fù)值與字符串長(zhǎng)度相加。

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)
如果傳入的是負(fù)數(shù), substring()會(huì)把所有字符參數(shù)都轉(zhuǎn)換為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)
如果傳入的是負(fù)數(shù),substr()方法將負(fù)的第一個(gè)參數(shù)加上字符串的長(zhǎng)度,而將負(fù)的第二個(gè)參數(shù)轉(zhuǎn)換為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

這兩個(gè)方法都可以接收可選的第二個(gè)參數(shù),表示從字符串中的哪個(gè)位置開(kāi)始搜索。

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對(duì)象
URI編碼方法
Global對(duì)象的encodeURI()和encodeURIComponent()方法可以對(duì)URI(Uniform Resources Identifiers,通用資源標(biāo)識(shí)符)進(jìn)行編碼,以便發(fā)送給瀏覽器。

var url = "http://www.baidu.com/";
console.log(encodeURI(url));
console.log(encodeURIComponent(url));
encodeURI()和encodeURIComponent()方法對(duì)象的兩個(gè)方法分別是decodeURI()和decodeURIComponent()

Math對(duì)象
random()方法

Math.random()方法返回介于0和1之間一個(gè)隨機(jī)數(shù),不包含0和1。對(duì)于某些站點(diǎn)來(lái)說(shuō),這個(gè)方法非常實(shí)用,因?yàn)榭梢岳盟鼇?lái)隨機(jī)顯示一些名言和新聞事件。套用下面的公式,就可以利用Math.random()從某個(gè)整數(shù)范圍內(nèi)隨機(jī)選擇一個(gè)值。

值=Math.floor(Math.random()*可能值的總數(shù)+第一個(gè)可能的值)

例如:如果想選擇一個(gè)1到10之間的數(shù)值,可以像下面這邊編寫(xiě)代碼:

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

相關(guān)文章

  • 詳解原生JS回到頂部

    詳解原生JS回到頂部

    這篇文章主要介紹了JS回到頂部,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • js oncontextmenu事件使用詳解

    js oncontextmenu事件使用詳解

    這篇文章主要介紹了js oncontextmenu事件使用詳解,需要的朋友可以參考下
    2017-03-03
  • 深入理解JS函數(shù)的參數(shù)(arguments)的使用

    深入理解JS函數(shù)的參數(shù)(arguments)的使用

    下面小編就為大家?guī)?lái)一篇深入理解JS函數(shù)的參數(shù)(arguments)的使用。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-05-05
  • js時(shí)間比較示例分享(日期比較)

    js時(shí)間比較示例分享(日期比較)

    這篇文章主要介紹了js時(shí)間比較示例,代碼簡(jiǎn)單,運(yùn)行后可以看到結(jié)果,需要的朋友可以參考下
    2014-03-03
  • javascript學(xué)習(xí)筆記(十九) 節(jié)點(diǎn)的操作實(shí)現(xiàn)代碼

    javascript學(xué)習(xí)筆記(十九) 節(jié)點(diǎn)的操作實(shí)現(xiàn)代碼

    javascript學(xué)習(xí)筆記之節(jié)點(diǎn)的操作實(shí)現(xiàn)代碼,包括節(jié)點(diǎn)的創(chuàng)建、添加、移除、替換、復(fù)制
    2012-06-06
  • 淺談一下JavaScript與C++的差異

    淺談一下JavaScript與C++的差異

    這篇文章主要介紹了淺談一下JavaScript與C++的差異,本來(lái)兩門(mén)語(yǔ)言各有各的戰(zhàn)場(chǎng),并沒(méi)什么交集,但自從?Node.js?框架出現(xiàn)之后,JavaScript?就擺脫了瀏覽器的樊籠,開(kāi)始滲透進(jìn)入系統(tǒng)應(yīng)用的領(lǐng)域
    2023-04-04
  • 新手入門(mén)常用代碼集錦

    新手入門(mén)常用代碼集錦

    新手入門(mén)常用代碼集錦...
    2007-01-01
  • Javascript中的數(shù)據(jù)類型之旅

    Javascript中的數(shù)據(jù)類型之旅

    JavaScript 是一種弱類型或者說(shuō)動(dòng)態(tài)語(yǔ)言。這意味著你不用提前聲明變量的類型,在程序運(yùn)行過(guò)程中,類型會(huì)被自動(dòng)確定。這也意味著你可以使用同一個(gè)變量保存不同類型的數(shù)據(jù)。
    2015-10-10
  • JavaScript 變量命名規(guī)則

    JavaScript 變量命名規(guī)則

    學(xué)習(xí)js的朋友一定要知道和注意,其實(shí)每種語(yǔ)言都有它的命名規(guī)則。
    2009-09-09
  • JS常用函數(shù)使用指南

    JS常用函數(shù)使用指南

    本文匯總了107個(gè)常用的js使用的時(shí)候的注意事項(xiàng),本來(lái)想湊足一百單八將的,結(jié)果還是差了一個(gè),哈哈,以后有機(jī)會(huì)再補(bǔ)吧。
    2014-11-11

最新評(píng)論

威海市| 万宁市| 平凉市| 武清区| 旅游| 章丘市| 原阳县| 拉孜县| 沂南县| 广平县| 枣强县| 庄河市| 九龙县| 龙州县| 乌兰察布市| 道孚县| 新河县| 怀集县| 嘉善县| 衢州市| 盘山县| 巴彦淖尔市| 兴化市| 池州市| 扎兰屯市| 松江区| 萨迦县| 元江| 晴隆县| 连云港市| 洪湖市| 南京市| 临清市| 巧家县| 安化县| 从江县| 巫山县| 巴青县| 中方县| 宁陵县| 河间市|