#19 JS: Modified functions & how function create spaces

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


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


<<:  前端工程师也能开发全端网页:挑战 30 天用 React 加上 Firebase 打造社群网站|Day19 巢状路由

>>:  [Day04]稽核员守则与伦理

第28天:箭头函式与this()

在解析this的方式箭头函式与函式宣告不同,函式宣告是以呼叫时的方式来决定,而箭头函式在建立时就会决...

Logback 配置来客制化 Log 讯息吧

在 Spring boot 可以使用 Logback 进行配置,系统预设加载日志配置档案 logba...

每个人都该学的30个Python技巧|技巧 30:档案操作(字幕、衬乐、练习)

今天!就是今天!!这个系列终於要完结啦~(撒花)(狂撒) 最後一个要教的技巧就是关於档案操作,如果想...

Day 4【HTML + CSS】於是他开始像灵犬莱西一样到处蒐集证据

【前言】 不知道大家有没有看过 Youtube 上面一些 5~12 小时的 Coding 教学影片...