JS 基礎知識,解說使用變數在操作時,不同型別的行為差異(純值與物件 / 傳值與傳參考),以及 ’this‘ 的變化、 Class 宣告方式...等。
A brief description of JavaScript ,introducing basic usage like for loop / class / different this in global function or object function.
9. var a = 12
Primitive
Value
Primitive
Value
var b = a
0x001
0x002
Copy of
變數 與 記憶體位置
複製新的值:CALL BY VALUE
10. 變數 與 記憶體位置
var a = { a : 12, b : ”jack” }
var b = a
Object
0x0010x001
所有物件都是傳參參考呼叫
存入記憶體位置作為參參考:CALL BY REFENERCE
11. FUNCTION
function hello (name) {
return “hello, ”+name
}
let hello = function (){
return ”hello”+name
}
let hello = ( ) => {
return “hello”+name
} ES6
函式是指⼀一連串串程式碼的集合,在開發上有許多好處,
如把不同地⽅方重複的程式碼包裝起來來,把可能會改變的部分⽤用函
式的參參數表⽰示,以此來來減少重複造輪輪⼦子(重⼯工)的次數。
幾種宣告函式的⽅方式:
函式 ( Function )
12. FUNCTION
當⼀一個函式被呼叫時,JavaScript 會隨之產⽣生⼀一個 this 關鍵字,這個
關鍵字會根據不同呼叫⽅方式指向不同物件( this 的概念念離不開物件),
最常遇到的兩兩種情況如下:
this 關鍵字
WHAT’S THIS
D
function getName (name) {
console.log( this )
}
直接呼叫 : 指向 Window 物件
getName() // this is window
D
以物件呼叫 : 指向該function對應之物件
user.getName() // this is user
let user = {
name: “john”,
getName: function(){
console.log(this)
}
}
13. ⽤用new Class產⽣生物件(傳統)
CLASS 是物件的藍藍圖,當你把屬性設計好時,就可以便便利利的使⽤用同⼀一份
架構產⽣生許多新的物件(⽽而不是參參考)。
D
function User (name,age,state) {
this.name = name;
this.age = age;
this.unmarried = state
}
let jerry = new User(‘Jerry’ , 18 , false )
let stan = new User(‘Stan’ , 28 , false )
console.log(jerry.name;) // ‘Jerry’
Javascript 早期是⽤用 function 當作建構⼦子
(new 物件時的進入點)所以為了了避免跟
⼀一般 function 混淆,建議使⽤用⼤大寫開頭來來
作為類別名稱。
new Class 時 ,JS會做以下幾個特殊動作:
1. 會產⽣生⼀一個全新的空物件
2. 將該建構函式的 this 指向該物件(重要)
3. 執⾏行行建構式內容,並將該新物件回傳
所以記得⼀一定要加上new 關鍵字,否則該函式
會以⼀一般function 呼叫⽅方式執⾏行行,產⽣生錯誤。
CLASS
14. ECMAScript 6 中引入了了類別 (class) 作為 JavaScript 物件導向的語法
糖,提供⼀一個更更簡潔的語法來來建立物件。
⽤用new Class產⽣生物件(現代)
D
class User {
constructor (name,age,state){
this.name = name;
this.age = age;
this.married = state;
}
}
let jerry = new User(‘Jerry’ , 18 , false )
let stan = new User(‘Stan’ , 28 , false )
console.log(stan.married;) // false
與傳統 function 建構⼦子函式的內容相同
CLASS
ES6
15. 判斷式
If 與 switch
If ( a == true ) {
// do something
} else if ( b == true ){
// do something
} else {
// do something
}
常⾒見見的兩兩種判斷式寫法 let user = “Jerry”
switch (user){
case “Jerry” :
// do something
break;
case “Stan” :
// do something
break;
default :
// do something if none
of the above case match.
}
不會⾃自動跳脫,
必須以 break
結束條件區塊
16. 判斷式
比較運算⼦子( Comparison Operator )
比較運算⼦子可以比較其兩兩端的值,並將結果以布林林值回傳( true / false )
這邊介紹幾個比較常⽤用到或較讓⼈人匪夷所思的運算⼦子:
a == b
等於,如果 a 和 b 的值相等則為 true,
兩兩變數型別若若不⼀一樣,會被 JS ⾃自動轉型。
JavaScript 是⼀一個弱型別的語⾔言(宣告時沒有嚴格規定變數型別),因此
在接收變數時,如果變數型別跟預期的不同,會試圖把變數轉型。
⾃自動轉型
if ( “what?” )
等於,如果 a 和 b 的值相等則為 true,
兩兩變數型別若若不⼀一樣,會被 JS ⾃自動轉型。
轉型為 true
19. 迴圈
for ( let i = 0 ; i < 100 ; i = i +1) {
console.log(i)
// do something.
}
初始值
條件判斷
每次回圈會執⾏行行的動作
let i = 0
while ( i <= 10 ) {
console.log( i )
i = i+1
}
條件符合時執⾏行行
迴圈的概念念就是⼀一段程式碼會重複好幾次,次數則是由開發者所寫的邏
輯⽽而定,通常⽤用來來計算次數的變數會由 0 開始計算。
常⽤用的兩兩種迴圈
20. 字串串操作
let str = ‘Hello world !!’
str.length // 14
介紹常⽤用到的字串串⽅方法,基本上要⽤用到在查就好,不⽤用特別背。
字串串長度 str.length
let hello = ‘hello’
字串串串串接 strA.concat( strB )
let world = ‘ world’
hello.concat( world ) // ‘hello world’
let hi = ‘hello’
字串串分解 str.split( ‘’ )
hello.split( ‘’ ) // ‘[‘h’ , ’e’ , ’l’ , ’l’ , ’o’ ]’
let eng = ‘abcdefg’
eng.substr( 0,3 ) //abc
截取字串串 str.substr(start , stop)