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

Reference type-Function type

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

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

Function Typ

There are three ways to define a function:

1. Function declaration

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

2. Function expression

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

3. Function constructor (not recommended)

/ / the last parameter is always regarded as the body of the function, resulting in code parsing twice.

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

Note: function names are only pointers to functions, so function names are no different from other pointers that contain objects.

Function sum (num1,num2) {return num1 + num2;} alert (sum (10Mag10)); / / 20var another = sum;alert (another (10Lab 10)); / / 20sum = null;alert (another (10Lab 10)); / / 20sum and another both point to the same function, so setting sum to null does not affect another

Note: using a function name without parentheses is to access the function pointer, not to call the function

No overload

Understanding the function name as a pointer is helpful to understand why there is no overloading

Function addSomeNumber (num) {return num + 100;} function addSomeNumber (num) {return num + 200;} alert (addSomeNumber (100))

The second function points addSomeNumber to the new function

Function declaration and function expression

When the parser loads data into the execution environment, it is not a temporary colleague of function declarations and function expressions. The parser takes the lead in reading the function declaration and making it available before any new lines of code are held. For a function expression, it will not really be parsed until it is executed to the line of code where it is located.

Alert (sum (10.10)); / / 20function sum (num1,num2) {return num1 + num2;}

Before the code execution environment, the parser reads and adds the function declaration to the execution environment through a process called function declaration promotion. When evaluating the code, the JavaScript engine declares functions at the first time and places them at the top of the source code tree

Alert (sum (1010)); sum = (num1,num2) {num1 + num2;}

A function as a value

You can pass one function to another like an argument, or you can return a function as a result of another function.

Function callSomeFunction (someFunction,someArugment) {return someFunction (someArugment);} function add10 (num) {return num + 10;} var result = callSomeFunction (add10,10); alert (result); / / 20

You can return one function from another

Function createComparisonFunction (propertyName) {return function (object1,object2) {var values1 = object1 [propertyName]; var values2 = object2 [propertyName]; if (values1values2) return 1; else return 0;} } var data = [{name: "qunzhu", age:21}, {name: "huishuai", age:22}, {name: "kuolang", age:23}]; data.sort (createComparisonFunction (name)); alert (data [1] .name); / / huishuai

Internal properties of function

Inside the function, there are two special objects: arguments and this.

Arguments, which is a class array object that contains all the parameters passed in the function, has a callee property, which is a pointer to the function that owns the arguments object.

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

Network Security

Wechat

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

12
Report