A brief analysis of the understanding of functions in JS
Text: We know that in js, a function is actually an object. Each function is an instance of the Function type, and has the same properties and methods as other reference types. Therefore, the function name is actually a pointer to the function object and is not tied to a function. In addition to the two common definition methods (see below), there is another definition method that can more intuitively reflect this concept:
var sum = new Function("num1", "num2", "return num1 + num2"); //不推荐
The constructor of Function can receive any number parameters, but the last parameter is always considered the function body. One disadvantage of this method is that it will cause the code to be parsed twice. The first time is to parse the basic ES code, and the second time is to parse the string passed into the constructor. This will lead to a decrease in performance. Write here It helps to understand the concept that functions in js are actually objects.
(1) There is no concept of overloading in js functions
When it comes to the concept of overloading, let’s make an analogy to how overloading in Java is implemented: In Java, methods are through methods A signature uniquely identifies a method. The method signature includes: method name, number of parameters, parameter order and parameter type. Therefore, if the method names of two methods are the same but other signature elements are different, the compiler will consider them to be two different methods, so that different methods with the same name can exist to realize the concept of overloading. (Quote: How to deeply understand why there is no overloading in js - manxisuo's answer in segmentfault).
As we said above, the function name in js is actually a pointer to the function object. Therefore, the function name can be said to be the unique identifier of a function. It has nothing to do with the parameter list, so it will not appear. Two functions with the same name (because a pointer can only point to one object at the same time) do not have the concept of overloading. For example:
function sum(num1, num2){ return num1 + num2; } function sum(num1, num2, num3){ return num1 + num2 + num3; }
The above can be written in another way, which will be more intuitive:
var sum = function(num1, num2){ return num1 + num2; } sum = function(num1, num2, num3){ return num1 + num2 + num3; }
This way it is obvious to see The above line points sum to function(num1,num2), and then points sum to function(num1, num2, num3), causing the second pointer to overwrite the first pointer, so it is more obvious to see There is no concept of overloading in js.
(2) The difference between function declaration and function expression
We know that there are two commonly used methods of defining functions: function declaration and function expression.
Function declaration can be said to be the most common definition method, as shown in the following example:
function sum(num1, num2){ return num1 + num2; }
Function expression, this definition method is used in closures and some frameworks There are many, such as the common $scope.doSomething = function(){...} in angular:
sum = function(num1, num2){ return num1 + num2; };
The above two definition methods are almost the same, but there is a small difference, please see the following example :
//函数声明 alert(sum(100, 100)); function sum(num1, num2){ return num1 + num2; } //函数表达式 alert(sum(100, 100)); var sum = function(num1, num2){ return num1 + num2; };
The two pieces of code don’t seem to be much different, just the way the function is defined is different. But the fact is that the first paragraph works fine but the second paragraph throws an error. This is because, before the code starts executing, the parser has already read and added function declarations to the execution environment through a process called function declaration hoisting. Simply put, the parser first places the function declaration at the top of the source tree. In the code below, the function is located in an initialization statement (it is not raised to the top). Simply put, before executing the assignment statement, sum does not point to any function, so calling the function before will Report an error.
The above is the understanding of functions in JS (Basics) 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. of. I would also like to thank you all for your support of the PHP Chinese website!
For more articles on the understanding of functions in JS, please pay attention to the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

Learning JavaScript is not difficult, but it is challenging. 1) Understand basic concepts such as variables, data types, functions, etc. 2) Master asynchronous programming and implement it through event loops. 3) Use DOM operations and Promise to handle asynchronous requests. 4) Avoid common mistakes and use debugging techniques. 5) Optimize performance and follow best practices.

Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...

Explore the implementation of panel drag and drop adjustment function similar to VSCode in the front-end. In front-end development, how to implement VSCode similar to VSCode...
