Javascript-the Good Parts Chapter 4

Source: Internet
Author: User

Why, every fault ' s condemn ' d ere it was done:
Mine were the very cipher of a function ...
-william Shakespeare, Measure for Measure

The best thing are about JavaScript are its implementation of functions. It got almost everything right. But, as a should expect with JavaScript, it didn ' t get everything right.

A function encloses a set of statements. Functions is the fundamental modular unit of JavaScript. They is used for code reuse, information hiding, and composition. Functions is used to specify the behavior of objects. Generally, the craft of programming is the factoring of a set of requirements to a set of functions and data structures.

Function Objects

Functions in JavaScript is objects. Objects is collections of name/value pairs have a hidden link to a prototype object. Objects produced from object literals is linked to Object.prototype. Function objects is linked to Function.prototype (which is itself linked to Object.prototype). Every function is also created with a additional hidden properties:the function ' s context and the code that implements The function ' s behavior.

every function object is also created with a prototype property. Its value was an object with a constructor property whose value is the function. This was distinct from the hidden link to function.prototype. The meaning of this convoluted construction would be a revealed in the next chapter.

Since functions is objects, they can be used as any other value. Functions can stored in variables, objects, and arrays. Functions can passed as arguments to Functions, and Functions can is returned from Functions. Also, since functions is objects, functions can has methods.

The thing that was special about functions was that they can be invoked.

Function Literal

function objects is created with function literals:

// Create A variable called Add and store a function // In it, adds, and numbers. var function (A, b) {    return a + b;};

A function literal had four parts. The first part is the reserved word function.
The optional second part is the function ' s name. The function can use it name to call itself recursively. The name can also is used by debuggers and development tools to identify the function. If a function is not given a name, as shown in the previous example, it's said to be anonymous.

The third part was the set of parameters of the function, wrapped in parentheses. Within the parentheses is a set of zero or more parameter names, separated by commas. These names is defined as variables in the function. Unlike ordinary variables, instead of being initialized to undefined, they'll be initialized to the arguments supplied W Hen the function is invoked.

The fourth part was a set of statements wrapped in curly braces. These statements is the body of the function. They is executed whe, the function is invoked.

A function literal can appear anywhere that an expression can appear. Functions can defined inside of other Functions. An inner function of course have access to its parameters and variables. An inner function also enjoys access to the parameters and variables of the functions it is nested within. The function object created by a function literal contains a link to that outer context. This is called closure. This is the source of enormous expressive power.

Invocation

Invoking a function suspends the execution of the current function, passing Control and parameters to the new func tion. In addition to the declared parameters, Every function receives, additional parameters:this and arguments. The This parameter are very important in object oriented programming, and their value is determined by the invocation PA Ttern. There is four patterns of invocation in Javascript:the method invocation pattern, the function invocation pattern, The constructor invocation pattern, and the Apply invocation pattern. The patterns differ in how the Bonus parameter this is initialized.

The invocation operator is a pair of parentheses, which follow any expression, that produces a function value. The parentheses can contain zero or more expressions, separated by commas. Each expression produces one argument value. Each of the argument values is assigned to the function ' s parameter names. There is no runtime error when the number of arguments and the number of parameters does not match. If There is too many argument values, the extra argument values would be ignored. If There is too few argument values, the undefined value is substituted for the missing values. There is no type checking on the argument values:any type of value can passed to any parameter.

The Method invocation Pattern

When a function was stored as a property of an object, we call it a method. When the A method is invoked, this is the bound to that object. If An invocation expression contains a refinement (which is, a. dot expression or [subscript] expression), it's invoked a S a method:

//Create MyObject. It has a value and an increment//method. The increment method takes an optional//parameter. If The argument is not a number and then 1//is used as the default.varMyObject ={value:0, Increment:function(inc) { This. Value + =typeofinc = = = ' Number '? Inc:1;    }};myobject.increment ();d Ocument.writeln (Myobject.value); //1Myobject.increment (2);d Ocument.writeln (Myobject.value); //3

Javascript-the Good Parts Chapter 4

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.