About javascript Functions _ javascript skills

Source: Internet
Author: User
About javascript Functions 1. Function objects and function pointers
A function is actually an executable object. Any way to access a function object is a function pointer.
No matter which method is used
Var a = new Function (...);
Function (...){...}
Var a = function (...){...}
The obtained function name a is a pointer to the function. You can use var B = a; to create another pointer to it.
However, they point to the same function. Because the function name is a pointer, it can be passed as a parameter.
() When an operator acts on a function pointer, it will execute the function where it is located, and of course it will pass in parameters.


2. Function direct volume (function text volume)
In fact, this argument is not accurate but can be explained in plain terms.
125 "good" is a direct amount. You can use a function without using the variable name in the code.
Function () {...} indicates the value of a function pointer. It points to the function defined by function () {...}.
Then (function () {...}) () executes this function a = function () {...} and creates a copy of this pointer.

3. function object Member
Function objects inherit from object objects. They also have their own attributes and methods.
There are so many prompts from vs2005.
F. apply (thisValue, argArray); // The execution parameters on the thisValue object are provided by the array argArray.
F. call (thisValue); // It is executed on the thisValue object. If it is not a parameter, it is followed directly by the thisValue.
F. prototype; // prototype is common. We have not mentioned it much before.
F. length; // the length of the parameter list

F. hasOwnProperty (propertyName); // The method inherited from the Object to check whether the Object has a specified attribute.
F. propertyIsEnumerable (propertyName); // The method inherited from the Object. Check whether the specified property can be enumerated by for in (except for prototype in firefox)
F. toLocaleString (); // The method inherited from the Object and converted to a string.
F. toString (); // The method inherited from the Object, which is automatically called when being converted to a string such as alert document. write.
F. valueOf (); // The method inherited from the Object. The value is automatically called when the Object is used for arithmetic operations. If the default value is toString

3. Accessible scope of javascript Functions
Which resources can javascript Functions access? This includes three parts: the function scope, this pointer, and parameters. In addition, the function cannot access any resources.
To clearly understand the function, you should imagine that the function has three sets of parameters:
The first group is the variables and functions that can be accessed by the external scope in the function definition, no matter where the pointer that calls this function is passed
The second group is determined by this when the function is called.
If a function is called with (), it points to the object to which the function belongs by default: if the function is a member of an object, this points to this object; otherwise, it points to the Global Object (most of the time window)
If the function uses apply or call to call this to point to the specified thisvalue
The third group is that when the function is called, the parameters in the parameter list can be directly accessed. Parameters in the no-parameter list can be accessed using arguments.

4. Function scope generation and access
In addition to being able to access a scope, a new scope (closure) will be generated each time a function is executed)
Code out of this scope cannot access the variables in this scope. Code in this scope can access this scope and the scope of this function definition.

Cross-scope access can only pass through pointers. In js, objects are pointer types.
To obtain a pointer pointing to a specific scope, the pointer must be passed out of this scope: that is, the value of the pointer type variable in this scope must be assigned to the variables in other scopes.
The resources that a function can access include the out-of-scope parameters of this, so you can access variables in other scopes in this way.
A very bt example

Script // find a way to access a very deep nested function Object var o = new Object (); function a (obj) {var pointer; (function () {var p = new function () {function f () {var x = function () {alert ("Try to visit me! ")}; // X is a pointer return x; // It is passed as the return value to the first layer;} this. x = f (); // use this to pass the second layer;} pointer = p. x; // pass the third layer} using the external scope) () obj. x = pointer; // The Layer 4} a (o); o. x (); script
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]


The final example is too BT. In fact, although js functions have such capabilities, we usually have a limit when accessing the external variables of the closure in the closure, expressions with "Side effects", such as ++, --, or value assignment, should not be used. That is to say, the content that restricts closures to access external domains is limited to "read" and "expression calculation ", you can modify the value of the object actually referenced by the external variable, but do not modify the value of the external variable itself in the closure. So the following example
Function c (){
Return function (x)
{
Return x + a ++;
}
}
It is usually legal but unreasonable because it is a closure with side effects. This can easily lead to confusion when the program logic is complex. This is because the time when the closure is called is completely unknown. The existence of this closure makes the return value of this function every call completely different, even if the same parameter is used. This clearly violates the basic principle of functional "formula.

However, in fact, the above function has a special purpose to generate some special counters :)
So the limitations are not absolute...
Related Article

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.