In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces how to use the function Function, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.
Function Function
1. Overview: a function is a repeatable snippet of code that completes a specific task and is a unit of code organized by JavaScript.
2. Function: organize the code into reusable units that can complete specific tasks and return data
3. Function definition:
The function of JavaScript belongs to the Function object, so you can use the constructor of the Function object to create a function.
3.2.You can use the function keyword to define a function in a normal form
Such as:
Var say = Function ([parameter list]) {statement; [return expression]} / / function variable
Function function name ([parameter list]) {statement; [return expression]} / / ordinary function
4. Function pointer calling mode
Callback: the mechanism is to call the function through a pointer.
The general way of function call is common and common, but there are many forms of function call in JavaScript, which is very flexible.
An important form of invocation that is often used in other languages is called callbacks. The callback function is based on the caller's agreement
Implement the function of the function, which is called by the caller
Function return type
In general, the non-object data returned by the function uses the value return method.
The reference type returns the address of the data, not the data itself.
The advantage of reference delivery is that it is fast, but the system will pay extra overhead to maintain the data. Reference passing is usually used when returning compound type data
Function getNameList () {
Var list = new Array ("Zhang San", "Li Si", "Wang Wu")
Return list; / / return reference
}
Var nameList = getNameList ()
NameList = null; / / Delete reference
Return function: a function can return a function pointer
External code can call the function object it refers to through a pointer in exactly the same way as a normal function.
In general, private functions cannot be called directly by the outside world, so the address of a private function can be returned to the outside world as a result.
Function getSum () {
Function sum (a _ r _ b) {
Return aquib
}
Return sum
}
Var sumOfTwo = getSum ()
Var total = sumOfTwo (1mai 2)
The scope of a function
Public functions: functions that are defined in the global scope and can be called by every code.
Private function: a function that is in local scope. When a function is nested within a definition
A child function is a private function of a parent function. Private functions cannot be called by the outside world
Can only be called by the function code that owns the function.
Function a () {
Alert ("a")
Function b () {
Alert ("b")
Function c () {
Alert ("c")
}
}
B (); / / Yes
C (); / / No
}
A (); / / call a
View plaincopy to clipboardprint?
/ / define a general sorting function
Function sortNumber (obj,func) {
/ / Parameter verification, throwing an exception if it is not a specified parameter type
If (! (obj instanceof Array) | |! (func instanceof Function)) {
Var e = new Error (); / / generate error message
E.number = 100000; / / define the error number
E.message = "invalid parameter"; / / error description
Throw e; / / throw an exception
} else {
For (n in obj) {/ / start sorting by
For (m in obj) {
If (func (OBJ [n], OBJ [m])) {/ / sort by callback function, rules are set by the user
Var temp = obj [n]; / / create a temporary variable
Obj [n] = obj [m]; / / Exchange data
Obj [m] = temp
}
}
}
}
Return obj; / / returns the sorted array
}
/ / callback function, user-defined collation
Function greatThan (arg1,arg2) {
Return arg1 > arg2; / / rules: from big to small
}
Try {
Var numAry = new Array (5, 8, 6, 32, 1, 45, 7, 25); / / generate an array
Pf ("before sorting:" + numAry)
SortNumber (numAry,greatThan); / / call the sort function
Pf ("after sorting:" + numAry)
} catch (e) {/ / catch exception
Alert (e.number+ ":" + e.message); / / exception handling
}
/ / define a general sorting function
Function sortNumber (obj,func) {
/ / Parameter verification, throwing an exception if it is not a specified parameter type
If (! (obj instanceof Array) | |! (func instanceof Function)) {
Var e = new Error (); / / generate error message
E.number = 100000; / / define the error number
E.message = "invalid parameter"; / / error description
Throw e; / / throw an exception
} else {
For (n in obj) {/ / start sorting by
For (m in obj) {
If (func (OBJ [n], OBJ [m])) {/ / sort by callback function, rules are set by the user
Var temp = obj [n]; / / create a temporary variable
Obj [n] = obj [m]; / / Exchange data
Obj [m] = temp
}
}
}
}
Return obj; / / returns the sorted array
}
/ / callback function, user-defined collation
Function greatThan (arg1,arg2) {
Return arg1 > arg2; / / rules: from big to small
}
Try {
Var numAry = new Array (5, 8, 6, 32, 1, 45, 7, 25); / / generate an array
Pf ("before sorting:" + numAry)
SortNumber (numAry,greatThan); / / call the sort function
Pf ("after sorting:" + numAry)
} catch (e) {/ / catch exception
Alert (e.number+ ":" + e.message); / / exception handling
}
The length property of the function
Function.length: is a read-only property that returns the number of parameters defined by the function
Arguments returns the list of parameters we passed when we called the function.
View plaincopy to clipboardprint?
Function check (args) {
Var actual = args.length; / / actual number of parameters
Var expected = args.callee.length; / / number of expected parameters
If (actual! = expected) {
/ / throw an exception when the number of actual participating parameters does not match
Throw new Error ("number of wrong parameters; shape parameter is" + expected + ", actual parameter is" + actual)
}
}
Function f (x, y, z) {
Var result = 0
Try {
/ / check whether the actual participating parameters match
Check (arguments)
Result = x + y + z
} catch (ex) {
[xss_clean] ("
It's a mistake! Error message: "+ ex.description)
}
Return result
}
[xss_clean] ("
The form of the call is: F (1 ~ 2 ~ 2 ~ 3) and the result is:
T "+ f (1, 2, 3))
[xss_clean] ("
The form of the call is: F (1Jing 2), and the result is:
T "+ f (1pm 2))
Function check (args) {
Var actual = args.length; / / actual number of parameters
Var expected = args.callee.length; / / number of expected parameters
If (actual! = expected) {
/ / throw an exception when the number of actual participating parameters does not match
Throw new Error ("number of wrong parameters; shape parameter is" + expected + ", actual parameter is" + actual)
}
}
Function f (x, y, z) {
Var result = 0
Try {
/ / check whether the actual participating parameters match
Check (arguments)
Result = x + y + z
} catch (ex) {
[xss_clean] ("
It's a mistake! Error message: "+ ex.description)
}
Return result
}
[xss_clean] ("
The form of the call is: F (1 ~ 2 ~ 2 ~ 3) and the result is:
T "+ f (1, 2, 3))
[xss_clean] ("
The form of the call is: F (1Jing 2), and the result is:
T "+ f (1pm 2))
Parameters of the function and its call
View plaincopy to clipboardprint?
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.