You must know that in js, the reference type is a data structure, including 5 reference types, namely Object, data, Array, rePlease enter the code
gExp, Function. Today we will talk about the data structure of Function.
In js, functions are actually objects, and each function is an instance of the Function type. Like other reference types, they have properties and methods.
1. There are several ways to define functions
-
Function declaration syntax definition
function sum(num1,num2){ return num1+num2; }
Function expression
Note There is a semicolon after the function expression;
var sum=function(num1,num2){ return num1+num2; }
-
Use the Function constructor
使用构造函数可以接收任意数量的参数,但最后一个参数始终被看成是函数体,而前面的参数则枚举出了参数
var sum=new Function("num1","num2","return num1+num2")
Of course the third option The method is unfriendly and not recommended.
The function name is just a pointer to the function, so the function name is no different from other variables containing object pointers. That is to say, a function may have multiple names.
What is the difference between function declaration and function expression here?
When the parser loads data into the execution environment, it treats function declarations and function expressions differently. The parser first reads the function declaration and makes the function declaration code callable before any code. This is what we usually call function declaration hoisting. Function expressions are different. They must wait until the function parser executes the code block where they are located before they are actually executed. Compare the following two examples
console.log(sum(10,10)); function sum(n1,n2){ return n1+n2; } 以上代码会正常执行 再看! console.log(sum(10,10)); var sum=function(n1,n2){ return n1+n2; } 以上执行器就会报错!
2. Functions are not overloaded
If two functions with the same name are declared, the result is that the later function overwrites the previous function.
function sun(a){ return a+100; } function sun(a){ return a+200; } var result=sum(2)//202
3. The internal attributes of the function
-
Including this, arguments
The details will not be described
4. Function attributes and methods
The function has 2 attributes, length and prototype
length indicates that the function hopes to receive named parameters number.
function a(name){ //todo } function b(name,age){ //todo } function c(){ //todo } a.length //1 b.length //2 c.length //0
The prototype attribute is very important for reference types and will be explained specifically, so I won’t go into details here.
Each function contains two non-inherited methods apply and call
The purpose of these two methods is to perform specific functions Calling a function in a domain actually sets the value referred to by this in the function body.
first. The apply() method receives two parameters, one is the scope in which the function runs, and the other is the parameter array. The second parameter can be an instance of the array or arguments.
function sum(num1,num2){ return num1+num2; } function test1(a,b){ return sum.apply(this,arguments); } function test2(c,d){ return sum.apply(this.[c,d]); } console.log(test1(1,1)) //2 console.log(test2(1,1)) //2
call method and apply method Not much difference. The difference is that the second parameter of the call method must be listed one by one.
The biggest advantage of using call and apply to expand the scope is that the object is not coupled to any method.
Related recommendations:
Detailed explanation of JS function examples
##js Passing parameters of functions by value
JS function calls and issues related to implicit parameters arguments and this
The above is the detailed content of Knowledge required for js functions. For more information, please follow other related articles on the PHP Chinese website!

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe

The differences in performance and efficiency between Python and JavaScript are mainly reflected in: 1) As an interpreted language, Python runs slowly but has high development efficiency and is suitable for rapid prototype development; 2) JavaScript is limited to single thread in the browser, but multi-threading and asynchronous I/O can be used to improve performance in Node.js, and both have advantages in actual projects.

JavaScript originated in 1995 and was created by Brandon Ike, and realized the language into C. 1.C language provides high performance and system-level programming capabilities for JavaScript. 2. JavaScript's memory management and performance optimization rely on C language. 3. The cross-platform feature of C language helps JavaScript run efficiently on different operating systems.

JavaScript runs in browsers and Node.js environments and relies on the JavaScript engine to parse and execute code. 1) Generate abstract syntax tree (AST) in the parsing stage; 2) convert AST into bytecode or machine code in the compilation stage; 3) execute the compiled code in the execution stage.

The future trends of Python and JavaScript include: 1. Python will consolidate its position in the fields of scientific computing and AI, 2. JavaScript will promote the development of web technology, 3. Cross-platform development will become a hot topic, and 4. Performance optimization will be the focus. Both will continue to expand application scenarios in their respective fields and make more breakthroughs in performance.


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

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver Mac version
Visual web development tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

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