[Seek the path to a javascript guru 01] What are the Javascript parameters?

Source: Internet
Author: User
Preface

I have been thinking about a problem recently. I have accumulated a lot of things in my own daily lives. I can't escape my blog (of course, it won't work in the advanced stage ), but when I was asked, I couldn't answer it.

Knowing and answering, the answer is not very effective .... This is a very embarrassing thing. In addition, it is reasonable to say that the Alibaba interview is finished. I will take a look at the job later. It is also good to have a rest.

In addition to memory accidents, mentality is also a problem. For example, the most common sentence is:

Well, you said this, I know. I also learned it and wrote a demo, but I forgot it !!!

Alas, I smiled shamelessly when I heard this sentence. Have you forgotten to have a wool relationship with me? There are many reasons for forgetting. The most direct reason should be that it is not profound enough. So we have put aside other distractions recently. Let's learn from the most basic HTML + CSS + JavaScript.

I hope this study will bring you and yourself different feelings. As I am going out for an interview recently, I am afraid Javascript is the most asked, so let's take a look at JS first, in this study, I took a look at some things that seem similar to me.

Function Parameters

You are right. I don't think I have a thorough understanding of function parameters:

1 VaRParam = {Name: 'Ye xiaochai ', age: 33};2 FunctionAlertargurment (d ){3For(VaRKInD ){4Alert (K + ':' +D [k]);5 }6 }7Alertargurment (PARAM );

There is no problem with this question. How can we change it?

1 VaRParam = {Name: 'Ye xiaochai ', age: 33};2 FunctionAlertargurment (PARAM ){3For(VaRKInParam ){4Alert (K + ':' +Param [k]);5 }6 }7Alertargurment (PARAM );

Do you know whether you are using the external Param or the internal Param? If it is still difficult, look at this:

  1   var  param = {Name: 'Ye xiaohuan ', age: 33  };   2   function   alertargurment (d) {  3  param. id = 'Knife crazy sword cray' ;  4   for  ( var  K  in   D) {  5  alert (K + ':' +  d [k]);   6  }  7  }  8  alertargurment (PARAM); 

what about this? Or what is the difference between the Code and the code? Compare the two codes:

  1   var  param = {Name: 'Ye xiaohuan ', age: 33  };   2   function   alertargurment (d) {  3  D. id = 'Knife crazy sword cray' ;  4   for  ( var  K  in   D) {  5  alert (K + ':' +  d [k]);   6  }  7  }  8  alertargurment (PARAM); 

How about it? Do you dare to say that you know the parameters? I am a little afraid, so let's take a look at what the following guys are doing?

 
① Arguments ② callee ③ caller

If you all know and have a deep understanding of it, then I am embarrassed myself .... Okay. Keep the above questions first. Let's take a look at the parameters:

 
The parameters of the ecmascript function are different from those of other languages. They have the following features: ① The number is unlimited; ② the type is unlimited; ③ When the function is called, the parameter is transferred. ecmascript internally uses an array to represent the parameters: the arguments object can access this array. However, arguments is similar to an array. It is not an array instance. Therefore, we can use arguments [0] instead of displaying the definition parameters when defining a function.
So function overloading can only be a legend.

Callee: There are two special objects in the function. One is this and the other is arguments. Arguments has a callee attribute, which is a pointer pointing to its function.

Ecmascript5 standardizes another function object attribute: caller, which stores the reference for calling the current function. The global variable corresponds to null.
Obtain arguments. callee. Caller (if you access arguments. callee in strict mode, an error is returned)

Let's first paste a picture to see:

As you can see, we just passed Param as a parameter, and added an attribute to it, But Param also has an attribute!

-------------- Gorgeous split line ------------------

PS: I suddenly remembered that I had a very disgusting question wrong during the interview that day. I suddenly remembered that, don't spray it. It's so disgusting...

1 VaRA ={};2A. A = 6;3 Alert (A. );4 5 VaRB =A;6B. A = 66;7Alert (A. );

I still answered this question correctly, but it seems that I have answered a wrong question like this. Else:

 
1 VaRA = 3;2 Alert ();3A = 4;

This is a question like this. Were I hungry or did my IQ decrease in front of the great gods? I suddenly remembered it and thought it was a good joke...

-------------- Gorgeous split line ------------------

Back to this question, let's take a look at this. Here we pass an object as a function parameter, rather than an object,It is better to say that it is an object reference..

 
All ecmascript parameters are passed with values and cannot be passed with references.

So changing the value internally will also change the external stuff, but the passing value will be different:

 
1 VaRA = 1;2 FunctionAlertargurment (d ){3D = 2;4 Alert (d );5 }6Alertargurment ();

There is 2 in this section, and A is still 1 outside. Based on the previous study, we will modify it again.Program:

 1   VaR Param = {Name: 'Ye xiaochai ', age: 33 };  2   Function  Alertargurment (d ){  3 D. id = 'Knife crazy swords' ;  4 Arguments [0]. QQ = 'plain Pr' ;  5       For ( VaR KIn  D ){  6 Alert (K + ':' + D [k]);  7   }  8   }  9   Alertargurment (PARAM );  10   VaR S = '';

Since reference cannot be passed, the object is directly passed in...

1 VaRParam = {Name: 'Ye xiaohaid', age: 33};2 FunctionAlertargurment (d ){3VaRTMP =Param;4D. id = 'Knife crazy swords';5Arguments [0]. QQ = 'plain Pr';6VaRSSS ='';7 }8Alertargurment (PARAM );

You can set a breakpoint to observe the changes of TMP. After d changes, TMP will be synchronized with it, so we should pass Param directly here:

Organize

Parameters of all functions in ecmascript are passed by value. At this point, we can blur it easily, because the reference type will be completely copied during transfer.

 
When the introduced reference type is passed, the address in the memory will be copied to a local variable, and the change of this local variable will be reflected to the external. As in the above example: ① We created an object Param ② we transferred it to the inside of the function and copied it to the D object (arguments [0]), d And Param are now the same object. They only have one object in the heap.

 

Conclusion

Well, our team's parameter research is now here. If you have any questions, please

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.