日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

【js实例】Array类型的9个数组方法,Date类型的41个日期方法,Function类型

發(fā)布時(shí)間:2023/12/13 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【js实例】Array类型的9个数组方法,Date类型的41个日期方法,Function类型 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

?前文提要:【js實(shí)例】js中的5種基本數(shù)據(jù)類(lèi)型和9種操作符

Array類(lèi)型的9個(gè)數(shù)組方法

Array中有9個(gè)數(shù)組方法:

1.檢測(cè)數(shù)組 2.轉(zhuǎn)換方法 3.棧方法 4.隊(duì)列方法 5.沖排序方法
6.操作方法 7.位置方法 8.迭代方法 9.歸并方法

在實(shí)例中介紹,實(shí)例如下

/* Array類(lèi)型 js數(shù)組中的每一項(xiàng)可以用來(lái)保存任何類(lèi)型的數(shù)據(jù);js數(shù)組的大小是可以動(dòng)態(tài)調(diào)整的 */ var colors = ["red", "blue", "green"]; alert(colors[0]); //red alert(colors[1]); //blue alert(colors[2]); //green alert(colors[3]); //undefined alert(colors.length); /*
1.
檢測(cè)數(shù)組:
instanceof(), isArray()
*/
if (colors instanceof Array) {alert("yes"); //yes }if (Array.isArray(colors)) {alert("yes"); //yes } /* 轉(zhuǎn)換方法:
toString(), toLocaleString(), valueOf() alert()要接收字符串參數(shù),當(dāng)傳入alert()的不是字符串參數(shù)時(shí)它會(huì)在后臺(tái)調(diào)用toString()方法
*///返回一個(gè)字符串,字符串由數(shù)組中每個(gè)值的字符串組成,并且以逗號(hào)分隔 alert(colors.toString()); //通常和toString()方法一樣,但是它是調(diào)用數(shù)組中每一項(xiàng)的toLocaleString()方法 alert(colors.toLocaleString()); //先是valueOf()方法,調(diào)用toString()方法,(valueOf返回的是數(shù)組) alert(colors.valueOf()); alert(colors);//join接收一個(gè)參數(shù),返回以參數(shù)做分隔符的所有數(shù)組項(xiàng)的字符串 alert(colors.join("~")); //red~blue~green/* 棧方法:push()和pop() push()向數(shù)組中添加元素,返回修改后數(shù)組的長(zhǎng)度 pop()移除數(shù)組中最后一項(xiàng),返回移除的項(xiàng) */var colors = ["red", "blue", "green"]; var count = colors.push("white", "yellow"); alert(count); //5 alert(colors.length); //5 alert(colors); //red,blue,green,white,yellowvar item = colors.pop(); alert(item); //yellow alert(colors.length); //4 alert(colors); //red,blue,green,white/* 隊(duì)列方法:shift()和unshift() shift()移除數(shù)組的第一項(xiàng)并返回移除的項(xiàng) unshift()在數(shù)組的第一項(xiàng)之前添加任意項(xiàng),并返回?cái)?shù)組的長(zhǎng)度 */ var colors = ["red", "blue", "green"]; var item = colors.shift(); //shift() alert(item); //red alert(colors.length); //2 alert(colors); //blue,green //unshift() var count = colors.unshift("white", "yellow"); alert(count); //4 alert(colors.length); //4 alert(colors); //white,yellow,blue,green/* 排序方法:reverse()和sort() reverse()會(huì)反轉(zhuǎn)數(shù)組想的順序,返回排序后的數(shù)組 sort()比較的是字符串,接收的參數(shù)會(huì)調(diào)用每個(gè)數(shù)組項(xiàng)的toString()方法,返回排序后的數(shù)組 sort()接收的參數(shù)也可以是函數(shù) */ //reverse() var value = [1, 3, 5, 2, 10]; var values = value.reverse(); alert(value); //10,2,5,3,1 alert(values); //10,2,5,3,1//sort() var value = [1, 3, 5, 2, 10]; var values = value.sort(); alert(value); //1,10,2,3,5 alert(values); //1,10,2,3,5/* 操作方法:concat(), slice()和splice() concat()創(chuàng)建當(dāng)前數(shù)組的副本,若有參數(shù)則將其添加到副本數(shù)組尾部,最后返回新創(chuàng)建的數(shù)組slice()基于當(dāng)前數(shù)組創(chuàng)建新數(shù)組,但是不改變?cè)瓟?shù)組;接收兩個(gè)參數(shù)start, end start為返回項(xiàng)的起始位置,end為返回項(xiàng)的結(jié)束位置(具體見(jiàn)例子),splice(),接收2個(gè)或3個(gè)參數(shù)通常用于刪除,插入或替換(插入和替換都要產(chǎn)生刪除操作,刪除項(xiàng)數(shù)可為0),返回刪除的項(xiàng) 刪除:splice(x, y); x為刪除的起始位置,y為要?jiǎng)h除的項(xiàng)數(shù) 插入和替換(通過(guò)改變參數(shù)實(shí)現(xiàn)):splice(x, y, z); x為起始位置,y為要?jiǎng)h除的項(xiàng)數(shù),z為要插入的項(xiàng);z可以是任意多個(gè)項(xiàng) */ //concat() var colors = ["red", "blue", "green"]; var colors2 = colors.concat(); alert(colors); //red,blue,green alert(colors2); //red,blue,green var colors3 = colors.concat("yellow", ["black", "brown"]); alert(colors); //red,blue,green alert(colors3); //red,blue,green,yellow,black,brown//slice() var colors = ["red", "blue", "green", "yellow", "black"]; //1.若有一個(gè)參數(shù),則返回從起始位置到原數(shù)組末尾所組成的數(shù)組 var colors2 = colors.slice(1); //2.若有兩個(gè)參數(shù),則返回從起始位置到結(jié)束位置前一項(xiàng)所組成的數(shù)組 var colors3 = colors.slice(1, 4); //3.若start < end時(shí)返回空數(shù)組 var colors4 = colors.slice(2, 1); //4.若參數(shù)為負(fù)數(shù),則參數(shù)加上數(shù)組長(zhǎng)度作為start或者end var colors5 = colors.slice(-3, -1);alert(colors); //red,blue,green,yellow,black alert(colors2); //blue,green,yellow,black alert(colors3); //blue,green,yellow alert(colors4); //返回空數(shù)組,屏幕上顯示空白警告框 alert(colors5); //green,yellow//splice() //刪除 var colors = ["red", "blue", "green", "yellow", "black"]; var remove = colors.splice(1, 2); alert(colors); //red,yellow,black alert(remove); //blue,green//插入 var colors = ["red", "blue", "green", "yellow", "black"]; var remove2 = colors.splice(1, 0, "white", "brown"); //刪除項(xiàng)數(shù)為0 alert(colors); //red,white,brown,blue,green,yellow,black alert(remove2); //空數(shù)組//替換 var colors = ["red", "blue", "green", "yellow", "black"]; var remove2 = colors.splice(1, 1, "white", "brown"); //刪除項(xiàng)數(shù)為1 alert(colors); //red,white,brown,green,yellow,black alert(remove2); //blue/* 位置方法:indexOf()和lastIndexOf() 兩個(gè)方法用于返回查找項(xiàng)在數(shù)組中的位置,未找到返回-1;都接收兩個(gè)參數(shù)x和y, x:要查找的項(xiàng);y:查找起始點(diǎn)位置的索引(可選參數(shù))indexOf()從數(shù)組開(kāi)頭向后查找查找并返回查找參數(shù)的第一個(gè)位置,找不到返回-1; lastIndexOf()從數(shù)組末尾向前查找,返回查找參數(shù)的第一個(gè)位置注意:要查找的項(xiàng)必須嚴(yán)格相等(===) */ var numbers = [1, 2, 3, 4, 5, 4, 3, 2, 1]; //indexOf() alert(numbers.indexOf(4)); //3 alert(numbers.indexOf(4, 4)); //5 alert(numbers.indexOf(4, 6)) //-1 alert(numbers.indexOf(10)); //-1 //lastIndexOf() alert(numbers.lastIndexOf(4)); //5 alert(numbers.lastIndexOf(4, 4)); //3 alert(numbers.lastIndexOf(4, 2)); //-1 alert(numbers.lastIndexOf(10)) //-1//要查找的項(xiàng)必須嚴(yán)格相等(===) var person = {name : "Nicholas"}; var people = [{name : "Nicholas"}]; var morePeople = [person]; //注意這是數(shù)組 alert(people.indexOf(person)); //-1 alert(morePeople.indexOf(person)); //0/* 迭代方法:every(), filter(), forEach(), map(), some() 每個(gè)方法接收兩個(gè)參數(shù):函數(shù)參數(shù)x,運(yùn)行該函數(shù)的作用域?qū)ο髖 函數(shù)參數(shù)x接收三個(gè)參數(shù):數(shù)組項(xiàng)的值,該項(xiàng)在數(shù)組中的位置和數(shù)組對(duì)象本身every():對(duì)數(shù)組中的每一項(xiàng)運(yùn)行給定的函數(shù),如果該函數(shù)對(duì)每一項(xiàng)都返回true,則返回true some():對(duì)數(shù)組中的每一項(xiàng)運(yùn)行給定的函數(shù),如果該函數(shù)中某一項(xiàng)返回true,則返回true filter():對(duì)數(shù)組中的每一項(xiàng)運(yùn)行給定的函數(shù),返回該函數(shù)會(huì)返回true的項(xiàng)組成的數(shù)組 forEach():對(duì)數(shù)組中的每一項(xiàng)運(yùn)行給定的函數(shù),無(wú)返回值 map():對(duì)數(shù)組中的每一項(xiàng)運(yùn)行給定的函數(shù),返回每次函數(shù)調(diào)用結(jié)果組成的數(shù)組 */var numbers = [1, 2, 3, 4, 5, 4, 3, 2, 1]; //every() var everyResult = numbers.every(function(item, index, array) {return (item > 2); }); alert(everyResult); //false//some() var someResult = numbers.some(function(item, index, array) {return (item > 2); }); alert(someResult); //true//filter() var filterResult = numbers.filter(function(item, index, array) {return (item > 2); }); alert(filterResult); //3,4,5,4,3//map() var mapResult = numbers.map(function(item, index, array) {return item * 2; }); alert(mapResult); //2,4,6,8,10,8,6,4,2/* 歸并方法:reduce()和reduceRight() 接收兩個(gè)參數(shù):一個(gè)在數(shù)組每一項(xiàng)調(diào)用的函數(shù)x,作為歸并基礎(chǔ)的初始值y(可選) 函數(shù)x:接收四個(gè)參數(shù),前一個(gè)值,當(dāng)前值,項(xiàng)的索引和數(shù)組對(duì)象 reduce():從數(shù)組的第一項(xiàng)開(kāi)始 reduceRight():從數(shù)組的最后一項(xiàng)開(kāi)始 */var values = [1, 2, 3, 4, 5]; //reduce() var sum = values.reduce(function(prev, cur, index, array) {return prev + cur; }); alert(sum); //"15"//redeceRight() var sum2 = values.reduceRight(function(prev, cur, index, array) {return prev + cur; }) alert(sum2); //"15"

?

Date類(lèi)型的41個(gè)日期方法

Date類(lèi)型可分為如下:

繼承的方法:Date(),?parse(),toLocaleString(),toString()和valueOf()方法;

日期格式化方法:

toDateString()
toTimeString()
toLocaleDateString()
toLocaleTimeString()
toUTCString()

日期/時(shí)間組件方法:getTime(),?getTimezoneOffset()等

具體在實(shí)例中給出:

/* Date類(lèi)型 */ var now = new Date(); alert(now); //繼承的方法 //Date.parse()接收一個(gè)表示日期的字符串參數(shù),根據(jù)參數(shù)返回相依日期的毫秒數(shù); //ECMA-262規(guī)范沒(méi)有定義此函數(shù)支持的格式,應(yīng)地區(qū)實(shí)現(xiàn)而異 var someDate = new Date(Date.parse("May 25, 2004")); alert(someDate); //Tue May 25 2004 00:00:00 GMT+0800//Date.UTC()方法接收7個(gè)參數(shù):年year,月month(0開(kāi)始),日day(1-31),小時(shí)hour(0-23),分鐘min,秒s,毫秒ms //year和month為必須參數(shù);day默認(rèn)為1,其它參數(shù)默認(rèn)為0var y2k = new Date(Date.UTC(2000, 0)); alert(y2k); //Sat Jan 01 2000 08:00:00 GMT+0800var allFives = new Date(Date.UTC(2005, 4, 5, 17, 55, 55, 3600)); alert(allFives); //Fri May 06 2005 01:55:58 GMT+0800//Date()構(gòu)造函數(shù)會(huì)模仿Date.parse()和Date.UTC()方法 var d = new Date("May 25, 2004"); var dd = new Date(2005, 4, 5, 17, 55, 55, 3600); alert(d); //Tue May 25 2004 00:00:00 GMT+0800 alert(dd); //Fri May 06 2005 01:55:58 GMT+0800/* Date類(lèi)型也重寫(xiě)了toLocaleString(),toString()和valueOf()方法; 但是瀏覽器之間對(duì)toLocaleString()和toString()輸出不一致.下面輸出為火狐瀏覽器下輸出 */ var date = new Date("1, 1, 2001"); alert(date); //Mon Jan 01 2001 00:00:00 GMT+0800 alert(date.toLocaleString()); //2001/1/1 上午12:00:00 alert(date.toString()); //Mon Jan 01 2001 00:00:00 GMT+0800 //注意:valueOf()方法返回的是日期的毫秒數(shù) alert(date.valueOf()); //978278400000/* 日期格式化的方法 這些方法也是因?yàn)g覽器而異,以下為火狐瀏覽器輸出 */ var date = new Date("1, 1, 2001"); //toDateString():以特定于實(shí)現(xiàn)的格式顯示星期幾,月,日和年 alert(date.toDateString()); //Mon Jan 01 2001 //toTimeString():以特定于實(shí)現(xiàn)的格式顯示時(shí),分,秒和時(shí)區(qū) alert(date.toTimeString()); //00:00:00 GMT+0800 //toLocaleDateString():以特定于地區(qū)的格式顯示星期幾,月,日和年 alert(date.toLocaleDateString()); //2001/1/1 //toLocaleTimeString():以特定于實(shí)現(xiàn)的格式顯示時(shí),分,秒 alert(date.toLocaleTimeString()); //上午12:00:00 //toUTCString():以特定與實(shí)現(xiàn)的格式完整的UTC日期 alert(date.toUTCString()); //Sun, 31 Dec 2000 16:00:00 GMT/* 日期/時(shí)間組件方法 */ var date = new Date(2001, 1, 1); //返回表示日期的毫秒數(shù),與valueOf()返回的值相同 alert(date.getTime()); //返回本地時(shí)間與UTC世紀(jì)那相差的分鐘數(shù) alert(date.getTimezoneOffset()); //以毫秒數(shù)設(shè)置日期,傳入?yún)?shù)為毫秒 alert(date.setTime(3600000000000));//參數(shù)為為毫秒數(shù)// var date = new Date(2001, 1, 1); //取得四位數(shù)的年份 alert(date.getFullYear()); //返回UTC日期的4位數(shù)年份 alert(date.getUTCFullYear()); //設(shè)置日期年份,傳入?yún)?shù)必須為4位數(shù) alert(date.setFullYear(2002)); //參數(shù)為年 //設(shè)置UTC日期年份,傳入?yún)?shù)必須為4位數(shù) alert(date.setUTCFullYear(2003));//參數(shù)為年//月:0-11 var date = new Date(2001, 1, 1); //返回日期中的月份 alert(date.getMonth()); //返回UTC日期中的月份 alert(date.getUTCMonth()); //設(shè)置日期的月份,傳入?yún)?shù)必須大于0,超過(guò)則增加年份 alert(date.setMonth(1));//參數(shù)為月 //設(shè)置UTC日期的月份,傳入?yún)?shù)必須大于0,超過(guò)則增加年份 alert(date.setUTCMonth(2));//參數(shù)為月//日:1-31 var date = new Date(2001, 1, 1); //返回日期月份中的天數(shù) alert(date.getDate()); //返回UTC日期月份中的天數(shù) alert(date.getUTCDate()); //設(shè)置日期月份中的天數(shù) alert(date.setDate(23));//參數(shù)為日 //設(shè)置UTC日期月份中的天數(shù) alert(date.setUTCDate(24));//參數(shù)為日//星期:1-6,0表示星期日 var date = new Date(2001, 1, 1); //返回日期中的星期幾 alert(date.getDay(2)); //返回UTC日期中的星期幾 alert(date.getUTCDay(3));//時(shí):0-23 var date = new Date(2001, 1, 1); //返回日期中的小時(shí)數(shù) alert(date.getHours()); //返回UTC日期中的小時(shí)數(shù) alert(date.getUTCHours()); //設(shè)置日期中的小時(shí)數(shù) alert(date.setHours(2));//參數(shù)為時(shí) //設(shè)置UTC日期中的小時(shí)數(shù) alert(date.setUTCHours(3));//參數(shù)為時(shí)//分:0-59 var date = new Date(2001, 1, 1); //返回日期中的分鐘數(shù) alert(date.getMinutes()); //返回UTC日期中的分鐘數(shù) alert(date.getUTCMinutes()); //設(shè)置日期中的分鐘數(shù) alert(date.setMinutes(34));//參數(shù)為分 //設(shè)置UTC日期中的分鐘數(shù) alert(date.setUTCMinutes(35));//參數(shù)為分//秒:0-59 var date = new Date(2001, 1, 1); //返回日期中的秒數(shù) alert(date.getSeconds()); //返回UTC日期中的秒數(shù) alert(date.getUTCSeconds()); //設(shè)置日期中的秒數(shù) alert(date.setSeconds(45));//參數(shù)為秒 //設(shè)置UTC日期中的秒數(shù) alert(date.setUTCSeconds(46));//參數(shù)為秒//毫秒 var date = new Date(2001, 1, 1); //返回日期中的毫秒數(shù) alert(date.getMilliseconds()); //返回UTC日期中的毫秒數(shù) alert(date.getUTCMilliseconds()); //設(shè)置日期中的毫秒數(shù) alert(date.setMillseconds(3454));//參數(shù)為毫秒 //設(shè)置UTC日期中的毫秒數(shù) alert(date.setUTCMillseconds(1111));//參數(shù)為毫秒

?

Function類(lèi)型

/* 函數(shù)Function 類(lèi)型 */ /* 1.函數(shù)是對(duì)象,函數(shù)名是只想函數(shù)對(duì)象的指針,不會(huì)與函數(shù)綁定(函數(shù)是對(duì)象,函數(shù)名是指針) */ function sum(num1, num2) {return num1 + num2; } alert(sum(10, 10)); //20var anotherSum = sum; alert(anotherSum(10, 10)); //20//sum是函數(shù)的指針并不與函數(shù)綁定 sum = null; alert(anotherSum(10, 10)); //20/* 2.函數(shù)沒(méi)有重載 */ function addNum(num) {return num + 100; }function addNum(num) {return num + 200; }alert(addNum(100)); //300/* 3.解析器會(huì)通過(guò)“函數(shù)聲明提升”將函數(shù)聲明添加到執(zhí)行環(huán)境中去,而函數(shù)表達(dá)式則須解析器執(zhí)行到它所在的代碼行才會(huì)被執(zhí)行 */ alert(sum(10, 10)); //20 function sum(num1, num2) {return num1 + num2; }alert(sum2(19, 10)); //error var sum2 = function(num1, num2) {return num1 + num2; }/* 4.函數(shù)的內(nèi)部屬性:arguments和this,callee,caller 注意:不能在嚴(yán)格模式下使用callee,caller */ //arguments保存函數(shù)的參數(shù),該對(duì)象還有一個(gè)callee的屬性,callee是一個(gè)指針,指向擁有arguments對(duì)象的函數(shù) function factorial(num) {if (num <= 1) {return -1;} else {return num * arguments.callee(num - 1);} }alert(4); //24//this引用的是函數(shù)執(zhí)行的環(huán)境對(duì)象。 var color = "red"; var o = {color : "blue"};function showColor() {alert(this.color); }//直接調(diào)用函數(shù)則this引用的環(huán)境對(duì)象是window showColor(); //red alert(window.color);//red //this引用的環(huán)境對(duì)象是o,所以調(diào)用的是o中的color o.showColor(); //red/* caller保存至調(diào)用當(dāng)前函數(shù)的函數(shù)的引用(在全局作用域中調(diào)用當(dāng)前函數(shù)則值為null),除opera早期版本不支持外其他都支持, 注意:ECMAScript并沒(méi)有定義這個(gè)屬性 */ function outer() {inner(); }function inner() {alert(inner.caller); }outer(); //顯示outer函數(shù)的源代碼/* apply(), call() apply():接收兩個(gè)參數(shù)x,y;x為運(yùn)行函數(shù)的作用域,y為參數(shù)數(shù)組(可以為Array實(shí)例) call():第一個(gè)參數(shù)與apply()類(lèi)似,但是后面的參數(shù)不已數(shù)組形式傳遞,而是直接傳遞給數(shù)組 */ function sum(num1, num2) {return num1 + num2; }//注意:在嚴(yán)格模式下,未指定環(huán)境對(duì)象而調(diào)用函數(shù)則this值不會(huì)轉(zhuǎn)型為window,this此時(shí)為undefined 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)); //20function sum(num1, num2) {return num1 + num2; } function callSum(num1, num2) {return sum.call(this, num1, num2); } alert(callSum(10, 10)); //20

?

轉(zhuǎn)載于:https://www.cnblogs.com/libra-yong/p/6903705.html

總結(jié)

以上是生活随笔為你收集整理的【js实例】Array类型的9个数组方法,Date类型的41个日期方法,Function类型的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。