In-depth understanding of JavaScript parameter passing by value, in-depth understanding of javascript

Source: Internet
Author: User

In-depth understanding of JavaScript parameter passing by value, in-depth understanding of javascript

Definition
All function parameters in ECMAScript are passed by value.

What is pass by value?

That is to say, copying the value outside the function to the parameter inside the function is the same as copying the value from one variable to another.

Pass by value

A simple example:

var value = 1;function foo(v) {  v = 2;  console.log(v); //2}foo(value);console.log(value) // 1

It is easy to understand that when the value is passed to function foo, it is equivalent to copying a copy of value. Assume that the copy is called _ value, and all the changes in the function are the value of _ value, without affecting the original value.

Reference Transfer

Although the copy operation is easy to understand, when the value is a complex data structure, the copy operation may cause performance problems.

So there is another transfer method called transfer by reference.

Passing by reference means passing the object reference. Any changes to the parameter in the function will affect the value of the object, because the two reference the same object.

For example:

var obj = {  value: 1};function foo(o) {  o.value = 2;  console.log(o.value); //2}foo(obj);console.log(obj.value) // 2

Ah, no. Even our redbooks have said that all function parameters in ECMAScript are passed by value. How can this be passed by reference?

Is this actually a reference transfer?

Third Transfer Mode

Don't worry. Let's look at another example:

var obj = {  value: 1};function foo(o) {  o = 2;  console.log(o); //2}foo(obj);console.log(obj.value) // 1

If JavaScript uses reference transmission, the value of the outer layer will also be modified. Why is this not changed? So is it true that it is not a reference transfer?

In this case, there is actually a third transfer method, that is, transfer by share.

Shared transfer refers to the transfer of a reference copy of an object when it is passed.

Note:Transferring by reference is to pass the object reference, while transferring by share is to pass the reference copy of the object!

Therefore, to modify the o. value, you can find the original value through reference, but directly modify the o without modifying the original value. So the second and third examples are actually based on shared transfer.

Finally, you can understand:

If the parameter is of the basic type, it is passed by value, if it is of the reference type, it is passed by share.

However, because the copy is also a copy of the value, it is also directly considered to be passed by value in the elevation.

So, elevation, who told you to be the redbaby!

In-depth Series

JavaScript deep series directory address: https://github.com/mqyqingfeng/Blog.

The JavaScript in-depth series is expected to write about 15 articles to help you understand the underlying knowledge of JavaScript, key concepts such as prototype, scope, execution context, variable object, this, closure, pass by value, call, apply, bind, new, and inheritance are explained.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.