<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>類型和語法--2</title>
</head>
<body>
<script>
// 類數(shù)組
// var a = "foo";
// var b = ["f", "o", "o"];
// console.log(a.length); // 3
// console.log(b.length); // 3
// console.log(a.indexOf('o')); // 1
// console.log(b.indexOf('o')); // 1
// var c = a.concat('bar');
// var d = b.concat(["b", "a", "r"]);
// console.log(c); // foobar
// console.log(d); // ["f", "o", "o", "b", "a", "r"]
// console.log(a === c); // false
// console.log(b === d); // false
// console.log(a); // foo
// console.log(b); // ["f", "o", "o"]
// var e = a.charAt(1);
// console.log(e); // o
// c = a.toUpperCase(); // toUpperCase()將字符串轉(zhuǎn)換為大寫
// console.log(c); // FOO
// console.log(a); // foo
// b.push('!');
// console.log(b); // ["f", "o", "o", "!"]
// ----------------------------------------------------------------------------------------------
// 總結(jié):
// --------字符串原始值不會(huì)改變,會(huì)創(chuàng)建并返回一個(gè)新的字符串;
// --------數(shù)組的成員函數(shù)都是在原始值上直接進(jìn)行操作的;
// ----------------------------------------------------------------------------------------------
// ------- 處理字符串 -------
// 一:
// var c = Array.prototype.join.call(a, '-'); // join:將一個(gè)數(shù)組或者一個(gè)類數(shù)組對(duì)象的所有元素連成一個(gè)字符串并返回這個(gè)字符串
// var d = Array.prototype.map.call(a, (v) => {
// return v.toUpperCase() + '.'
// }).join('');
// console.log(c); // f-o-o
// console.log(d); // F.O.O.
// 二:字符串反轉(zhuǎn)
// b.reverse();
// console.log(b); // ["o","o","f"]
// var c = a.split("").reverse().join("");
// console.log(c); // oof
// ----------------------------------------------------------------------------------------------
// reverse:顛倒數(shù)組中元素的順序
// split:把一個(gè)字符串分割成字符串?dāng)?shù)組
// join:將一個(gè)數(shù)組或者一個(gè)類數(shù)組對(duì)象的所有元素連成一個(gè)字符串并返回這個(gè)字符串
// ----------------------------------------------------------------------------------------------
// ========================================================================================================
// number
// var a = 2E6;
// console.log(a); // 2000000
// console.log(a.toExponential()); // 2e+6
// var b = a * a;
// console.log(b.toExponential()); // 4e+12
// var c = 1 / a;
// console.log(c.toExponential()); // 5e-7 - 0.0000005
// ----------------------------------------------------------------------------------------------
// toExponential:把對(duì)象的值轉(zhuǎn)換成指數(shù)計(jì)數(shù)法;
// ----------------------------------------------------------------------------------------------
// var a = 43.59;
// console.log(a.toFixed(0)); // 44
// console.log(a.toFixed(1)); // 43.6
// console.log(a.toFixed(2)); // 43.59
// console.log(a.toFixed(3)); // 43.590
// console.log(a.toFixed(4)); // 43.5900
// ----------------------------------------------------------------------------------------------
// toFixed:指定小數(shù)部分的顯示位數(shù);
// ----------------------------------------------------------------------------------------------
// console.log(a.toPrecision(1)); // 4e+1
// console.log(a.toPrecision(2)); // 44
// console.log(a.toPrecision(3)); // 43.6
// console.log(a.toPrecision(4)); // 43.59
// console.log(a.toPrecision(5)); // 43.590
// console.log(a.toPrecision(6)); // 43.5900
// ----------------------------------------------------------------------------------------------
// toPrecision:指定有效數(shù)位的顯示位數(shù);
// ----------------------------------------------------------------------------------------------
// 無效語法:
// console.log(42.toFixed( 3 )); // SyntaxError
// 下面的語法都有效:
// console.log((42).toFixed( 3 )); // "42.000"
// console.log(0.42.toFixed( 3 )); // "0.420"
// console.log(42..toFixed( 3 )); // "42.000"
// 42.. 第一個(gè) . 被視為 number 的一部分,第二個(gè) . 是屬性訪問 運(yùn)算符。
// console.log(42 .toFixed(3)); // "42.000" 注意空格
// console.log(0xf3); // 243
// console.log(0o363); // 243的八進(jìn)制
// console.log(0O363); // 同上
// console.log(0b11110011); // 243的二進(jìn)制
// console.log(0B11110011); // 同上
// ========================================================================================================
// 解決 0.1 + 0.2 != 0.3的問題
// function numbersClose (n1, n2) {
// return Math.abs(n1 - n2) < Number.EPSILON;
// };
// var a = 0.1 + 0.2;
// var b = 0.3;
// console.log(numbersClose(a, b)); // true
// console.log(numbersClose( 0.0000001, 0.0000002 )); // false
// document.write(Math.abs(7.25) + "<br />")
// document.write(Math.abs(-7.25) + "<br />")
// document.write(Math.abs(7.25-10))
// document.write(Number.isInteger(42) + "<br />"); // true
// document.write(Number.isInteger(42.3)); // false
// document.write(Number.isSafeInteger(Math.pow(2, 53)) + "<br />"); // false
// document.write(Number.isSafeInteger(Math.pow(2, 53) - 1)); // true
// document.write(Math.pow(2, 5));
// ----------------------------------------------------------------------------------------------
// Math.abs:返回?cái)?shù)的絕對(duì)值
// Number.EPSILON:表示 1 與大于 1 的最小浮點(diǎn)數(shù)之間的差。
// Number.isInteger:檢測(cè)一個(gè)值是否為整數(shù)
// Number.isSafeInteger:檢測(cè)一個(gè)值是否是安全的整數(shù)
// ----------------------------------------------------------------------------------------------
// ========================================================================================================
// 特殊的數(shù)字
// if (!Number.isNaN) {
// Number.isNaN ((n) => {
// return typeof n === 'number' && window.isNaN(n);
// // return n !== n;
// });
// };
// var a = 2 / 'foo';
// var b = 'foo';
// console.log(Number.isNaN(a)); // true
// console.log(Number.isNaN(b)); // false
// ----------------------------------------------------------------------------------------------
// isNaN(...):判斷一個(gè)值是否是 NaN。缺陷:判斷一個(gè)值是否不是 NaN,也不是數(shù)字
// Number.isNaN(...):判斷一個(gè)值是否是 NaN。 --->推薦使用
// NaN 是 JavaScript 中唯 一一個(gè)不等于自身的值。
// ----------------------------------------------------------------------------------------------
// ========================================================================================================
// 無窮數(shù)
// var a = 1 / 0;
// console.log(a); // Infinity
// var b = Number.MAX_VALUE; // 1.7976931348623157e+308
// var c = Number.MIN_VALUE; // 5e-324
// console.log(b, c);
// ========================================================================================================
// var arr = [1,2,3, { a : 1 } ];
// console.log(JSON.stringify(arr)); // [1,2,3,{"a":1}]
// var jsonStr = '[1,2,3,{"a":1}]';
// console.log(JSON.parse(jsonStr)); // [1, 2, 3, {"a":1}]
// var a = 0 / -3;
// console.log(JSON.stringify( a )); // "0"
// console.log(JSON.parse( "-0" )); // "-0"
// -0 == 0; // true
// JSON.stringify(-0) 返回 "0",而 JSON.parse("-0") 返回 -0。
function isNegZero(n) {
n = Number( n );
return (n === 0) && (1 / n === -Infinity);
}
console.log(isNegZero( -0 )); // true
console.log(isNegZero( 0 / -3 )); // true
console.log(isNegZero( 0 )); // false
// ----------------------------------------------------------------------------------------------
// JSON.stringify(...):將數(shù)組轉(zhuǎn)換為字符串。
// JSON.parse(...):解析字符串轉(zhuǎn)換為數(shù)組
// ----------------------------------------------------------------------------------------------
// ========================================================================================================
// var a = 2 / "foo";
// var b = -3 * 0;
// Object.is( a, NaN ); // true
// Object.is( b, -0 ); // true
// Object.is( b, 0 ); // false
if (!Object.is) {
Object.is = function(v1, v2) {
// 判斷是否是-0
if (v1 === 0 && v2 === 0) {
return 1 / v1 === 1 / v2;
}
// 判斷是否是NaN
if (v1 !== v1) {
return v2 !== v2;
}
// 其他情況
return v1 === v2;
};
}
// ----------------------------------------------------------------------------------------------
// Object.is(...):判斷兩個(gè)值是否絕對(duì)相等。主要用來處理那些特殊的相等比較。
// ----------------------------------------------------------------------------------------------
// 小結(jié) ========================================================================================================
// JavaScript 中的數(shù)組是通過數(shù)字索引的一組任意類型的值。
// 字符串和數(shù)組類似,但是它們的 行為特征不同,在將字符作為數(shù)組來處理時(shí)需要特別小心。
// JavaScript 中的數(shù)字包括“整 數(shù)”和“浮點(diǎn)型”。
// 基本類型中定義了幾個(gè)特殊的值。
// null 類型只有一個(gè)值 null,undefined 類型也只有一個(gè)值 undefined。
// 所有變量在賦值之 前默認(rèn)值都是 undefined。
// void 運(yùn)算符返回 undefined。
// 數(shù)字類型有幾個(gè)特殊值,包括NaN( 意指“not a number”, 更確切地說是“invalid number”)、 +Infinity、-Infinity和 -0。
// 簡(jiǎn)單標(biāo)量基本類型值(字符串和數(shù)字等)通過值復(fù)制來賦值 / 傳遞,而復(fù)合值(對(duì)象等) 通過引用復(fù)制來賦值 / 傳遞。
// JavaScript 中的引用和其他語言中的引用 / 指針不同,它們不 能指向別的變量 / 引用,只能指向值
// 小結(jié) ========================================================================================================
</script>
</body>
</html>
類型和語法--類型、值
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
相關(guān)閱讀更多精彩內(nèi)容
- 這是16年5月份編輯的一份比較雜亂適合自己觀看的學(xué)習(xí)記錄文檔,今天18年5月份再次想寫文章,發(fā)現(xiàn)簡(jiǎn)書還為我保存起的...
- 3.1 語法 對(duì)象的文字語法: 構(gòu)造形式: 3.2 類型 在 JavaScript 中一共有六種主要類型:stri...
- 值類型轉(zhuǎn)換將值從一種類型轉(zhuǎn)換為另一種類型通常稱為類型轉(zhuǎn)換,這是顯示的情況;隱式的情況稱為強(qiáng)制類型轉(zhuǎn)換。JavaSc...
- 國(guó)家電網(wǎng)公司企業(yè)標(biāo)準(zhǔn)(Q/GDW)- 面向?qū)ο蟮挠秒娦畔?shù)據(jù)交換協(xié)議 - 報(bào)批稿:20170802 前言: 排版 ...