iT邦幫忙

0

Day 22 (Js)

1.宣告a 賦值 null

var a = null

賦值:數字、"字串"、null、undefined... 皆不是 就是「另一變數」


2.indexOf()

, 要數個
? 可有可無
https://ithelp.ithome.com.tw/upload/images/20210625/20137684xNluQqQ6If.png


3.split()

https://www.w3schools.com/jsref/jsref_split.asp
string.split(separator, limit)
[]陣列


4.空白的定義

https://ithelp.ithome.com.tw/upload/images/20210625/20137684WnfwkOK3Br.png


5.偵錯、抓錯

console.log(變數); 放進去看看這到底是甚麼?
ex:console.log(errMag);

      var errMag = 1 / "apple";  //NaN
      console.log(errMag);       //是甚麼?

      var errMag = 1 / 0;     //Infinity
      console.log(errMag);     //是甚麼?

   console.log(typeof errMag); //甚麼型別?

6.有幾個? 想到>>length 


7.JavaScript by reference vs. by value

傳址 vs.傳值
區別實益:更改述職時會不會被取代(傳值x.傳址v)

number -> 傳值
Array -> 傳址
https://ithelp.ithome.com.tw/upload/images/20210625/201376845BpFtOzC3p.png
EX:

      var lsB = lsA;

取得 lsB 第一個位置的值

      console.log(lsB[0]); // 1,其實是lsA的1

修改 lsB 第一個位置的值

      lsB[0] = 999;
      console.log(lsB.toString()); // 999,2,3,4,5,6

將 lsA 列印出來查看
JavaScript by reference vs. by value(傳址) (傳值)
沒有lsA改變,lsA為什麼變了??? 因為「回傳址」非「回傳值」

      console.log(lsA.toString()); // 999,2,3,4,5,6

8.甚麼時候需要一個物件?

非常多的變數形容一個物體的時候
ex:人

      var Sid = 1;
      var SName = "Crash";
      var SHtml = 100;
      var SPhone = ["0910000000", "0412345678"];

ex:成績

      var apple = {
        //key1:value1
        chinese: 100,
        english: 90,
        math: 70,
      };

9. {}、[]、.差異

(1)var a = {物件}
中括弧裡面有字串,「前面」一定是個物件

ex:apple

      var apple = {     //物件 = 屬性 + 方法
        chinese: 100,   //key1:value1
        english: 90,
        math: 70,
      };

      console.log(apple["math"]);  //物件的找法
      console.log(apple);

(2)var a = [陣列]
中括弧裡面非字串,是數字

ex:poker

      var poker = [];
      poker[0] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13];
      console.log(poker[0].length); //13 因開0號櫃子 裡面有13個

(3) . 的
ex:蘋果的英文 apple.english

      console.log(apple.english); //90

10.四則運算前,維持符號左右「必須」都是數字

 
"字串" 的四則運算

      var apple = "11";
      var bee = "1";
      console.log(apple + bee); //1+11=111 黏起來XD
      console.log(apple - bee); //你就是數字啦= = 脫你"字串"衣服(藍色)
      console.log(apple * bee); //你就是數字啦= = 脫你"字串"衣服(藍色)
      console.log(apple / bee); //你就是數字啦= = 脫你"字串"衣服(藍色)

Ex: 關於"字串"的運算的陷阱

           a. "11" + 1    // 111(O)  12(X)
           b. "11" - 1    // 10(O)

11.讓比較值「兩個一組」,電腦才不會誤會

(1) 3 > 2 > 1

(1-1) 3 > 2 = true
(1-2) true =1 >1 false

      console.log(3 > 2 > 1); // false (==??) 

12.判斷式請單純!

計算在判斷式外面做
在外面算好在抓近來比較

      var apple = 10;
      var bee = 20;

      if (apple++ > 10 && bee-- < 20) {
        console.log(`A. apple:${apple}, bee:${bee}`); 

      } else {
        console.log(`B. apple:${apple}, bee:${bee}`);
      }
apple= 11
因為 && ,apple++ > 10 一定是fales,出來結果一定是fales,會進入B
bee = 20
所以就只算前面 ,後面就不繼續計算了(嫌麻煩逆XD?)

結論:判斷式在外面算好再進去比較

ex:if (apple > 10 && bee < 20)

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

尚未有邦友留言

立即登入留言