Javascript麻瓜:callback 是什麼? For Javascript Muggle: What is callback Simply put: A callback is a function that is to be executed after another function has finished executing — hence the name ‘call back’. More complexly put: In JavaScript, functions are objects. Because of this, functions can take functions as arguments, and can be returned by other functions. Functions that do this are called higher-order functions . Any function that is passed as an argument is called a callback function . Example 1, function sayHello() { console.log('Hello everyone'); } setTimeout(sayHello(), 3000) function sayHello() is a function and it is passed as an argument of the function setTimeout, i.e. the function setTimeout takes the fucntion sayHello() as its argument. setTimeout is called a higher-order function. sayHello() is called a callback function. The callback function sayHello() is executed after 3000 miliseconds is past. Callbacks ...
發表文章
目前顯示的是 6月, 2019的文章
- 取得連結
- X
- 以電子郵件傳送
- 其他應用程式
Javascript麻瓜的練習:Parse URL Exercise for Javascript Muggle: Parse URL Create a function called parse_URL to parse the URL: https://example.com:4000/folder/page.html?x=y&a=b#section-2 //please refer the link to complete the exercise: https://developer.mozilla.org/zh-TW/docs/Web/API/URL/URL The function should print the following text to the console: source: https://example.com:4000/folder/page.html?x=y&a=b#section-2 protocol: https host: example.com port: 4000 query:x=y,a=b path: /folder/page.html hash: section-2 segments: [ '', 'folder', 'page.html' ] Sample codes ============================================================================= function parse_URL(myURL) { var a = new URL(myURL); urlObj= { source: myURL, host: a.hostname, ...