[Learning] #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

--

--

--

PM, Relationship, Travel, or anything quirky but interesting.|Subscribe & buy Vida a drink @LikerLand, the bottom of each article. 在文章底下LikerLand訂閱、請Vida喝一杯飲料 ❤

Love podcasts or audiobooks? Learn on the go with our new app.

Learn React With ME :)

Nunjucks with Gulp

Angular pluggable apps. Is it the right approach? - Part 1

Webpack 4 to webpack 5 migration

Level Up Your JavaScript With These 60 Quick Fixes for VS Code

Getting started with Angular 5

Selenium with JavaScript: How to run Automation Tests

Getting Started template project for NodeJS Express JS

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Vida Lin

Vida Lin

PM, Relationship, Travel, or anything quirky but interesting.|Subscribe & buy Vida a drink @LikerLand, the bottom of each article. 在文章底下LikerLand訂閱、請Vida喝一杯飲料 ❤

More from Medium

Rendering API Data to the DOM: Two Methods for Creating HTML Elements with JavaScript

How to Extract Metadata from Music Files with JavaScript using jsmediatags

Type Conversion and Coercion in javascript

How to Fix JavaScript Runtime issues — Lazy Loading