iT邦幫忙

2021 iThome 鐵人賽

DAY 19
0
Modern Web

Learn & Play JavaScript -- Entry-Level Front-End Web Development系列 第 19

#19 JS: Modified functions & how function create spaces

  • 分享至 

  • xImage
  •  

Modified functions(變形函式)

Before introducing modified function, let's recall our memory about the "basic function":

function add(n1,n2) {
    alert(n1+n2);

And this is how a modified function look like:

var add=function (n1,n2) { 
    alert(n1+n2);
}
add(1,100); // → call the function 
var test=add; // → didn't call the function, so it purely input the data into the variable above.
test(2,200);

The meanings behind these two functions are actually the same, but the modified one input the function into the variable. We can say that function is a kind of data as well.


Function block creates space

  • With function block, it creates a new space for coding.
  • 2 spaces: global variable(全域變數 or 全域空間) and local variable(區域變數 or 區域空間)
var x=13; // → global variable
function test() { // → local variable
    var y=7;
    var x=1;
    alert(x+y);
}
alert(x);

External code can not use the local variable, but local variable can use global variable.
For example, if we don't declare the x variable at local variable, var x=13 will still be operated right away.

var x=13;
function test() { 
    var y=7;
    alert(x+y);
}
alert(x);

Music of Today: Hymn For The Weekend by Coldplay

Yes


Like/Share/Follow

Feel free to comment and share your ideas below to learn together!
If you guys find this article helpful, please kindly do the writer a favor — LIKE this article./images/emoticon/emoticon12.gif


上一篇
#18 JS: Intro to function
下一篇
#20 JS: Object Fundamentals
系列文
Learn & Play JavaScript -- Entry-Level Front-End Web Development30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言