本篇章主要是我自己整理出來的 (大多數是爬了各網站、教學文章)
我把它當作學習筆記, 如果有也侵害著作權
請留言或是 E-mail: lioajimzen@gmail.com
資料參考來源: 1.你不可不知的 JavaScript 二三事#Day19:函數定義 (Function Definition) 的 100 種寫法 2.MDN_Web 3.Fooish_Javascript 4.JS-ES6箭頭函數 5.Arrow function expressions 6.六角學院課程
function 關鍵字來宣告一個函數functionNamefunction functionName(parameter1, parameter2, ...) {
statements
return value;
} function functionName() {
//只有陳述式
statements
}
// 輸出結果 :
undefined 函式名稱: squre
在squre函式裡面的算式= number*2 , 運算完成後會把該值 resultVal 作回傳。
let resultVal;
let number;
function square(number) {
resultVal=number * number;
return resultVal
}
console.log(square(10));
-----------------------------------
// 輸出結果
100 參考資料:
英文原文: The return statement ends function execution and specifies a value to be returned to the function caller.
函式回傳:
當你呼叫這個函式, 此函式的處理功能…. 接手過來處裡你的資料, 處理完之後把這個處理好的值丟到一個內定記憶體區間或式暫存區, 然後再把這個暫存值丟給”呼叫者”, 此時執行到return 陳述式時也代表此函式已經執行完畢!
function getRectArea(width, height) {
if (width > 0 && height > 0) {
return width * height;
}
return 0;
}
-----------------------------------
console.log(getRectArea(3, 4));
// expected output: 12
console.log(getRectArea(-3, 4));
// expected output: 0
return;
return true;
return false;
return x;
return x + y / 3; function 關鍵字作函數的宣告和定義。console.log(myFunc);
console.log(myFunc(3, 6));
function myFunc(a, b) {
return a + b;
}
-----------------------------------
// 執行結果
//function myFunc(a, b) {
return a + b;
}
//9 console.log(myFunc);
// console.log(myFunc(3, 6)); // TypeError: myFunc is not a function
let myFunc = function (a, b) {
return a + b;
};
console.log(myFunc);
console.log(myFunc(3, 6));
-----------------------------------
//執行結果
//undefined
//ƒ (a, b) {
return a + b;
}
//9 console.log(myFunc);
// console.log(myFunc(3, 6)); // TypeError: myFunc is not a function
var myFunc = function aaa(a, b) {
return a + b;
};
console.log(myFunc);
console.log(myFunc(3, 6));
// console.log(aaa); // ReferenceError: aaa is not defined
-----------------------------------
//執行結果
//undefined
//ƒ aaa(a, b) {
return a + b;
}
//9 說到建構子我們不得不提 new 這個Keyword,因為提到new 我們就要有個概念: 我們正在建立物件!
Function() 去定義函數內容,放到該變數裡。Function() 定義的函數自動被給予函數名稱 anonymous,但和「具名表達式」一樣,都無法直接透過該函數名稱呼叫。console.log(myFunc);
// console.log(myFunc(3, 6)); // TypeError: myFunc is not a function
var myFunc = new Function("a", "b", "return a + b");
console.log(myFunc);
console.log(myFunc(3, 6));
-----------------------------------
//執行結果 // undefined
// ƒ anonymous(a,b) {
return a + b
}
//9 JavaScript 最容易考的的題目, 箭頭函式與一般函式的差別!
參考資料: MDN_Web_箭頭函式 , 鐵人賽 (Arrow function) 卡斯柏
An arrow function expression is a compact alternative to a traditional function expression, but is limited and can’t be used in all situations.
箭頭函式 有著比一般函式表示法更精簡的表示法, 但箭頭函式並非一體適用….
箭頭函式也有它的極限啊!
this, arguments or super, and should not be used as methods.new.target keyword.call, apply and bind methods, which generally rely on establishing a scope.yield, within its body.底下範例碼參考: Arrow function js_github
可省略 function 這個關鍵字 !
FUNCTION(a){} 將改成 (a) =>{}
//ES5 版本
var circle = function(r) {
var PI = 3.14 ;
return r*r*PI
}
console.log(circle(2))
//執行結果 12.56
//ES6 版本
const circle = (r) => {
const PI = 3.14 ;
return r*r*PI
}
console.log(circle(2))
//執行結果 12.56
(參數1, 參數2, …, 參數N) => { 陳述式; }
(參數1, 參數2, …, 參數N) => 表達式; (參數1)=> { statements }
//若無參數,就一定要加括號:
() => { statements } // 用大括號將內容括起來,返回一個物件字面值表示法:
params => ({foo: bar})
// 支援其餘參數與預設參數
(param1, param2, ...rest) => { statements }
(param1 = defaultValue1, param2, …, paramN = defaultValueN) => {
statements }
// 也支援 parameter list 的解構
var f = ([a, b] = [1, 2], {x: c} = {x: a + b}) => a + b + c; f();
//執行結果 6 Arrow Function有兩個重要的特性:
1. 更簡潔的函式寫法
2. this 變數是全域性的。
//物件內的屬性們
var elements = [
'Hydrogen',
'Helium',
'Lithium',
'Beryllium'
];
// -----這段函式會輸出[8, 6, 7, 9]這個陣列
elements.map(function(element) {
return element.length;
});
// -----上方一般的函式,可以被改寫成下方的箭頭函式
elements.map((element) => {
return element.length;
});
//執行結果 [8, 6, 7, 9]
// -----如果輸入的參數只有一個,我們可以移除掉外面的括號
elements.map(element => {
return element.length;
});
//執行結果 [8, 6, 7, 9]
// -----當箭頭函式裡的內容只有'return'的時候,我們刪除return和外面的大括號
elements.map(element => element.length);
//執行結果 [8, 6, 7, 9]
// -----在這個範例中,因為我們只需要length這個屬性,所以也可以使用解構賦值:
// -----下方的'length'對應到我們想取得的屬性,而'lengthFooBArX'只是很普通的變數名稱,
// -----可以被任意修改成你想要的名字
elements.map(({ length: lengthFooBArX }) => lengthFooBArX);
// [8, 6, 7, 9]
// -----上面解構賦值之後的參數也可以被改寫為下面這樣。但要注意的是,在這個範例中,
// -----我們不是要指定'length'這個值給一個虛構的屬性,而是這個變數的名稱'length'本身就是
// -----用來當成我們想從物件上取得的屬性
elements.map(({ length }) => length);
//執行結果 [8, 6, 7, 9]
setTimeout(() => {
// -----100 milliseconds 之後執行
}, 100); setInterval(()=> {
// -----100 milliseconds 這期間一直處裡。
},100); 參考資料:
PjChener_Arrow Function_This, PJCHENder
那些沒告訴你的小細節this
解釋 JavaScript 中 this 的值?|ExplainThis
function User() {
this.name = 'Bob';
}
function AnotherUser() {
this.name = 'Hugo';
}
var user1 = new User();
var user2 = new AnotherUser();
console.log(user1.name);
console.log(user2.name);
//顯示結果
Bob
Hugo //-----一般函式
function User() {
this.name = "Bob";
return this;
}
console.log(this.name);
//-----箭頭函式
()=>{
this.name ="It is Arrow Functions";
}
console.log(this.name);
console.log(typeof this.name);
//顯示結果
(空物件)
It is Arrow Functions
string // Arrow function 1
()=>{
this.name ="It is Arrow Functions";
}
console.log(this.name);
console.log(typeof this.name);
//顯示結果
//It is Arrow Functions
//It is Arrow Functions
//string
- 箭頭函數(Arrow Functions) — this是定義時的對象, 不是在使用的物件。
- 傳統Function語法 — this是使用、呼叫(call)時的對象。
function Person() {
// Person() 建構式將 this 定義為它自己的一個實體
this.agegrowing = 0;
setInterval(function growingUp() {
// 在非嚴格模式stric mode下, growUp() 函式把 this 定義為全域物件
// (因為那是 growingUp()執行的所在),
// 與 Person() 建構式所定義的 this 有所不同
this.agegrowing++;
}, 3000);
}
// 在全域做另一個物件 p
var p = new Person();
console.log(p); function Person() {
var me = this;
me.age = 0;
setInterval(function growUp() {
// 這個 callback 參考 `self` 變數,為預期中的物件。
mee.age++;
}, 1000);
} 透過 bind 函式來綁定 this 變數到指定函式(上面為例,就是 growingUp() 函式)。
箭頭函式不會擁有自己的 this; 箭頭函式遵循 “常規變量查找規則“。
在當前範圍中搜索不到 this 變量時,他們最終會尋找其作用域。
由於 this 變數具有詞彙上綁定意義,故嚴格模式的宣告對 this 的作用將被忽略。
// Arrow function 1
()=>{
this.name ="It is Arrow Functions";
return this;
}
console.log(this.name);
// Arrow function 2
var ArrowObject =()=>{
this.name ="It is ArrowObject Functions";
}
ArrowObject();
console.log(this.name);
console.log(typeof this.name);
const obj = { // does not create a new scope
i: 10,
b: () => console.log(this.i, this),
c() {
console.log(this.i, this);
},
}
obj.b(); // prints undefined, Window { /* … */ } (or the global object)
obj.c(); // prints 10, Object { /* … */ } 'use strict';
const obj = { // does not create a new scope
i: 10,
b: () => console.log(this.i, this),
c() {
console.log(this.i, this);
},
}
obj.b(); // prints undefined, Window { /* … */ } (or the global object)
obj.c(); // prints 10, Object { /* … */ }
結語
有點想不太出來結語要寫甚麼, 光是要弄懂一般函式 vs 箭頭函式 + 蒐集資料就挺累人的
import 與 export 在Node.js 有提到其用法: Node.js require、module以及exports 模組設定 - Hugo Habor 每一個Javascript檔案可當作獨立模組系統(ES Module) 1. Module(模組)…
作者今天遇到一個很奇怪的問題 寫程式寫到一半... 點擊應用程式時... 怎麼都跳不出畫面... 後來查詢了一下是Reg註冊可能被微軟更新給搞掉了... 文章資訊來源: 【以解決】各位.exe檔案打不開,不要相信"在 Windows 7 或 Windows Vista 中無法打開 .EXE 檔案"這篇文章…
Node.js可以提供你很多模組,今天來探索如何使用 Node.js 的 http 模組來架設一個簡單的伺服器。 本篇使用模組require 載入你所需要的模組。這次我們使用'http' 模組來創造一個簡單的server。 資料參考 Node.js - createServer 起手式 - iT…
針對Node.js來談談 require、module以及exports 模組設定。這些概念允許開發者將大型程序分解成小的、可管理的、可重用的部分,稱為模塊。 下面將逐一介紹這些概念,以及它們如何與 JavaScript 關聯。 An Essential Guide to Node.js Modules (javascripttutorial.net) 1. Module(模組)…
像是很多專案必須讀取國家資料中心的metadata,許多檔案格式為CSV、JSON、Html可以利用網頁技術去爬蟲;拿一個範例來嘗試看看就知道這些語法的實際用途。 [主題週]專題報導-開放資料 (Open Data)相關議題與應用 (114230) - Cool3c 在這裡我們使用台北市資料大平台 UBike2.0 作為資料依據! 目的 來抓取空位數量 >15, 抓取空位數量 <15。…