前面有提到JS有分為"基本型別"及"物件型別",那Function函式即屬於物件的一種,
以下為四種常見的建立函式方式:
function functDeclaration () {};
var functExpression = function () {};
var namedFunctExpression = function myFunct() {};
var functConstructors = new Function ();
myName('Hsien'); // I am Hsien
function myName(name){
console.log('I am '+ name);
}
console.log(myName); // undefined (宣告提升)
myName('Hsien'); // myName is not a function ("函式的內容"不會被提升)
var myName = function(name){
console.log('I am ' + name);
}
var add = new Function('number', 'return number + number');
add(6) //12
資料來源:
https://blog.gtwang.org/programming/defining-javascript-functions/
https://miahsuwork.medium.com/%E7%AC%AC%E5%9B%9B%E9%80%B1-javascript-%E5%87%BD%E5%BC%8F-function-5c6114de5fff