`
leonzhx
  • 浏览: 786807 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Chapter 2. Functions

阅读更多

 

1.  The keyword function is always used when creating a new function. When it is followed by a variable name, the new function will be stored under this name. After the name comes a list of argument names and finally the body of the function.

 

2.  A return statement without an expression after it will cause the function to return undefined.

 

3.  Javascript looks up all function definitions, and stores the associated functions, before it starts executing the rest of the program. The nice thing about this is that we do not have to think about the order in which we define and use our functions—they are all allowed to call each other, regardless of which one is defined first.

 

4.  The variables created inside of them are local to the function. This “localness” of variables applies only to the arguments of the function and those variables that are declared with the var keyword inside the function. It is possible to access global (nonlocal) variables inside a function, as long as you haven’t declared a local variable with the same name.

 

5.  Functions defined inside other functions can refer to the local variables in their parent function, functions defined inside those inner functions can refer to variables in both their parent and their grandparent functions, and so on.

 

6.  All variables that were defined “above” a function’s definition are visible, which means both those in function bodies that enclose it and those at the top level of the program. This approach to variable visibility is called lexical scoping.

 

7.  Functions are the only things that create a new scope. A block of code (between braces) doesn’t produce a new local environment. You are allowed to use free-standing blocks.

 

8.  The names of defined functions can be used like normal variables, and their content can be passed around and used in bigger expressions.

 

9.  The “nameless” (or “anonymous”) function expression function(){return "B";} simply creates a function value. It is possible to specify arguments or multi-statement bodies in such definitions as well.

 

10.  Javascript preserves the local variable as long as it is in any way reachable. This feature is called closure, and a function that “closes over” some local variables is called a closure.

 

11.  JavaScript is notoriously nonstrict about the amount of arguments you pass to a function. If you pass too many, the extra ones are ignored. If you pass too few, the missing ones get the value undefined.

 

12.Math.round is a function that rounds a number.

 

13.The basic rule, which has been repeated by many programmers is to not worry about efficiency until your program is provably too slow. When it is, find out which parts are taking up the most time, and start exchanging elegance for efficiency in those parts.

 

分享到:
评论

相关推荐

    C.in.a.Nutshell.2nd.Edition.1491904755

    Chapter 2. Types Chapter 3. Literals Chapter 4. Type Conversions Chapter 5. Expressions and Operators Chapter 6. Statements Chapter 7. Functions Chapter 8. Arrays Chapter 9. Pointers Chapter 10. ...

    Get Programming with Go

    Chapter 2. Loops And Branches Chapter 3. Variable Scope Chapter 4. Capstone: Ticket To Mars Unit 2 - TYPES Chapter 1. Real Numbers Chapter 2. Whole Numbers Chapter 3. Big Numbers Chapter 4. ...

    [removed] Moving to ES2015 (AZW3格式)

    Chapter 2. Functions, Closures, And Modules Chapter 3. Data Structures And Manipulation Chapter 4. Object-Oriented Javascript Chapter 5. Javascript Patterns Chapter 6. Testing And Debugging Chapter 7....

    Learn.C++.Programming.Language.Become.A.Complete.C++.Programmer.pdf

    Chapter 2. A Tour of C++: The Basics Chapter 3. A Tour of C++: Abstraction Mechanisms Chapter 4. A Tour of C++: Containers and Algorithms Chapter 5. A Tour of C++: Concurrency and Utilities Part II: ...

    iOS 9 Programming Fundamentals with Swift (非epub转pdf)

    Chapter 2. Functions Chapter 3. Variables and Simple Types Chapter 4. Object Types Chapter 5. Flow Control and More Part II. IDE Chapter 6. Anatomy of an Xcode Project Chapter 7. Nib Management ...

    R.for.Data.Science.Visualize.Model

    Chapter 2. Workflow: Basics Chapter 3. Data Transformation with dplyr Chapter 4. Workflow: Scripts Chapter 5. Exploratory Data Analysis Chapter 6. Workflow: Projects Part II. Wrangle Chapter 7. ...

    JavaScript The Good Parts

    Chapter 2. Grammar Chapter 3. Objects Chapter 4. Functions Chapter 5. Inheritance Chapter 6. Arrays Chapter 7. Regular Expressions Chapter 8. Methods Chapter 9. Style Chapter 10 Beautiful Features ...

    Python.Essentials.1784390348

    Chapter 2. Simple Data Types Chapter 3. Expressions And Output Chapter 4. Variables, Assignment And Scoping Rules Chapter 5. Logic, Comparisons, And Conditions Chapter 6. More Complex Data Types ...

    R for Data Science Visualize Model Transform Tidy and Import Data.pdf

    Chapter 2. Workflow: Basics Chapter 3. Data Transformation with dplyr Chapter 4. Workflow: Scripts Chapter 5. Exploratory Data Analysis Chapter 6. Workflow: Projects Part II. Wrangle Chapter 7. ...

    Prentice.Hall.C++.for.Business.Programming.2nd.Edition.2005.chm

    Preface xv Changes From the First Edition xvi Chapter Overview xvii Part I: C++ Basics 1 ... Section B.2. Selection 803 Section B.3. Iteration 808 Section B.4. Combinations 812 Index

    Cplusplus.Language.Tutorial.For.Beginner.Learn.Cplusplus.in.7.days

    Chapter 2. Expressions Chapter 3. Statements Chapter 4. Functions Chapter 5. Arrays, Pointers, and References Chapter 6. Classes Chapter 7. Overloading Chapter 8. Derived Classes Chapter 9. Templates ...

Global site tag (gtag.js) - Google Analytics