JavaScript中各種引用型別的常用操作方法小結_基礎知識
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);

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

如何使用WebSocket和JavaScript實現線上語音辨識系統引言:隨著科技的不斷發展,語音辨識技術已成為了人工智慧領域的重要組成部分。而基於WebSocket和JavaScript實現的線上語音辨識系統,具備了低延遲、即時性和跨平台的特點,成為了廣泛應用的解決方案。本文將介紹如何使用WebSocket和JavaScript來實現線上語音辨識系

股票分析必備工具:學習PHP和JS繪製蠟燭圖的步驟,需要具體程式碼範例隨著網路和科技的快速發展,股票交易已成為許多投資者的重要途徑之一。而股票分析是投資人決策的重要一環,其中蠟燭圖被廣泛應用於技術分析。學習如何使用PHP和JS繪製蠟燭圖將為投資者提供更多直觀的信息,幫助他們更好地做出決策。蠟燭圖是一種以蠟燭形狀來展示股票價格的技術圖表。它展示了股票價格的

人臉偵測辨識技術已經是一個比較成熟且應用廣泛的技術。而目前最廣泛的網路應用語言非JS莫屬,在Web前端實現人臉偵測辨識相比後端的人臉辨識有優勢也有弱勢。優點包括減少網路互動、即時識別,大大縮短了使用者等待時間,提高了使用者體驗;弱勢是:受到模型大小限制,其中準確率也有限。如何在web端使用js實現人臉偵測呢?為了實現Web端人臉識別,需要熟悉相關的程式語言和技術,如JavaScript、HTML、CSS、WebRTC等。同時也需要掌握相關的電腦視覺和人工智慧技術。值得注意的是,由於Web端的計

WebSocket與JavaScript:實現即時監控系統的關鍵技術引言:隨著互聯網技術的快速發展,即時監控系統在各個領域中得到了廣泛的應用。而實現即時監控的關鍵技術之一就是WebSocket與JavaScript的結合使用。本文將介紹WebSocket與JavaScript在即時監控系統中的應用,並給出程式碼範例,詳細解釋其實作原理。一、WebSocket技

泛型函數在Go中處理指標型別時,會收到原始變數的引用,允許修改變數值。引用類型則在傳遞時會被複製,使函數無法修改原始變數值。實戰案例包括使用泛型函數比較字串或數字切片。

如何利用JavaScript和WebSocket實現即時線上點餐系統介紹:隨著網路的普及和技術的進步,越來越多的餐廳開始提供線上點餐服務。為了實現即時線上點餐系統,我們可以利用JavaScript和WebSocket技術。 WebSocket是一種基於TCP協定的全雙工通訊協議,可實現客戶端與伺服器的即時雙向通訊。在即時線上點餐系統中,當使用者選擇菜餚並下訂單

隨著網路金融的快速發展,股票投資已經成為了越來越多人的選擇。而在股票交易中,蠟燭圖是常用的技術分析方法,它能夠顯示股票價格的變動趨勢,幫助投資人做出更精準的決策。本文將透過介紹PHP和JS的開發技巧,帶領讀者了解如何繪製股票蠟燭圖,並提供具體的程式碼範例。一、了解股票蠟燭圖在介紹如何繪製股票蠟燭圖之前,我們首先需要先了解什麼是蠟燭圖。蠟燭圖是由日本人

JavaScript和WebSocket:打造高效的即時天氣預報系統引言:如今,天氣預報的準確性對於日常生活以及決策制定具有重要意義。隨著技術的發展,我們可以透過即時獲取天氣數據來提供更準確可靠的天氣預報。在本文中,我們將學習如何使用JavaScript和WebSocket技術,來建立一個高效的即時天氣預報系統。本文將透過具體的程式碼範例來展示實現的過程。 We
