JavaScript/原始資料型別
原始型別使用固定格式;一些型別只能包含有限數量的特定值。相反,物件更復雜,尤其是包括方法和屬性。
除了null和undefined,原始型別都有相應的 物件包裝器,具有特定資料型別的方法。因此,您將在本頁上找到一些方法的描述。
字串是一種資料型別,用於儲存任意長度的文字。字串變數透過將字串文字賦值給它們來建立。字串文字可以包含在" "或' '中。
"use strict";
const myName_1 = "Mike"; // double quote
const myName_2 = 'Monica'; // apostrophe
const myName_3 = "naɺ̠ɯçito"; // non-latin characters
如果您的字串文字包含"或',您可以使用另一個作為外部定界符,或者用\進行轉義。
"use strict";
const book_1 = "Mike's book";
const monica_1 = 'Here name is "Monica".';
const book_2 = 'Mike\'s book';
const monica_2 = "Here name is \"Monica\".";
如果您的字串文字是透過一些固定文字加上一些動態部分計算出來的,您可以使用模板文字技術。在這裡,文字包含在反引號` `中,幷包含變數和表示式。
"use strict";
const a = 1;
const b = 2;
const resultMessage = `The sum of ${a} and ${b} is: ${a + b}.`;
// same as:
'The sum of ' + a + ' and ' + b + ' is: ' + (a + b);
alert(resultMessage);
+運算子連線兩個字串,例如alert("Hello " + "world!");。此外,還有很多 方法用於字串。
提示:JavaScript 沒有類似 '字元' 或 '位元組' 的資料型別。
我們展示了一些經常使用的方法。有關完整列表,請參閱 MDN。
length是一個屬性,而不是一個方法。因此沒有括號()。它返回字串的長度作為一個整數。
const foo = "Hello!";
alert(foo.length); // 6
如果字串包含指定的字串,則該方法返回true,否則返回false。
let text = "Hello world, hello Wikiversity!";
document.write(text.includes("Hello")); //true
該方法返回一個字串,其中 'text' 附加到原始字串。
const foo = "Hello";
const bar = foo.concat(" World!");
alert(bar); // Hello World!
該方法返回 'searchText' 第一次出現的索引,從 0 開始。如果找不到 'searchText',則返回 -1。該方法區分大小寫。
const foo = "Hello, World! How do you do?";
alert(foo.indexOf(" ")); // 6
const hello = "Hello world, welcome to the universe.";
alert(hello.indexOf("welcome")); // 13
該方法將字串與正則表示式(=模式)進行比較,返回字串中第一個匹配項的索引。它類似於indexOf(),但功能強大得多。例如:可以透過將"hello"中的引號替換為正斜槓和末尾的i來使其不區分大小寫。
let text = "Hello world, hello Wikiversity!";
document.write(text.search(/hello/i)); // 0
該方法返回 'searchText' 最後一次出現的索引。如果找不到 'searchText',則返回 -1。該方法區分大小寫。
const foo = "Hello, World! How do you do?";
alert(foo.lastIndexOf(' ')); // 24
該方法返回一個字串,其中 'text' 在原始字串中被 'NewText' 替換。只替換第一個出現的字元。該方法區分大小寫。
const foo = "foo bar foo bar foo";
const newString = foo.replace("bar", "NEW"):
alert(foo); // foo bar foo bar foo
alert(newString); // foo NEW foo bar foo
正如您所見,replace 方法只返回新內容,不會修改 'foo' 中的原始字串。
該方法返回從 'start' 位置開始的子字串。
"hello".slice(1); // "ello"
當提供 'end' 時,它們將提取到,但不包括結束位置。
"hello".slice(1, 3); // "el"
slice 允許透過使用負索引來提取從字串末尾引用的文字。
"hello".slice(-4, -2); // "el"
與substring不同,slice 方法永遠不會交換 'start' 和 'end' 位置。如果 'start' 在 'end' 之後,slice 將嘗試按原樣提取內容,但很可能會提供意外的結果。
"hello".slice(3, 1); // ""
該方法已 棄用。請改用substring或slice。
該方法提取從 'start' 位置開始的子字串。
"hello".substring(1); // "ello"
當提供 'end' 時,它們將提取到,但不包括結束位置。
"hello".substring(1, 3); // "el"
substring 始終從左到右工作。如果 'start' 位置大於 'end' 位置,substring 將交換值;雖然有時有用,但這並不總是你想要的;slice 提供了不同的行為。
"hello".substring(3, 1); // "el"
該方法返回當前字串的小寫形式。
const foo = "Hello!";
alert(foo.toLowerCase()); // hello!
該方法返回當前字串的大寫形式。
const foo = "Hello!";
alert(foo.toUpperCase()); // HELLO!
數字
[edit | edit source]Number 是兩種數值型別之一(另一種是 BigInt)。Number 使用 IEEE 754 定義的統一 64 位格式儲存整數和浮點數。這意味著,與其他一些語言不同,JavaScript 沒有為整數和浮點數使用不同的資料型別。
此類值的可能範圍大約為 -10300 到 +10300,精度隨距離 0 的距離而變化。
在 -(253 − 1) 到 +253 − 1 的範圍內,整數運算沒有不確定性。253 = 9,007,199,254,740,992,略小於 1016。
"use strict";
let counter = 20; // no decimal point
alert(counter + " " + typeof counter);
let degree = 12.1; // with decimal point
alert(degree + " " + typeof degree);
對於 Number,可以使用通常的算術運算子(“冪”是 **,“模”是 %)、比較運算子(<、> 等)和位運算子。
與其他一些語言相反,兩個整數的除法可能導致帶有小數點的數字,例如 alert(1/3);。
數字的屬性和方法
[edit | edit source]許多屬性和方法支援數字運算。在內部,它們在不同的區域實現。
- 內建物件
Math提供表示常見數學常數(如 π 或 e)的屬性。語法:Math.xyz(無括號) - 內建物件
Math提供常見的數學函式,如 sin 或 log。語法:Math.xyz()(帶括號) - 物件
Number提供描述數字資料型別實現的屬性,如 MAX_VALUE 或 NEGATIVE_INFINITY。語法:Number.xyz(無括號) - 物件
Number提供用於檢查數字和其他資料型別之間關係的 靜態 方法,例如 isInteger 或 parseFloat。語法:Number.xyz()(帶括號) - 物件
Number提供作用於具體數字值或變數的 例項 方法,例如 toExponential 或 toFixed。語法:value.xyz()(帶括號)
// some examples
"use strict";
const var_1 = Math.PI;
alert(var_1);
alert(var_1.toFixed(2));
const var_2 = Math.sqrt(3);
alert(var_2);
alert(Number.MAX_VALUE);
alert(Number.isInteger(123)); // true
alert(Number.isInteger(12.3)); // false
我們展示了一些經常使用的屬性和方法。有關完整列表,請參閱 MDN Math 和 MDN Numbers。
屬性
[edit | edit source]最常用的常數
Math.E返回常數 e。Math.PI返回常數 pi。Math.LN10返回 10 的自然對數。Math.LN2返回 2 的自然對數。Math.SQRT2返回 2 的平方根。
Math.ceil(number)
[edit | edit source]返回作為引數傳入的數字的最小整數。
const myInt = Math.ceil(90.8);
alert(myInt); // 91
alert(Math.ceil(-90.8)); // -90
Math.floor(number)
[edit | edit source]返回作為引數傳入的數字的最大整數。
const myInt = Math.floor(90.8);
alert(myInt); // 90
alert(Math.floor(-90.8)); // -91
Math.round(number)
[edit | edit source]返回作為引數傳入的數字的最接近的整數。
alert(Math.round( 90.8)); // 91
alert(Math.round(-90.8)); // -91
alert(Math.round( 90.3)); // 90
alert(Math.round(-90.3)); // -90
Math.max(number_1, number_2)
[edit | edit source]返回作為引數傳入的兩個數字中較大的一個。
alert(Math.max(8.3, 9)); // 9
alert(Math.max(8.3, -9)); // 8.3
Math.min(number_1, number_2)
[edit | edit source]返回作為引數傳入的兩個數字中較小的一個。
alert(Math.min(8.3, 9)); // 8.3
alert(Math.min(8.3, -9)); // -9
Math.random()
[edit | edit source]生成 0 到 1 之間的偽隨機數。
alert(Math.random());
Number.parseInt(string)
[edit | edit source]Number.parseFloat(string)
[edit | edit source]parseInt 和 parseFloat 這兩種方法將字串轉換為數字。它們從左到右掃描給定的字串。當它們識別出與 0-9 不同的字元時,它們將停止掃描並返回到目前為止讀取的已轉換數字(parseFloat 接受小數點)。如果第一個字元與 0-9 不同,它們將返回 Math.NaN,表示 非數字。
提示:在原始碼中不需要指定“Number.”。
const x = parseInt("7.5");
alert(x); // 7
const y = parseInt("Five");
alert(y); // NaN
const z = parseFloat("2.8") + 3;
alert(z); // 5.8
// scientific notation is accepted
alert(parseFloat("123.456e6")); // 123456000
BigInt
[edit | edit source]BigInt 是一種表示任意長度的整數的資料型別。因此,它是一種可變長度格式(概念上),僅受可用 RAM 的限制。
BigInt 的建立方式是在整數字面量末尾新增“n”,或使用 BigInt() 函式。
"use strict";
let hugeNumber_1 = 12345678901234567890n; // 'n' at the end
alert(typeof hugeNumber_1);
let hugeNumber_2 = BigInt("12345678901234567890"); // no 'n'
alert(typeof hugeNumber_2);
// a 'small' BigInt can be created out of an integer value
let hugeNumber_3 = BigInt(123);
alert(typeof hugeNumber_3);
BigInt 支援算術運算子 + - * / **、比較運算子和大多數位運算子。
布林值
[edit | edit source]布林變數可以包含兩個可能的值之一,true 或 false。JavaScript 使用布林 false、數字 0、NaN、空字串或內建型別 undefined 或 null 來表示 false。任何其他值都被視為 true。
"use strict";
let firstLoop = true;
alert(typeof firstLoop);
未定義
[edit | edit source]僅宣告但未初始化任何值的變數具有 undefined 資料型別。
"use strict";
let x;
// ...
alert(typeof x);
if (x == undefined) {
// remedy the missing initialization
x = 0;
}
alert(typeof x);
// ...
空值
[edit | edit source]在 JavaScript 中,null 被標記為原始值之一,因為它的行為看似原始。但是,使用 typeof 運算子時,它會返回“object”。這被認為是一個錯誤,但無法修復,因為它會破壞太多指令碼。[1]
符號
[edit | edit source]Symbol 代表一個唯一的識別符號。Symbol 可以有一個描述符,作為字串提供給它的建構函式。
"use strict";
// 'person' is a symbol with the description "Olaf"
const person = Symbol("Olaf");
alert(person.description); // Olaf
Symbols 用作物件中的鍵(嵌入在 [ ] 中)以保證唯一性。