A study on the _javascript skills of JavaScript glimpse-form participation in real parameters

Source: Internet
Author: User
Tags eval function definition
Introduction:
Today I see someone else's topic:
Copy Code code as follows:

function fn (x) {
x = 10;
Arguments[0] = 20;
Console.log (X,arguments[0])
}
FN ()

Feel oneself to this also is a smattering of, oneself also can try, so on specially analysis.
Originally want to analyze from the language angle, helpless skill is not enough, can only superficial try, so call the glimpse, also look Daniel correct.
This is written yesterday, eat today and think for a while, think about to feel some problems or say not reliable, and then try to modify a bit.
Each JS entry book will mention, JS function inside there is a arguments object arguments, used to call the function when the actual parameters of the function, Fn.length save the length of the formal parameters.
These are useful for analysis, but I would like to get more information on the formal parameters, I do not know who has a better way, I temporarily have no solution.
So it can only simulate.
Ignore the simulation and start with the actual problem:
Copy Code code as follows:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en"
"Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title></title>
<body>
<script type= "Text/javascript" >
The formal parameter contains the invisible declaration var x = undefined
function fn (x) {
Console.log (X,arguments[0]);
Arguments[0] = 2;
Console.log (X,arguments[0]);
}
Console.log (' fn (): ');
FN ();
Undefined, undefined
Undefined, 2
Console.log (' fn (1): ');
fn (1);
1,1
2,2

