A few topics on JavaScript functions _javascript Tips

Source: Internet
Author: User
Tags closure function definition
1. function objects and function pointers
A function is actually an executable object, and any way to access a function object is a function pointer.
No matter which way you use it
var a=new Function (...);
Function A (...) {...}
var a=function (...) {...}
The resulting function name A is a pointer to the functions that can be used with Var b=a to create another pointer to it.
But they point to the same function. Because the function name is a pointer, it can be passed as a parameter.
The () operator acting on a function pointer will execute the function it is in, and of course pass in the argument.


2. Function Direct quantity (function literal quantity)
Actually, the direct quantity of the function is not accurate, but it can be used to explain some usages
The "good" is a direct amount that can be used in code without using a variable name. There are similar uses
The function () {...}} represents the value of a functions pointer that points to function () {...} The defined function
So (function () {...}) () executes this function a=function () {...} A copy of this pointer is created.

3. Members of Function objects
function objects inherit from object objects and it has its own properties and methods.
That's all we got. Tips from vs2005
F.apply (Thisvalue,argarray)//The execution of parameters on the Thisvalue object is provided by the array Argarray
F.call (Thisvalue)//On the Thisvalue object, not without parameters, to use parameters directly follow the thisvalue behind
F.prototype; The prototype is very common and is not used much before.
f.length;//the length of the argument list

F.hasownproperty (PropertyName);//Inherits from Object method to check if object has specified property
F.propertyisenumerable (PropertyName);//Inherits from the object method to check whether the specified attribute can be enumerated for the in (except for the prototype other properties under Firefox)
F.tolocalestring ();//Inherits from Object method, converts to string
F.tostring ();//inherited from object method, converted to string automatically called when alert document.write, etc.
F.valueof ();//Inherit from object method, evaluate, object to do arithmetic operation automatically call, if the default is ToString

The range that 3.javascript functions can access
How can JavaScript functions access those resources? Includes three parts: the scope of the function, the this pointer, and the arguments otherwise, the function cannot access any resources.
If you want to have a clearer understanding of the function should imagine that the function has three sets of parameters:
The first group is that the scope is defined by the function definition position. Variables and functions that can be accessed at the function definition can also be accessed in the function, regardless of where the pointer to the function is passed
The second group is this, which is determined when the function is called
If a function is invoked with () it defaults to the object to which the function belongs: If the function is a member of an object, then this point points to the object, or to the global object (most of the time window)
If the function is pointing to the specified thisvalue with apply or call
The third group is that parameters that are determined in the argument list when the function is invoked can directly access parameters in the list of no longer arguments can be accessed using arguments

4. Function scope Generation and access
function generates a new scope (closure) in addition to being able to access a scope for each execution
Code outside this scope cannot access a variable of this scope the code within this scope can access this scope and the scope of the function definition.

Cross-scope access can only pass through pointers. The object in JS is the pointer type.
To get a pointer to a scope must pass the pointer from this scope: that is, to assign the value of a pointer type variable in this scope to a variable of another scope
The resources that are accessible in one function, including the This external scope parameter, can therefore be accessed in other scopes in this way.
An example of a very BT
<script>//Find a way to access a very deep nesting function object var o=new objects (); function A (obj) {var pointer; (function () {var p=new function () {function f () {var x=function () {alert ("Try to Vis It me! ")};/ /x is a pointer return x;//to pass the first layer as the returned value; This.x=f () ()///To pass the second layer with this; pointer=p.x;//A third layer is passed out with an outer scope}) () obj.x=pointer;//passes the fourth layer} A (o) with parameters; O.x (); </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

Finally this example is too BT, in fact, although the function of JS has such a capability, but in the use of our general will have a limit, is the closure of the package access to the external variables should not have "side effects" expression, such as + + 、--or assignment, That is, restricting the access of the closure to the external domain is limited to "read" and "Expression evaluation", you can modify the value of the object that the external variable actually refers to, but do not modify the value of the external variable itself within the closure. So the following example
Function C (a) {
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 logic of the program is complex. Because when the closure is called is completely uncertain. The closure makes this function completely different from the return value of each call, even if the same argument is used. This is clearly contrary to the functional principle of "formulation".

In fact, however, the above function has a special purpose for generating some special counters:)
So the restrictions 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.