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 is the JavaScript function?

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article shows you what the function of JavaScript is, which is concise and easy to understand. It can definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

A function is a piece of code that is defined only once, but can be executed or called any number of times. In JavaScript, functions are objects, and programs can manipulate them at will. For example, you can assign functions to variables, or pass them as arguments to other functions, set properties to them, or even call their methods. If a function is mounted on an object as a property of the object, it is called the method of the object. If functions are defined nested in other functions, they can access any variable in the scope in which they are defined.

Function definition

In JavaScript, a function is actually an object, and each function is an instance of a Function constructor, so the function name is actually a pointer to the function object and is not bound to a function. Functions are usually defined in the following 3 ways. For example:

/ / method 1: function declaration (recommended) function sum (num1, num2) {return num1 + num2;} / / method 2: function expression (recommended) var sum = function (num1, num2) {return num1 + num2;}; / / method 3: Function constructor (not recommended) var sum = new Function ("num1", "num2", "return num1 + num2")

Because the function name is just a pointer to the function, the function name is no different from other variables that contain object pointers. In other words, a function may have more than one name. For example:

Function sum (num1, num2) {return num1 + num2;} console.log (sum); / / 20 var anotherSum = sum; console.log (anotherSum (10)); / / 20 sum = null; console.log (anotherSum (10)); / / 20

No overload

Imagining a function name as a pointer also helps to understand why there is no concept of function overloading in JavaScript.

Function addSomeNumber (num) {return num + 100;} function addSomeNumber (num) {return num + 200;} var result = addSomeNumber

Obviously, two functions with the same name are declared in this example, and the result is that the latter function overrides the previous function. The above code is actually no different from the following code.

Var addSomeNumber = function (num) {return num + 100;}; addSomeNumber = function (num) {return num + 200;}; var result = addSomeNumber; / / 300

After rewriting the code, it's easy to see that when you create the second function, you actually override the variable addSomeNumber that references * functions.

Function declaration and function expression

Parsers do not treat function declarations and function expressions equally when loading data into the execution environment. The parser is the first to read the function declaration and make it available (accessible) before any code is executed; as for a function expression, it will not really be interpreted until the parser executes to its line of code. For example:

Console.log (sum (1010)); / / 20 function sum (num1, num2) {return num1 + num2;}

The above code works perfectly. Because before the code starts execution, the parser reads and adds the function declaration to the execution environment through a process called function declaration promotion (function declaration hoisting). When evaluating the code, the JavaScript engine declares functions and places them at the top of the source code tree. So, even if the code that declares the function is behind the code that calls it, the JavaScript engine can raise the function declaration to the top. Changing the above "function declaration" to the equivalent "function expression" will cause an error during execution. For example:

Console.log (sum (1010)); / / Uncaught TypeError: sum is not a function var sum = function (num1, num2) {return num1 + num2;}

In addition to the above differences, the syntax of "function declaration" and "function expression" is equivalent.

A function as a value

Because the function name in JavaScript is itself a variable, the function can also be used as a value. That is, you can not only pass one function to another like a parameter, but also return one function as the result of another function. Let's take a look at the following function.

Function callSomeFunction (someFunction, someArgument) {return someFunction (someArgument);}

This function takes two arguments. The * argument should be a function, and the second argument should be a value to be passed to the function. Then, you can pass the function as in the following example.

Function add10 (num) {return num + 10;} var result1 = callSomeFunction (add10, 10); console.log (result1); / / 20 function getGreeting (name) {return "Hello," + name;} var result2 = callSomeFunction (getGreeting, "Nicholas"); console.log (result2); / / "Hello, Nicholas"

The callSomeFunction () function here is generic, that is, no matter what function is passed in the * * parameters, it will return the result after executing the * * parameters. To access the pointer to a function without executing the function, you must remove the pair of parentheses after the function name. So what is passed to callSomeFunction () in the above example is add10 and getGreeting, not the result of executing them.

Of course, you can also return one function from another, and this is a very useful technique. For example, suppose you have an array of objects, and we want to sort the array according to an object property. The comparison function passed to the array sort () method receives two parameters, the value to be compared. However, we need a way to indicate which attribute to sort by. To solve this problem, you can define a function that takes a property name and then creates a comparison function based on that property name. here is the definition of this function.

Function createComparisonFunction (propertyName) {return function (object1, object2) {var value1 = object1 [propertyName]; var value2 = object2 [propertyName]; if (value1

< value2){ return -1; } else if (value1 >

Value2) {return 1;} else {return 0;}};}

This function definition looks a bit complicated, but in fact it is nothing more than nesting one function within another, and the inner function is preceded by a return operator. After the inner function receives the propertyName parameter, it uses square brackets to get the value of the given property. Once you have the desired attribute value, it is very easy to define the comparison function. The above function can be used as in the following example.

Var data = [{name: "Zachary", age: 28}, {name: "Nicholas", age: 29}]; data.sort (createComparisonFunction ("name")); console.log (data [0] .name); / / Nicholas data.sort (createComparisonFunction ("age")); console.log (data [0] .name); / / Zachary

Here, we create an array data that contains two objects. Each object contains a name property and an age property. By default, the sort () method calls the toString () method of each object to determine their order; however, the results are often not in line with human thinking habits. Therefore, we call the createComparisonFunction ("name") method to create a comparison function to sort by the value of the name property of each object. The items at the top of the list are name with "Nicholas" and age with 29. Then we use the comparison function returned by createComparisonFunction ("age"), this time sorted by the object's age property. The result is that objects with a name value of "Zachary" and an age value of 28 are ranked in the * * bit.

Formal parameters and actual parameters of a function

Inside the function, there are two special objects: arguments and this. Where arguments is a class array object that contains all the parameters passed in the function. Although the main purpose of arguments is to hold function parameters, the object also has a property called callee, which is a pointer to the function that owns the arguments object. Take a look at the following very classic factorial function.

Function factorial (num) {if (num)

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