Home Web Front-end JS Tutorial Code details of function calls in JavaScript

Code details of function calls in JavaScript

Mar 23, 2017 pm 02:26 PM

Perhaps many people have encountered confusion about the function parameter transmission method in the process of learning JavaScript. In the spirit of in-depth analysis, I will share with you a tutorial about JavaScript. Function calling knowledge, interested friends can learn together

Definition

Many people may have encountered function parameters in the process of learning Javascript Confused about the transmission method, in the spirit of in-depth analysis, I want to find some answers in the source code. But before doing this, I first need to clarify a few concepts. Abandon the inherent names of value passing, reference passing, etc., and return to English:

call by reference && call by value && call by sharing

They are what we understand as reference passing and value passing in C++. The third one is more confusing. The official explanation is receives the copy of the reference to object . Let me explain it in layman’s terms:

Object can be understood as a collection of key. Object refers to the data pointed to by key (I will not delve into whether it is a pointer implementation or a C++ reference implementation here). What the function receives is a copy of a variable. The variable contains a reference to the Object and is passed by value.

It is obvious that the objecttype parameter we receive when passing function parameters is actually a copy of the actual parameter, so it is not feasible to directly change the pointer of the type parameter; because the Object itself The keys are all references, so it is feasible to modify the key's pointer.

Proof

A few simple pieces of code can prove it

Code 1: The function can modify the data pointed to by key

let func = obj => { obj.name = 'Dosk' };
let obj = {name : 'Alxw'};
console.log(obj); //{ name: 'Alxw' }
func(obj)
console.log(obj); //{ name: 'Dosk' }
Copy after login

Code 2: The function cannot modify obj

let func = obj => { obj = {} };
let obj = {name : 'Alxw'};
console.log(obj); //{ name: 'Alxw' }
func(obj)
console.log(obj); //{ name: 'Alxw' }
Copy after login

Code 3: The internal obj and external === results are equal

let def = {name : 'Alxw'};
let func = obj => { console.log(obj === def) };
func(def); //true
Copy after login

So there may be something wrong with the third piece of code, since obj is a copy of def, why can the === operation still be true? Doesn't it mean that the === operation compares the address in the memory for Object? If it is a copy, it should be false?

So let’s go back to the source code of Google V8 to look at this.

In-depth Google V8

Let’s take a look at the strictly equal operation code part of the source code:

bool Object::StrictEquals(Object* that) {
 if (this->IsNumber()) {
  if (!that->IsNumber()) return false;
  return NumberEquals(this, that);
 } else if (this->IsString()) {
  if (!that->IsString()) return false;
  return String::cast(this)->Equals(String::cast(that));
 } else if (this->IsSimd128Value()) {
  if (!that->IsSimd128Value()) return false;
  return Simd128Value::cast(this)->Equals(Simd128Value::cast(that));
 }
 return this == that;
}
Copy after login

It should look like In the last case, theoretically if def and obj are different objects, then false should be returned. Doesn't this overturn the above? Actually no, one thing is ignored, that is, when instantiating an Object internally, Google V8 itself is a dynamic instantiation, and we know that in compiled languages, dynamic instantiation can only be done on the heap memory, that is, only pointers can be used. Quote. The proof of this conclusion involves the implementation of Local, Handle, etc. class. I think it is too troublesome. There is a simple way to prove it, that is, searchsource code It is found that all calls to Object::StrictEquals are passed in directly without taking the address operation.

However, some people may ask, since the variable passed by value contains a reference to Object, theoretically it can also modify Object. Why can't the third piece of code be modified?

The reason is very simple, because our so-called operations at the logical level of Javascript language are just calling the instance method of Google V8, and it is impossible to operate to this point (of course, potential The BUG is not counted -. -)

##Redefine##I think I can re-explain call by sharing here:

Indeed, the transfer is by value, but the content contains the Object pointer, and this pointer cannot be modified. It is shared by multiple variables.

Another simple proofCome on, look at the source code

V8_DEPRECATE_SOON("Use maybe version",
         Local<Value> Call(Local<Value> recv, int argc,
                  Local<Value> argv[]));
V8_WARN_UNUSED_RESULT MaybeLocal<Value> Call(Local<Context> context,
                       Local<Value> recv, int argc,
                       Local<Value> argv[]);
Copy after login

The above is about to be deprecated

Interface

, it happens that this version of the code I saw contains a lot of this code that is about to be deprecated, just take a look. The focus is on the second interface, which is the only calling interface of the function. The Local will eventually call C++'s bit copy, so it can be simply proved that it is value transfer.

Maybe this is the key pointDon’t forget, the variables we define are similar to

Handle

This form, so objects are shared between them. What we call variables in Javascript do not directly refer to instances of Object!!!

The last last

In short, it may be difficult to understand or even contain errors, but it is important to be able to determine the characteristics at the Javascript language level.

The above is the function call in Javascript introduced by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. I would also like to thank everyone for your support of the Script House website!

The above is the detailed content of Code details of function calls in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

How to use JavaScript and WebSocket to implement a real-time online ordering system How to use JavaScript and WebSocket to implement a real-time online ordering system Dec 17, 2023 pm 12:09 PM

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

C++ function call performance tuning: impact of parameter passing and return values C++ function call performance tuning: impact of parameter passing and return values May 04, 2024 pm 12:57 PM

C++ function call performance optimization includes two aspects: parameter passing strategy and return value type optimization. In terms of parameter passing, passing values ​​is suitable for small objects and unmodifiable parameters, while passing references or pointers is suitable for large objects and modifiable parameters, and passing pointers is the fastest. In terms of return value optimization, small values ​​can be returned directly, and large objects should return references or pointers. Choosing the appropriate strategy can improve function call performance.

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

How to call functions in different modules in C++? How to call functions in different modules in C++? Apr 12, 2024 pm 03:54 PM

Calling functions across modules in C++: Declare the function: Declare the function to be called in the header file of the target module. Implement function: Implement the function in the source file. Linking modules: Use a linker to link together modules containing function declarations and implementations. Call the function: Include the header file of the target module in the module that needs to be called, and then call the function.

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

C++ function call reflection technology: parameter passing and dynamic access of return values C++ function call reflection technology: parameter passing and dynamic access of return values May 05, 2024 am 09:48 AM

C++ function call reflection technology allows dynamically obtaining function parameter and return value information at runtime. Use typeid(decltype(...)) and decltype(...) expressions to obtain parameter and return value type information. Through reflection, we can dynamically call functions and select specific functions based on runtime input, enabling flexible and scalable code.

See all articles