Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the differences between Js value passing and reference passing

Shulou Source: shulou.com Published: 2022-05-31 17:45:51 10月04日 Update

This article introduces the relevant knowledge of "what are the differences between Js value transmission and reference transmission". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Different concepts

1. The value is passed one-way, which can only be passed from the argument to the parameter, and the parameter cannot change the value of the parameter.

2. The passing of reference type can change the actual parameter by changing the parameter. As long as any variable points to the same space, the variable can manipulate the value of that space.

The type of data passed

3. Value transfer passes a value. Reference passing is passing an object.

(1) value transfer

Var a = 1 fn function fn (a) {a + +; return a;} a = 1 position console.log (fn (a)); console.log (a)

(2) reference transfer

Var obj = {name: "abc",} function fn (obj) {Orville's Ideas and Interests = "abc"; / / points to the object created outside the function obj = new Object (); / / creates a brand new object that does not point to the externally created object Orville's Ideas and Interests = "123"; / / adds attributes to the newly created object} var obj = new Object (); fn (obj); console.log (Orville's Ideas and Interests) / / obj is an externally created object, so the output value is "abc"what are the differences between Js value passing and reference passing". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Object different point parameter different point content variable more knowledge space type output practical successful learning next brand-new function one-way can pass dilemma Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Shulou Technology Apple Huawei OPPO Reno