Focus on the execution of the following two functions (fn_1,fn_2), where we directly re declare the parameters corresponding to x, see some people on the internet say this is a declaration of a local variable x.
Also, but this local variable is not a general local variable, x directly related to the corresponding arguments, the above example is the X Association arguments[0];
So I guess this assignment process is supposed to be
1. When the function is defined, the formal parameter is declared, and if the function body has a local variable of the same name, the declaration is ignored. At the same time, there will be an object arguments in the function body;
(In a random sentence: the individual thought that arguments did not define the number of groups of a consideration is because in the function definition cannot determine the actual parameters of the count [run-time Dynamic Determination], then either the array is infinitely large, or the array one value on the line out of bounds.
Back to the point:
For Fn_2, the initial Huafing parameter is equivalent to Var x; (x is not assigned, default is undefined, assignment is assigned at the time of statement execution)
So if you can write this, fn_2 should be like this:
Copy Code code as follows:

function fn_2 (var x) {
x = 3;
Console.log (X,arguments[0]);
Arguments[0] = 2;
Console.log (X,arguments[0]);
}

2, the function of the grammar detection through, the implementation of the time, the function of the arguments object at the beginning of the assignment, after the assignment, the function body statement began to execute.
The following statement is my own thinking, I do not know the correct incorrect (especially the associated statement):
Copy Code code as follows:

Once the parameter (the corresponding variable) is found to be assigned, the arguments is searched for the corresponding item, and if a arguments corresponding entry is found, the form participates in the association of the arguments counterpart. If no corresponding item (undefined) is found in the arguments, then the formal parameters and arguments remain independent. This is looking for a snapshot of arguments at the start of the function run. Conversely arguments assignment is the same.

The deletion of the above part is yesterday, the Red letter part is written to half of the time found that there are problems plus go. Today to return to God, why yesterday to silly think of the snapshot, this is not just when the function began to run directly
Judge the connection? So I changed the statement:
Copy Code code as follows:

Sets the associated information for the form participation arguments when the function starts executing. If the form participates in the corresponding arguments inside can find the corresponding item (all is undefined), then the two associations. No matter what you do, you will not change the information associated with the entire function body.

So the statement that follows is also going to change:
Back to the example, the Fn_2 function syntax detection is passed, starting with the second step:
With no parameters
Copy Code code as follows:

Fn_2 ();
function Fn_2 (x) {//arguments assignment completed, and the Arguments argument list is empty because there are no arguments. At the same time to judge the relevance of information, obviously formal parameters, arguments empty, both independent of each other, will not be linked later
var x = 3;//x Assignment is 3,x and Arguments[0] mutually independent, arguments[0] or undefined
Console.log (X,arguments[0]);//print x=3,arguments[0] is undefined
Arguments[0] = 2;//arguments is assigned, X and Arguments[0] are independent of each other. So x=3 does not change
Console.log (X,arguments[0]);//Print x = 3,arguments[0]=2
}

Case with parameters
Copy Code code as follows:

Case Fn_2 with parameters (1);
function Fn_2 (x) {//arguments assignment complete, arguments[0]=1. At the same time, parameter x has a value, which is related to each other and never knot together.
var x = 3;//x Assignment is 3,x associated with arguments[0], and Arguments[0] is assigned a value of 3.
Console.log (X,arguments[0]);//printing x=3,arguments[0] = 3
Arguments[0] = 2;//arguments[0] is assigned a value of 2, since X and Arguments[0] are associated together, so x changes at the same time
Console.log (X,arguments[0]);//Print x = 2,arguments[0]=2
}

The converse should also be the same:
With no parameters
Copy Code code as follows:

Fn_2 ();
function Fn_2 (x) {//not associated
Arguments[0] = 2;//cannot find the corresponding x (undefined), independent of each other
Console.log (X,arguments[0]);//undefined,2
x = 3;//are independent of each other, snapshots. Although arguments dynamically added, ways, so still failed
Console.log (X,arguments[0]);//3,2
}

With parameters
Copy Code code as follows:

Fn_2 (1);
function Fn_2 (x) {
ARGUMENTS[0] = 2;//Association
Console.log (X,arguments[0]);//2,2
x = 3;//Association
Console.log (X,arguments[0]);//3,3
}

Since we have only one formal parameter, it may not be persuasive enough, and now it is increased to two.
Case with only one argument:
Copy Code code as follows:

Fn_2 (1);
function Fn_2 (x,y) {//arguments assignment complete, arguments[0]=1,arguments[1]=undefined, so only X is associated with arguments[0], Y and arguments[1] Old and dead, no intercourse.
Console.log (X,y,arguments[0],arguments[1]); 1,undefined,1,undefined
var x = 3; The x assignment is 3,x associated with arguments[0], so arguments[0] is assigned a value of 3.
Console.log (X,y,arguments[0],arguments[1]); 3,undefined,3,undefined
var y = 4; Y assignment is 3,y and arguments[1] mutually independent, arguments[1] or for undefined
Console.log (X,y,arguments[0],arguments[1]); 3,4,3,undefined
Arguments[0] = 2; ARGUMENTS[0] is assigned a value of 2, since X and Arguments[0] are associated together, so x changes at the same time
Console.log (X,y,arguments[0],arguments[1]); 2,4,2,undefined
ARGUMENTS[1] = 5; ARGUMENTS[1] are assigned 5,y and Arguments[1] independent of each other, so y remains 4
Console.log (X,y,arguments[0],arguments[1]); X=2,y=4,arguments[0]=2,arguments[1]=5
}

There are two arguments:
Copy Code code as follows:

Fn_3 (1,6);
function Fn_3 (x,y) {//arguments assignment complete, arguments[0]=1,arguments[1]=6,x and Arguments[0],y and arguments[1] are interrelated
Console.log (X,y,arguments[0],arguments[1]); 1,6,1,6
var x = 3; The x assignment is 3,x associated with arguments[0], so arguments[0] is assigned a value of 3.
Console.log (X,y,arguments[0],arguments[1]); 3,6,3,6
var y = 4; Y assignment is 3,y associated with arguments[1], so arguments[1] is assigned a value of 4.
Console.log (X,y,arguments[0],arguments[1]); 3,4,3,4
Arguments[0] = 2; ARGUMENTS[0] is assigned a value of 2, since X and Arguments[0] are associated together, so x changes at the same time
Console.log (X,y,arguments[0],arguments[1]); 2,4,2,4
ARGUMENTS[1] = 5; ARGUMENTS[1] is assigned a value of 5, because Y and arguments[1] are already connected together, so y changes at the same time
Console.log (X,y,arguments[0],arguments[1]); X=2,y=5,arguments[0]=2,arguments[1]=5
}

All of the above is speculative, because there is no way to form the parameters of the information, so I wrote a small test by inference:
The following changes are also made:
Copy Code code as follows:

function _function () {//obtained parameter list is an array: _args
var _args = [];
for (var i = 0; i < arguments.length-1; i++) {
var obj = {};
obj[' key '] = Arguments[i];
Obj[arguments[i]] = undefined;
_args.push (obj);
}
This._argu = _args;
var fn_body = arguments[arguments.length-1];
The following method gets the argument _arguments, where _arguments is implemented as an array, not as a arguments object
This.exec = function () {
Argument _arguments is assigned when the function is run
var _arguments = [];
for (var i = 0; i < arguments.length; i++) {
_arguments[i] = arguments[i];
}
The following function body is executed
eval (fn_body);
}
}

Replace with:
Copy Code code as follows:

function _function () {//obtained parameter list is an array: _args
var _args = [];
for (var i = 0; i < arguments.length-1; i++) {
var obj = {};
obj[' key '] = Arguments[i];
Obj[arguments[i]] = undefined;
_args.push (obj);
}
This._argu = _args;
var fn_body = arguments[arguments.length-1];
The following method gets the argument _arguments, where _arguments is implemented as an array, not as a arguments object
This.exec = function () {
Argument _arguments is assigned when the function is run
var _arguments = [];
for (var i = 0; i < arguments.length; i++) {
_arguments[i] = arguments[i];
}
Determine the associated information at the start of the run
for (var j = 0; J < math.min (_arguments.length,_args.length); j + +) {
_args[j]["link"] = true;
}
The following function body is executed
eval (fn_body);
}
}

Logically, the association should be to point the two to the same object, but I just need to analyze the example, not intended to do so fine, so it is in the function body with the IF statement to judge.
Replace the fn_2 in the example with the corresponding form:
Copy Code code as follows:

function Fn_2 (x) {
var x = 3;
Console.log (X,arguments[0]);
Arguments[0] = 2;
Console.log (X,arguments[0]);
// }
Fn_2 (1)
In Fn_2body, use _args[i]["link" = true; to represent the form participation argument associated
var fn_2body = ' +
' _args[0][_args[0][' key ']] = 3; ' +
' If (_args[0]["link"]) {_arguments[0] = _args[0][_args[0]["key"];} ' +
' Console.log (_args[0][_args[0]["key"]],_arguments[0]); ' +
' _arguments[0] = 2; ' +
' If (_args[0]["link"]) {_args[0][_args[0]["key"] = _arguments[0]} ' +
' Console.log (_args[0][_args[0]["key"]],_arguments[0]);
var fn_2 = new _function (' x ', fn_2body);
Fn_2.exec (1);

Draw a diagram to show the relationship between the instance and the rewrite function, and change it by the way:

Back to the example at the beginning of the article:
Copy Code code as follows:

function fn (x) {
x = 10;
Arguments[0] = 20;
Console.log (X,arguments[0])
}
FN ()

Obviously, the two are independent of each other:
x = 10,arguments[0] = 20;
Speculate:
Copy Code code as follows:

function fn (x) {
x = 10;
Arguments[0] = 20;
Console.log (X,arguments[0])
}
FN (1)

It should all be output 20,20.
Copy Code code as follows:

function fn (x) {
Arguments[0] = 20;
Console.log (X,arguments[0])
}
FN (1)

should also be output 20,20
Copy Code code as follows:

function fn (x) {
Arguments[0] = 20;
Console.log (X,arguments[0])
}
FN ()

should be undefined and 20.
The original text comes from Cnblogs Xiao Xi Shan son

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.