Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the relevant knowledge points of js function?

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail what are the relevant knowledge points about the js function, the editor thinks it is very practical, so share it for you to do a reference, I hope you can get something after reading this article.

In js, functions are actually objects, and each function is an instance of type Function. Like other reference types, they all have properties and methods.

1. There are several ways to define a function

1) function declaration syntax definition

Function sum (num1,num2) {return num1+num2;}

2) function expression

Notice that there is a semicolon after the function expression

Var sum=function (num1,num2) {return num1+num2;}

Use the Function constructor

You can accept any number of parameters using the constructor, but the last parameter is always treated as a function body, and the previous parameter enumerates the parameter

Var sum=new Function ("num1", "num2", "return num1+num2")

Of course, the third method is not friendly and is not recommended.

Function names are just pointers to functions, so function names are no different from other variables that contain object pointers. This means that a function may have more than one name.

What's the difference between function declarations and function expressions here?

When the parser loads data into the execution environment, the function declaration is different from the function expression. 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 promotion. The function expression is different, and it must wait until the function parser executes to his code block before it is actually executed. Compare the following two examples

Console.log (sum (1010)); function sum (N1 ~ N2) {return N1 ~ N2;} the above code will execute normally. Console.log (sum (10); var sum=function (N1) {return N1 (N2);} the above actuators will report an error!

two。 Function is not overloaded

If two functions with the same name are declared, the result is that the latter function overrides the previous function.

Function sun (a) {return asides 100;} function sun (a) {return asides 200;} var result=sum (2) / / 202

3. Internal properties of a function

Including this,arguments

The details are no longer described.

4. Properties and methods of functions

The function has two properties, length and prototype

Length indicates the number of named arguments that the function wants to receive.

Function a (name) {/ / todo} function b (name,age) {/ / todo} function c () {/ / todo} a.length / / 1b.length / / 2c.length / / 0

The prototype attribute is important for reference types and will be specifically explained, so I won't repeat it here.

Each function contains 2 non-inherited methods apply,call

The purpose of both methods is to call a function in a specific scope, which is to set the value referred to by the this in the body of the function.

First. The apply () method takes two parameters, one is the scope of the function, the other is the parameter array, and the second parameter can be an instance of the array or arguments.

Function sum (num1,num2) {return num1+num2;} function test1 (AMagneb) {return sum.apply (this,arguments);} function test2 (cMagne d) {return sum.apply (this. [cMagne d]);} console.log (test1 (1m 1)) / / 2console.log (test2 (1J 1)) / / 2

There is not much difference between the call method and the apply method. The difference is that the second parameter of the call method must be passed one by one.

The biggest advantage of using call and apply to extend scope is that objects are not coupled to any method

This is the end of this article on "what are the relevant knowledge points of js function". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it out for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report