JavaScript Language Essence Reading notes-JavaScript functions

Source: Internet
Author: User

JavaScript is a C-language cloak of Lisp, in addition to lexical similarities with the C- family language , there are almost no similarities.

JavaScript functions:
The function contains a set of statements, which are the basic module units of JavaScript for code reuse, information hiding, and combination invocation.
function is used to specify the behavior of the object.

Function Object Functions

In JavaScript, a function is an object. The object is a collection of key-value pairs and has a hidden connection to the prototype object.
Object literals are generated by objects connected to the Object.prototype. The function object is connected to the Function.prototype (the prototype object itself is connected to Object.prototype).
Each function has two additional properties at the time of creation: the context of the function and the code that implements the function's behavior.
Each function object is created with a prototype property, and its value is an object that has the constructor property and the value is the function.

functions literal function Literal
var add = function (A, b) {return a + B;};
A function object created from a function literal contains a connection to the external context, which is called a closure.

Call Invocation
Calling a function pauses the execution of the current function, passing control and parameters to the new function. In addition to the formal parameters defined at the time of declaration, each function receives two additional attributes: this and arguments.
The parameter this is very important in object-oriented programming, and its value depends on the mode of the call. JavaScript has a total of 4 call Patterns: The method invocation pattern, the function invocation pattern, the constructor invocation pattern, and the Apply invocation pattern. These patterns differ on how to initialize the key parameter this.
call operator(): A pair of parentheses following any expression that produces a function value. When the number of actual parameters (arguments) does not match the number of formal parameters (parameters), a run-time error is not caused.

Method Invocation Pattern: The Method invocation Pattern
When a function is saved as a property of an object, we call it a method. When a method is called, this is bound to the object. If a call expression contains a property access expression (that is, a. Sign expression or [subscript] subscript expression), it is invoked as a method.
Method can use this to access an object, so it can take value from the object or modify the object. This binding to the object occurs at the time of the call. This "super" late binding (very late binding) allows the function to be reused for this height. The method through this to obtain the context of the object they belong to is called a public method.

function Call Pattern: The Function invocation Pattern
When a function is not a property of an object, it is called as a function.
var sum = Add (3,4);
This is bound to the global object when the function is called in this mode.
Add a double method to MyObject (above MyObject object already has property value and method Increasement)
myobject.double = function () {
var = this;
var helper = function () {
That.value = Add (That.value, that.value);
};
The Function invocation Pattern
Helper ();
}
The Method invocation Pattern
Myobject.double ();
Document.writeln (Myobject.getvalue ());

constructor Invocation Pattern: The Constructor invocation Pattern
JavaScript is a prophecy based on the inheritance of prototypes. This means that an object can inherit properties directly from other objects. The language is non-categorical. This deviates from the mainstream of today's programming. Most languages today are class-based languages. Although archetypal inheritance has a strong expressiveness, he is not widely understood. JavaScript itself lacks confidence in the nature of its prototypes, so it provides a set of object building grammars similar to class-based languages.
If you call with new in front of a function, you will create a new object that hides the prototype member connected to the function, and this will be bound to the new object.
Functions called with the new prefix are called constructor functions. By convention, they are kept in a variable named in uppercase.

Apply call pattern: The Apply invocation pattern
Because JavaScript is a functional object-oriented programming language, functions can have methods.
The Apply method lets us construct a parameter array and use it to invoke the function. It also allows us to select the value of this.
The Apply method receives two parameters. The first parameter is bound to this. The second is an array of arguments.

parameter Arguments
When the function is called, a "free" variable arguments array is given. It provides access to all of the argument lists that are passed to it when it is called, including the extra arguments that are not defined when the function declaration is assigned to the form parameter. This makes it possible to write a function that does not require parameters to be specified.
Arguments is an array-like (Array-like) object, and he is not really an array. There is a length property, but there is no other method for the array.

Back to return
A function always returns a value and returns undefined if no return value is specified.
If the function is called with a method prefixed with the new prefix and the return value is not an object, then this is returned (the new object).

Exception Exceptions: Try Catch throw

adding methods to types
JavaScript allows you to add methods to the basic type of language (via prototype).
Function.prototype.method = function (name, method) {if (This.prototype[name]) {This.prototype[name] = method; return This;}};
By adding a method to the Function.prototype object, we do not have to type the prototype property name.
Number.method (' Integer ', function () {return Math[this < 0? ' Ceiling ': ' floor ' [this];});
String.metho (' Trim ', function () {return this.replace ('/^\s+|\s+$/g ', ');});

By adding methods to basic types, we can greatly improve the expressive force of language. Because JavaScript is the dynamic nature of prototype inheritance (when a property accessor is called, a layer of checks), the new method is immediately assigned to all values (objects), even if the value (object) is created before the method is created.
Another thing to note is that the for in statement behaves poorly on the prototype. You can use the hasOwnProperty method to filter out inherited properties.

recursive recursion: Hanoi Puzzles

Scope Scope
In programming languages, scopes control the visibility and life cycle of variables and parameters. This is an important help for programmers because it reduces name collisions and provides automatic memory management.

Closed bag closure
The benefit of scopes is that intrinsic functions can access parameters and variables defined outside of them (except this and arguments).

Callback Callbacks
Modular module:
We can use functions and closures to construct modules. A module is a function or object that provides an interface but hides state and implementation. By using functions to generate modules, we can almost completely abandon the use of global variables to mitigate the effects of one of the worst features of this JavaScript.
The general form of a module: a function that defines private variables and functions, and the use of closures to create privileged functions that can access private variables and functions. Finally, return to the privileged function, or save them to a place where you can access them.

Cascade Cascade
There are some methods that do not return values. If we let these methods return this instead of undefined, we can start cascading (example: chained invocation of jquery).

Apply Curry
Memory Memoization
Functions can use objects to memorize previous results, thus avoiding unnecessary operations. This optimization is called memory.

JavaScript Language Essence Reading notes-JavaScript functions

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.