In-depth study of JS series is the witness of my phased growth. I hope to comb relevant JS knowledge more rigorously and objectively through the form of articles. I also hope to help more friends of front-end development to solve problems and look forward to our common progress.

If you think this series is good, welcome to like, comment, forward, your support is the biggest power OF my persistence.


Definition:

In JavaScript Advanced Programming, 3rd edition 4.1.3, we talk about passing parameters:

Arguments to all functions in ECMAScript are passed by value.

What is passing by value?

That is, copying a value from outside a function to an argument inside a function is the same as copying a value from one variable to another.

Pass by value

Here’s a simple example:

var value = 1;

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

foo(value);
console.log(value); / / 1
Copy the code

When you pass a value to foo, you make a copy of the value. Let’s say the copy is called _value. The function changes the value of _value without affecting the original value.

So in the code above, when we execute foo, we pass the global value and when we pass that value as an argument, we reassign it internally to be 2 so we print 2 inside the function, and then we print the global value again after the function is done with the value of 1, Note The value changed inside the function does not affect the global value.

Passing by reference?

Copying is well understood, but when values are complex data structures, copying can cause performance problems. So there’s another way of passing data called passing by reference.

Passing by reference refers to passing a reference to an object, and any changes to parameters within a function affect the value of that object, because they refer to the same object.

Here’s an example:

var obj = {
  value: 1
};

function foo(o) {
  o.value = 2;
  console.log(o.value); / / 2
}
foo(obj);
console.log(obj.value); / / 2
Copy the code

All function parameters in js are passed by value. How can this function be passed by reference?

Is this passed by reference?

The third method of transmission

Take your time, let’s look at an example:

var obj = {
  value: 1
};

function foo(o){
  o = 2;
  console.log(o); / / 2
}
foo(obj);

console.log(obj.value);/ / 1
Copy the code

If js is passed by reference in the above code, the outer values will also be changed. Why is this not changed in the final print?

So there’s also a third type of delivery — shared delivery.

Shared passing means that when you pass an object, you pass a copy of the object reference.

Note: passing by reference is passing a reference to an object, while passing by share is passing a copy of a reference to an object!

So changing O.value can find the original value by reference, but directly changing O does not change the original value, so the second and third examples are actually shared passing.

Finally, you can understand it this way:

Parameters are passed by value if they are of basic type and shared if they are of reference type. But because a copy copy is also a copy of a value, it is also considered to be passed by value in elevation.

# # reference:

JavaScript In Depth: Passing Parameters by Value

Learn more about the JavaScript catalog

  • #1 [In-depth study of JS — Prototype and Prototype Chain]
  • #2: Lexical scope and dynamic scope
  • #3 [In-depth study of JS — implement Yamashita Text stack]
  • #4 【 In-depth study of JS – variable objects 】
  • #5 [In-depth study of JS — scope chain]
  • #6 [In-depth study of JS — This point in real Development Scenarios]
  • #7: The execution context of JS
  • #8: Js closures
  • #9 — Parameter passing by value
  • #10: Call and Apply
  • Class array objects and arguments
  • Learn more about the various ways to create objects in JS

Welcome to add my personal wechat to discuss technology and personal growth.