ประเภทวัตถุ
ประเภทอาร์เรย์
วิธีการสั่งซื้อใหม่: เปรียบเทียบ
คำสั่งซื้อจากน้อยไปมาก:
ฟังก์ชั่นเปรียบเทียบ (ค่า 1, value2) {ถ้า (ค่า 1 <ค่า 2) {return -1; } if (value1> value2) {return 1; } else {return 0; }} ค่า var = [0,1,5,10,15]; value.sort (เปรียบเทียบ); console.log (ค่า); // [0,1,5,10,15]คำสั่งซื้อจากมากไปน้อย:
ฟังก์ชั่นเปรียบเทียบ (ค่า 1, value2) {ถ้า (ค่า 1 <ค่า 2) {return 1; } if (value1> value2) {return -1; } else {return 0; -ชิ้น:
ชิ้น (เริ่มต้นจบ); เมธอด slice () ส่งคืนรายการทั้งหมดเริ่มต้นจากตำแหน่งที่ระบุของพารามิเตอร์ไปยังจุดสิ้นสุดของอาร์เรย์ปัจจุบัน หากมีสองพารามิเตอร์วิธีการส่งคืนรายการระหว่างตำแหน่งเริ่มต้นและสิ้นสุด แต่ไม่รวมรายการที่ตำแหน่งสิ้นสุด
สี var = ["สีแดง", "สีเขียว", "สีน้ำเงิน", "เหลือง", "สีม่วง"]; var colors2 = colours.slice (1); var colours3 = colours.slice (1,4); console.log (colors2); // สีเขียว, สีน้ำเงิน, สีเหลือง, purpleconsole.log (สี 3); // สีเขียว, น้ำเงิน, เหลือง
ประกบ:
Splice () มีฟังก์ชั่นการลบการแทรกและการเปลี่ยน
ลบ:
จำเป็นต้องมีพารามิเตอร์สองตัวที่ตั้งของรายการแรกที่จะลบและจำนวนรายการที่จะลบ
var colours = ["red", "green", "blue"]; var ลบ = colors.splice (0,1); console.log (สี); // สีเขียว, blueconsole.log (ลบ); // สีแดง
แทรก:
ต้องใช้พารามิเตอร์สามตัว: เริ่มต้นตำแหน่ง 0 (จำนวนรายการที่จะลบ) และรายการที่จะแทรก
var colours = ["Red", "Green", "Blue"]; var ลบ = colors.splice (1,0, "สีเหลือง", "สีส้ม"); console.log (สี); // ["สีแดง", "เหลือง", "สีส้ม", "สีเขียว", "สีน้ำเงิน"] console.log (ลบ); // กลับไปที่ว่าง
แทนที่:
ต้องใช้พารามิเตอร์สามตัว: ตำแหน่งเริ่มต้นจำนวนรายการที่จะลบและจำนวนรายการใด ๆ ที่จะแทรก
var colours = ["Red", "Green", "Blue"]; var ลบ = colors.splice (1,1, "เหลือง", "สีส้ม"); console.log (สี); // ["สีแดง", "เหลือง", "สีส้ม", "สีน้ำเงิน"] console.log (ลบ); // ["สีเขียว"]
ประเภทวันที่
ประเภท regexp
var pattern1 = /[bc] /i; var pattern2 = ใหม่ regexp ("[bc] ที่", "i");Pattern1 และ Pattern2 เป็นนิพจน์ปกติที่เทียบเท่ากันสองแบบ ควรสังเกตว่าพารามิเตอร์ทั้งสองที่ส่งผ่านไปยังตัวสร้าง Regexp คือสตริง (ตัวอักษร Regex ไม่สามารถส่งผ่านไปยังตัวสร้าง Regexp) เนื่องจากข้อโต้แย้งรูปแบบของตัวสร้าง regexp เป็นสตริงในบางกรณีสตริงจะถูกหลบหนีสองครั้ง
var pattern1 =/[bc]/i; var pattern2 = ใหม่ regexp ("// [bc //] ที่", "i");วิธีการอินสแตนซ์ regexp
ผู้บริหาร
EXEC ได้รับพารามิเตอร์คือสตริงเพื่อใช้รูปแบบและส่งคืนอาร์เรย์ที่มีข้อมูลการจับคู่แรก
var text = "cat, bat, sat, fat"; var pattern1 = /.at/; // ["แมว"]
จับคู่
การจับคู่เป็นวิธีการสำหรับสตริงในการดำเนินการตามกฎการแสดงออกปกติและพารามิเตอร์ของมันเป็นนิพจน์ทั่วไป
var text = "cat, bat, sat, fat"; var pattern1 = /.at/; // ["แมว"]
ทดสอบ
ทดสอบ () รับพารามิเตอร์สตริง
var text = "000-00-0000"; var pattern = // d {3}-/d {2}-/d {4}/; ถ้า (pattern.test (ข้อความ)) {console.log ("รูปแบบถูกจับคู่"); // รูปแบบถูกจับคู่}ประเภทฟังก์ชัน
ฟังก์ชันคุณสมบัติภายใน
แปลงข้อโต้แย้งเป็นอาร์เรย์
(function () {var slice = array.prototype.slice, aarguments = slice.apply (อาร์กิวเมนต์); console.log (aarguments);}) (10, 20, 30); arguments.calleeคุณสมบัตินี้เป็นตัวชี้ไปยังฟังก์ชันที่เป็นเจ้าของวัตถุอาร์กิวเมนต์นี้ เมื่อฟังก์ชั่นกำลังทำงานในโหมดที่เข้มงวดการเข้าถึงอาร์กิวเมนต์ callee จะส่งผลให้เกิดข้อผิดพลาด
คุณสมบัติฟังก์ชันและวิธีการ
ความยาว
แอตทริบิวต์ความยาวแสดงจำนวนพารามิเตอร์ที่มีชื่อที่ฟังก์ชั่นต้องการรับ
ฟังก์ชั่น SaysName (ชื่อ) {Alert (ชื่อ);} ฟังก์ชันผลรวม (num1, num2) {return num1 + num2;} ฟังก์ชั่น sayshi () {alert ("hi");} console.log (sayname.length); //1console.log(sum.length); //2console.log(Sayhi.length); // 0ต้นแบบ
โทรสมัคร
ฟังก์ชั่นผลรวม (num1, num2) {return num1 + num2;} ฟังก์ชั่น callsum1 (num1, num2) {return sum.apply (นี้, อาร์กิวเมนต์);} ฟังก์ชัน callsum2 (num1, num2) {return sum.apply (นี่, [num1, num2]); } console.log (callesum1 (10,10)); // 20console.log (callesum2 (10,10)); //20Window.Color = "สีแดง"; var o = {color: "blue"}; function saycolor () {console.log (this.color);} saycolor (); // redsaycolor.call (นี่); // redsaycolor.call (หน้าต่าง); // redsaycolor.call (o); // สีฟ้าประเภทบรรจุภัณฑ์พื้นฐาน
var value = "25"; var number = number (value); console.log (หมายเลข typeof); console.log (หมายเลขอินสแตนซ์ของหมายเลข); // falsevar obj = หมายเลขใหม่ (ค่า); console.log (typeof obj); console.log
ประเภทบูลีน
var talseObject = ใหม่บูลีน (เท็จ); var result = falseObject && true; // true // วัตถุทั้งหมดในนิพจน์บูลีนจะถูกแปลงเป็น TRUE ดังนั้นวัตถุ talseObject จะแสดงถึง trueconsole.log (ผลลัพธ์); // TrueVar FALSEVALUE = FALSE; result = FALSEVALUE && true; console.log (ผลลัพธ์); //falseconsole.log(TypeOf TalseObject); //objectconsole.log(Typeof FALSEVALUE); // booleanconsole.log (talseObject อินสแตนซ์ของบูลีน); //trueconsole.log (อินสแตนซ์ของบูลีน); // เท็จ
ประเภทหมายเลข
var numberObject = หมายเลขใหม่ (10); var numberValue = 10; console.log (typeof numberObject); // objectConsole.log (typoef numberValue); // numberConsole.log (หมายเลขอินสแตนซ์หมายเลขของหมายเลข); // trueconsole.log (หมายเลขอินสแตนซ์ของหมายเลขของหมายเลข); // เท็จ
ประเภทสตริง
วิธีการ
charat () charcodeat ()
วิธี Charat () ส่งคืนสตริงที่ตำแหน่งที่กำหนดเป็นสตริงตัวละครเดี่ยว
Charcodeat () ส่งคืนการเข้ารหัสอักขระ
var stringValue = "Hello World"; console.log (StringValue.charat (1)); // econsole.log (stringValue.charcodeat (1)); // 101
วิธีการใช้งานสตริง
concat ()
concat () ใช้เพื่อแยกสายอย่างน้อยหนึ่งสาย
var stringValue = "hello"; var result = stringValue.concat ("โลก"); console.log (ผลลัพธ์); // hello worldconsole.log (StringValue); // สวัสดีSlice (เริ่มจบ)
สิ้นสุดหมายถึงที่สตริงสิ้นสุด
หากส่งผ่านในจำนวนลบเมธอด slice () จะเพิ่มค่าที่ส่งผ่านในค่าลบกับความยาวสตริง
var str = "Hello Happy World!"; console.log (str.slice (6)); // Happy World! console.log (Str.slice (6,11)); // happyconsole.log (str.slice (-3)); // ld! console.log (str.slice (3, -4)); // lo happy wo
Substring (เริ่มต้นจบ)
หากผ่านในจำนวนลบ substring () จะแปลงพารามิเตอร์อักขระทั้งหมดเป็น 0
var str = "Hello Happy World!"; console.log (str.substring (6)); // Happy World! console.log (str.substring (6,11)); // happy world.log (str.substring (-3)); // สวัสดี Happy World! console.log (str.substring (3, -4)); // HEL
substr (เริ่มต้นความยาว)
หากตัวเลขที่ส่งผ่านเป็นจำนวนลบวิธีการ substr () จะเพิ่มพารามิเตอร์แรกลบให้กับความยาวของสตริงและแปลงพารามิเตอร์ที่สองลบเป็น 0
var str = "Hello World!"; console.log (str.substr (3)); // lo World! console.log (str.substr (3, 7)); // lo worldconsole.log (str.substr (-3)); // ld! console.log (str.substr (3, -3)); // สตริงเปล่า
วิธีตำแหน่งสตริง
indexof () lastIndexof () var stringValue = "hello world"; console.log (stringvalue.indexof ("o")); // 4console.log (StringValue.lastIndexof ("O")); // 7ทั้งสองวิธีสามารถรับพารามิเตอร์ที่สองที่เป็นตัวเลือกเพื่อระบุตำแหน่งที่จะเริ่มการค้นหาในสตริง
var stringValue = "hello world"; console.log (stringValue.indexof ("o", 6)); // 7console.log (StringValue.lastIndexof ("O", 6)); // 4วิธีการจับคู่รูปแบบสำหรับสตริง
จับคู่()
var text = "cat, bat, sat, fat"; var pattern = /.at/; //0console.log(Matches [0]); // catconsole.log (pattern.lastindex); // 0
ค้นหา()
var text = "cat, bat, sat, fat"; var pos = text.search (/at/); console.log (pos); // 1
แทนที่()
var text = "cat, bat, sat, fat"; var result = text.replace ("at", "ond"); console.log (ผลลัพธ์); // cond, bat, sat, fatvar result = text.replace (/at/g, "ond"); console.log (ผลลัพธ์); // cond, bond, sond, fondวัตถุระดับโลก
วิธีการเข้ารหัส URI
วิธีการ encodeuri () และ encodeuricomponent () ของวัตถุทั่วโลกสามารถเข้ารหัส URIs (ตัวระบุทรัพยากรที่เหมือนกัน) สำหรับการส่งไปยังเบราว์เซอร์
var url = "http://www.baidu.com/"; console.log (encodeuri (url)); console.log (encodeuricomponent (url)); encodeuri () และ encodeuricomponent ()
วัตถุคณิตศาสตร์
วิธีการสุ่ม ()
เมธอด math.random () ส่งคืนหมายเลขสุ่มระหว่าง 0 ถึง 1 ไม่ได้มี 0 และ 1 วิธีนี้ใช้งานได้จริงสำหรับบางไซต์เพราะสามารถใช้ในการสุ่มแสดงคำพูดที่มีชื่อเสียงและเหตุการณ์ข่าว ด้วยการใช้สูตรต่อไปนี้คุณสามารถใช้ Math.random () เพื่อสุ่มเลือกค่าจากช่วงของจำนวนเต็ม
value = math.floor (math.random () *จำนวนทั้งหมดของค่าที่เป็นไปได้ + ค่าที่เป็นไปได้แรก)
ตัวอย่างเช่น: หากคุณต้องการเลือกค่าระหว่าง 1 ถึง 10 คุณสามารถเขียนโค้ดได้เช่นด้านล่าง:
var num = math.floor (math.random ()*10 + 1); ฟังก์ชั่น selectfrom (lowerValue, uppervalue) {var choice = uppervalue - LowerValue + 1; return math.floor (math.random ()*ตัวเลือก+lowerValue);} var num = selectfrom (2,10); console.log (num); var colours = ["สีแดง", "สีเขียว", "สีน้ำเงิน", "สีเหลือง", "สีดำ", "สีม่วง", "สีน้ำตาล"];