備忘單是我們開發人員始終需要的參考。因此,這里我編譯了許多JavaScript參考代碼。查看分類并找到它。這篇文章對學習者和開發人員都有幫助。


JavaScript Number方法備忘單
- toExponential():以字符串形式返回表示Number對象的字符串
function expo(x, f) {
return
Number.parseFloat(x).toExponential(f);
}
console.log(expo(123456, 2));
// -> 1.23e+5
- toFixed():使用定點表示法格式化數字
function financial(x) {
return Number.parseFloat(x).toFixed(2);
}
console.log(financial(123.456));
// -> 123.46
- toPrecision():以指定的精度返回表示Number對象的字符串
function precise(x) {
return
Number.parseFloat(x).toPrecision(4);
}
console.log(precise(123.456));
// -> 123.5
- toString():返回表示指定Number對象的字符串
function hexColour(c) {
if (c < 256) {
return Math.abs(c).toString(16);
}
return 0;
}
console.log(hexColour(233));
// -> e9
- valueOf():返回數字對象的包裝原始值
const numObj = new Number(42);
console.log(typeof numObj);
// -> object
const num = numObj.valueOf();
console.log(num);
// -> 42
console.log(typeof num);
// -> number
JavaScript循環備忘單
- 對于循環
for (var i = 0; < 10; i++) {
console.log(i + ": " + i * 3 + "<br />");
}
// -> 0: 0<br />
// -> 1: 3<br />
// -> ...
let a = [1, 2, 3];
var sum = 0;
for (var i - 0; i <a.length; i++) {
sum += a[i];
} // pasing an array
console.log(sum);
// -> 6
- While循環
var i = 1; // initialize
while (i < 100) { // enters the cycle if statement is true
i *= 2; // increment to avoid infinte loop
console.log(i + ", "); // output
}
// 2,
// 4,
// ...
// 128,
- 循環執行
var i = 1; // initialize
while (i < 100) { // enters the cycle asleast once
i *= 2; // increment to avoid infinte loop
console.log(i + ", "); // output
} while (1 < 100); // repeats cycle if statement is true at the end
// 2,
// 4,
// ...
// 128,
- 打破
for (var i = 0; i < 10; i++) {
if (i == 5 ) { break; } // stops and exits the cycle
console.log(i + ", "); // Lat output number is 4
}
// -> 0,
// -> 1,
// ...
// -> 4,
- 繼續
for (var i = 0; i < 10; i++) {
if (i == 5 ) { continue; } // skips the rest of the cycle
console.log(i + ", "); // skips 5
}
// -> 0,
// -> 1,
// ...
// -> 9,
JavaScript字符串方法備忘單
- charAt():返回指定索引處的字符
const sentence = "Jeff bezos is now the second richest.";
const index = 4;
console.log(`The character at index ${index} is ${sentence.charAt(index)}`);
// The character at index 4 is f
- concat():連接兩個或多個字符串,并返回所連接字符串的副本
const str1 = "Hello";
cosnt str2 = "World";
console.log(str1.concat(" ", str2));
// -> Hello World
console.log(str2.concat(", ", str1));
// -> World, Hello
- replace():搜索子字符串(或正則表達式)和字符串之間的匹配項,并將匹配的子字符串替換為新的子字符串
const p = "Talk is cheap. Show me the work. - Someone";
console.log(p.replace("work", "code"));
// -> Talk is cheap. Show me the code. - Someone
- search():搜索正則表達式和字符串之間的匹配項,并返回匹配項的位置
const paragraph = "The quick brown fox jumps over the lazy dog.";
// any character that is not a word character or whitespace
const regex = /[^ws]/g;
console.log(paragraph.search(regex));
// -> 43
- slice():提取字符串的一部分并返回新的字符串
const str = "The quick brown fox jumps over the lazy dog.";
consolelog(str.slice(31));
// -> the lazy dog
console.log(str.slice(4, 19));
// -> quick brown fox
- trim():刪除字符串兩端的空格
const greeting = " Hello world! ";
console.log(greeting);
// -> Hello world!
console.log(greeting.trim());
// -> Hello world!
- substr():從字符串中提取字符,從指定的起始位置開始,直到指定的字符數
const str = "Mozilla";
console.log(str.substr(1, 2));
// -> oz
console.log(stre.substr(2));
// -> zilla
- toLowerCase():將字符串轉換為小寫字母
const sentence = "Elon became the richest last night.";
console.log(sentence.toLowerCase());
// -> elon became the richest last night.
JavaScript數組方法指導表
- concat():連接兩個或多個數組,并返回聯接數組的副本
let array1 = ["a", "b", "c"];
let array2 = ["d", "e", "f"];
let array3 = array1.concat(array2);
console.log(array3);
// -> Array(6) ["a", "b", "c", "d", "e", "f" ]
- indexOf():在數組中搜索元素并返回其位置
let beasts = ["ant", "bison", "camel", "duck", "bison"];
console.log(beasts.indexOf("bison"));
// -> 1
// start from index 2
console.log(beasts.indexOf("bison", 2));
// -> 4
- join():將數組的所有元素連接到一個字符串中
let elements = ["Fire", "Air", "Water"];
console.log(elements.join());
// -> Fire,Air,Water
console.log(elements.join(" "));
// -> Fire Air Water
- pop():刪除數組的最后一個元素,并返回該元素
let plants = ["broccoli", "cauliflower", "cabbage", "kale", "tomato"];
console.log(plants.pop());
// -> tomato
console.log(plants);
// -> Array(4) ["brocxoli", "cauliflower", "cabbage", "kale"]
- reverse():反轉數組中元素的順序
let array1 = ["one", "two", "three"];
console.log("array1:", array1);
// -> array1: Array(3) [ "one", "two", "three" ]
let reversed = array1.reverse();
console.log("reversed", reversed);
// -> reversed: Array(3) [ "three", "two", "one" ]
- shift():刪除數組的第一個元素,并返回該元素
let array1 = [1, 2, 3];
let firstElement = array1.shift();
console.log(array1);
// -> Array [ 2, 3 ]
- sort():對數組的元素進行排序
let months = ["March", "Jan", "Feb", "Dec"];
months.sort();
console.log(months);
// -> Array(4) [ "Dec", "Feb", "Jan", "March" ]
- toString():將數組轉換為字符串,并返回結果
const array1 = [1, 2, "a", "1a"];
console.log(array1.toString());
// -> 1,2,a,1a
JavaScript數據類型備忘單
var age = 18; // Number
var name = "Rahul"; // string
var name = {first:"Rahul", last:"Singh"}; // object
var truth = false; // boolean
var sheets = ["HTML", "CSS", "JS"]; // array
var a; typeof a; // undefined
var a = null; // value null
JavaScript運算符備忘單
a = b + c - d; // addition, substraction
a = b * (c / d); // multiplication, division
x = 100 % 48; // modulo. 100 / 48 remainder = 4
a++; b--; // postfix increment and decrement
變量備忘單
- var:最常見的變量??梢灾匦路峙?,但只能在函數中訪問。執行代碼時,用var定義的變量移到頂部。
- const:在出現在代碼中之前無法重新分配并且無法訪問
- let:與const類似,但是可以重新分配let變量,但不能重新聲明
var a; // variable
var b = "init"; // string
var c = "Hi" + "" + "Rahul"; // "Hi Rahul"
var d = 1 + 2 + "3"; // "33"
var e = [2,3,5,8]; // array
var f = false; // boolean
var g = /()/; // RegEx
var h = function(){}; // function object
const PI = 3.14; // constant
var a = 1, b = 2, c = a + b; // one line
let z = 'zzz'; // block scope local variable
獲取日期方法提示表
- getFullYear():根據當地時間返回指定日期的年份
const moonLanding = new Date("January 08, 69 00:20:10");
console.log(moonLanding.getFullYear());
// -> 1969
- getMonth():根據本地時間返回指定日期中的月份,該值從零開始(其中零表示一年的第一個月)。
const moonLanding = new Date("January 08, 69 00:20:10");
console.log(moonLanding.getMonth()); // (January gives 0)
// -> 6
- getDate():根據當地時間返回指定日期的月份
const birthday = new Date("June 16, 2004 23:14:00");
const date1 = birthday.getDate();
console.log(date1);
// -> 19
- getHours():根據當地時間返回指定日期的小時
const birthday = new Date("June 16, 04 4:20");
console.log(birthday.getHours());
// -> 4
- getMinutes():根據當地時間返回指定日期的分鐘
const birthday = new Date("June 16, 04 04:10");
console.log(birthday.getMinutes());
// -> 20
- getSeconds()根據當地時間返回指定日期中的秒數
const moonLanding = newDate("June 16, 69 00:23:11");
console.log(moonLanding.getSeconds());
// -> 18
版權聲明:本文內容由互聯網用戶自發貢獻,該文觀點僅代表作者本人。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。如發現本站有涉嫌抄襲侵權/違法違規的內容, 請發送郵件至 舉報,一經查實,本站將立刻刪除。
發表評論
請登錄后評論...
登錄后才能評論