- Mastering JavaScript Functional Programming
- Federico Kereki
- 230字
- 2021-07-02 22:41:13
Functions as objects
The concept of first-class objects means that functions can be created, assigned, changed, passed as parameters, or returned as result of yet other functions, in the very same way that you can do with, say, numbers or strings. Let's start with their definition. When you define a function in the usual way:
function xyzzy(...) { ... }
This is (almost) equivalent to writing:
var xyzzy = function(...) { ... }
Except for hoisting. JS moves all definitions to the top of the current scope, but not assignments; so, with the first definition you can invoke xyzzy(...) from any place in your code, but with the second you cannot invoke the function until the assignment has been executed.
See the parallel with the Colossal Cave Adventure Game? Invoking xyzzy(...) anywhere won't always work! And, if you never played that famous interactive fiction game, try it online -- for example, at http://www.web-adventures.org/cgi-bin/webfrotz?s=Adventure or http://www.amc.com/shows/halt-and-catch-fire/colossal-cave-adventure/landing.
The point we want to make, is that a function can be assigned to a variable -- and can also be reassigned, if desired. In a similar vein, we can define functions on the spot, when they are needed. We can even do this without naming them: as with common expressions, if used only once, then you don't need to name it or store it in a variable.
- Java多線程編程實戰指南:設計模式篇(第2版)
- JavaScript Unlocked
- Software Testing using Visual Studio 2012
- 程序員面試算法寶典
- 實戰Java程序設計
- iOS開發實戰:從零基礎到App Store上架
- RSpec Essentials
- Protocol-Oriented Programming with Swift
- Julia高性能科學計算(第2版)
- Solr Cookbook(Third Edition)
- Ext JS 4 Plugin and Extension Development
- After Effects CC案例設計與經典插件(視頻教學版)
- Web前端測試與集成:Jasmine/Selenium/Protractor/Jenkins的最佳實踐
- Java面向對象程序設計教程
- Flutter從0基礎到App上線