iT邦幫忙

2

[ JS個人筆記 ] 各種討人厭的運算&轉型案例—DAY2

  • 分享至 

  • xImage
  •  

特殊的數字

Infinity 系列:

Infinity + Infinity      // Infinity

-Infinity + -Infinity    // -Infinity

-Infinity + Infinity     // NaN

Infinity - Infinity      // NaN

-Infinity - -Infinity    // NaN

-Infinity - Infinity     // -Infinity

Infinity - -Infinity     // Infinity

// ------------------- 乘 除 取餘 -----------------------

Infinity * Infinity       // Infinity

Infinity / Infinity       // NaN

Infinity % Infinity       // NaN

Infinity % -Infinity      // NaN

// -------------- 0 & Infinity 系列 ----------------

Infinity * 0               // NaN

Infinity / 0              // Infinity

Infinity % 0              // NaN

0 % Infinity              // 0

// -------------- 常數 & Infinity 系列 ---------------

Infinity * 100             // Infinity

Infinity / 100             // Infinity

Infinity % 100            // NaN

100 % Infinity            // 100

-------------------------------------------------

NaN 系列:

其中一個是 NaN,那麼結果就必定是 NaN

10 + NaN            // NaN

Infinity + NaN      // NaN

-Infinity + NaN     // NaN

情況簡單判別

當加號 + 兩側的其中一方是字串

當加號 + 兩側的其中一方是字串的情況下,加號 + 會將兩者都視為「字串」連接在一起。

10 + 'abc'          // "10abc"

true + 'abc'        // "trueabc"

"abc" + undefined   // "abcundefined"

"123" + null        // "123null"
var num1 = 10;
var num2 = 100;

var str = "10 加 100 的數字會是" + num1 + num2;

當 減(-) 乘(*) 除(/) 取餘數(%) 兩側的其中一方是數字

基本型別:

若其中一方屬於基本型別(string、boolean、undefined、null)且不是數字的情況,那麼 JavaScript 會在先在背後透過 Number() 嘗試將數值轉為「數字」,再進行運算。

  • true 轉型成數字後,會變成 1
  • false 轉型成數字後,會變成 0
  • null 轉型成數字後,會變成 0
  • undefined 轉型成數字後,會變成 NaN

當被除數為0時

  • 除數為正(+),結果為 infinity
  • 除數為負(-),結果為 -infinity
  • 除數為0,結果為NaN
100 - "50"        // 50

100 - "abc"       // NaN

100 - false       // 100

100 - true        // 99

100 - undefined   // NaN

100 - null        // 100

100 * "10"      // 1000

100 * abc       // NaN

100 * true      // 100

100 * false     // 0

100 * {}        // NaN

物件型別

- 會先透過物件的 valueOf() 方法
- 如果物件沒有 valueOf() 方法的話,則是透過 toString() 先轉成「字串」後,再以 Number() 嘗試將數值轉為「數字」後進行運算。
// 簡單物件
100 - { }         // NaN

// 自訂物件,透過 Object.prototype.valueOf 來指定物件的 value

function Obj(number) {
  this.num = number;
};

Obj.prototype.valueOf = function(){
  return this.num;
};

var o = new Obj(50);

// 因為 o.valueOf() 的值為 50
100 - o         // 50

奇怪的判別

兩個等號 == 會自動將資料轉型

true == 'true'      // false

false == 'false'    // false
false == 0    // true
true == 1     // true

[] == []      // false
[] == ![]     // true

[] == ''      // true
[] == 0       // true

[''] == ''    // true
[0] == 0      // true

[0] == ''     // false
[''] == 0     // true
null == undefined   // true

[null] == ''        // true
[null] == 0         // true

[undefined] == ''   // true
[undefined] == 0    // true

練習題

1.以下變數 a, b, c, d, e, f 它們的值、型別各自為何?

let a = 1 +"2"+ 3 ;
let b = "1" * "1";
let c = 2 - "1";
let d = 1 > 2;
let e = 2 > 1;
let f = "我好棒" - 1;

Ans:
a = '123'、String;
b = 1、Number;
c = 1、Number;
d = false、boolean;
e = true、boolean;
f = NaN、Number;

2.分別印出甚麼答案

console.log(1 + "2" + "2")
console.log(1 - "1" + "2") 
console.log("1" + "1" + "2") 
console.log("A" - "B" + "2")
console.log("A" - "B" + 2) 

Ans:122、02、112、NaN2、 NaN

3.進階題,一元運算子


let a = 5
console.log( --a + "2" + "2") 
console.log( a-- * "2" + "2") 
console.log( --a * NaN + "2") 
console.log(Infinity / --a + "2") 
console.log(Infinity * 0 + "--a" ) 

Ans:422、102、NaN2、Infinity2、 NaN--a


圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言