In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what are the function knowledge points in javascript". The explanation in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn what are the function knowledge points in javascript.
JavaScript uses the keyword function to define functions.
The function can be defined by declaration, or it can be an expression, which is not executed immediately after the function is declared, but will be called when we need it.
Function declaration
The syntax is as follows:
Function (function name) functionName (parameters (parameter)) {
Executed code
}
Semicolons are used to separate executable JavaScript statements and do not end with semicolons because the function declaration is not an executable statement.
The function will not be executed immediately after it is declared, but will be called when we need it.
Function expression:
The JavaScript function can be defined by an expression, the function expression can be stored in the variable, and after the function expression is stored in the variable, the variable can also be used as a function.
The function is stored in a variable and does not require a function name, which is usually called through the variable name, and the function is actually an anonymous function (the function has no name).
The Function () constructor:
Functions can also be defined through the built-in JavaScript function constructor (Function ()).
In JavaScript, there are times when you need to avoid using the new keyword.
Function promotion (Hoisting):
Hoisting is the behavior that JavaScript promotes the current scope to the front by default.
Hoisting is applied to the declaration of variables and functions.
Therefore, the function can be called before the declaration:
Cannot be promoted when using an expression to define a function.
Self-calling function:
A function expression can be "self-calling", and the function is actually an anonymous self-calling function (without a function name).
The self-calling expression is called automatically.
If the expression is followed by (), it is called automatically.
A declared function cannot be called by itself.
Indicate that it is a function expression by adding parentheses.
The JavaScript function is used as a value:
The JavaScript function can be used as an expression
Self-calling function syntax: (functin () {}) ()
A function is an object:
Determining the function type using the typeof operator in JavaScript returns "function".
But the JavaScript function is more accurately described as an object.
JavaScript functions have properties and methods.
The arguments.length property returns the number of parameters received by the function call.
The toString () method returns the function as a string.
The function is defined as the property of the object, which is called the object method, and if the function is used to create a new object, it is called the constructor of the object.
Function parameter
The functions explicit parameter (Parameters) and implicit parameter (Arguments).
Explicit parameters (Parameters): function explicit parameters are listed when the function is defined.
Implicit parameters (Arguments): the implicit parameters of a function are passed to the true value of the function when the function is called.
Parameter rules:
The explicit parameter does not specify a data type when the JavaScript function is defined.
The JavaScript function does not type-check implicit parameters.
The JavaScript function does not detect the number of implicit parameters.
.
If the function does not provide an implicit parameter when it is called, the parameter is set to: undefined by default.
Sometimes this is acceptable, but it is recommended that you set a default value for the parameter:
If too many parameters are set when the function is called, the parameters cannot be referenced because the corresponding parameter name cannot be found. Can only be called using the arguments object.
Arguments object:
The JavaScript function has a built-in object, arguments object.
In this way you can easily find the value of the largest parameter.
Pass parameters by value:
The parameters called in the function are the implicit parameters of the function.
JavaScript implicit parameters are passed by value: the function simply takes the value.
If the function modifies the value of the parameter, it does not change the initial value of the explicit parameter (defined outside the function).
Changes in implicit parameters are not visible outside the function.
Pass parameters through the object:
In JavaScript, you can reference the value of an object.
So if we modify the properties of the object inside the function, we will change its initial value.
Modifying object properties can act outside the function (global variables).
Modifying object properties is visible outside the function.
JavaScript function call
The JavaScript function can be called in four ways.
The difference in each way is the initialization of this.
This keyword:
Generally speaking, in Javascript, this points to the current object when the function is executed. Note: this is a reserved keyword, you cannot change the value of this.
The JavaScript function is called, and the code in the function executes after the function is called.
The default global object in HTML is the HTML page itself, so the function belongs to the HTML page.
The page object in the browser is the browser window (window object), and myFunction () and window.myFunction () are the same.
This is a common way to call JavaScript functions, but it is not a good programming habit. Global variables, methods or functions can easily cause naming conflicts in bug.
Global objects:
The value of this becomes a global object when the function is not called by its own object.
In web browsers, the global object is the browser window (window object).
When the function is called as a global object, the value of this becomes a global object, and using the window object as a variable can easily cause the program to crash.
Function as a method call:
In JavaScript you can define a function as a method of an object.
The function is called as an object method, which makes the value of this become the object itself.
Use the constructor to call the function:
If the new keyword is used before the function call, the constructor is called.
The call to the constructor creates a new object. The new object inherits the properties and methods of the constructor.
The this keyword in the constructor has no value, and the value of this is created when the function calls the instantiated object (new object).
Call the function as a function method:
In JavaScript, a function is an object. The JavaScript function has its properties and methods.
Call () and apply () are predefined function methods. Two methods can be used to call a function, and the first parameter of both methods must be the object itself.
Both methods use the object itself as the first parameter. The difference between the two lies in the second parameter: apply passes in an array of parameters, that is, multiple parameters are combined into an array, while call is passed in as a parameter to call (starting with the second parameter).
In JavaScript strict mode (strict mode), the first argument becomes the value of this when the function is called, even if it is not an object.
In JavaScript non-strict mode (non-strict mode), if the value of the first parameter is null or undefined, it will be replaced by a global object.
You can set the value of this through the call () or apply () method and call it as a new method for an existing object.
This is a keyword of the JavaScript language. It represents an internal object that is automatically generated when the function is running and can only be used within the function.
The value of this changes depending on where the function is used. But there is a general principle, that is, this refers to the object that calls the function.
JavaScript closure
JavaScript variables can be local or global variables, private variables can use closures, and functions can access variables defined within the function.
Global variables belong to window objects in web pages, and global variables can be applied to all scripts on the page.
A local variable can only be used to define the interior of its function. Is not available for other functions or script code.
Global and local variables are two different variables even if they have the same name. Modifying one of them does not affect the value of the other.
If a variable is declared without the var keyword, it is a global variable, even if it is defined within a function.
Variable life cycle:
The scope of global variables is global, that is, global variables are everywhere in the whole JavaScript program.
Variables declared inside the function only work inside the function. These variables are local variables and the scope is local; the parameters of the function are also local and only work within the function.
Counter dilemma:
Imagine that if you want to count some values, and the counter is available in all functions, you can use global variables, and the function sets the counter to increment.
JavaScript embedded functions:
All functions can access global variables.
In fact, in JavaScript, all functions can access the scope of the layer above them.
JavaScript supports nested functions. Nested functions can access function variables at the upper level.
A closure is a function that can access variables in the scope of a function at the upper level, even if the function at the upper level has been closed.
Thank you for your reading, these are the contents of "what are the function knowledge points in javascript". After the study of this article, I believe you have a deeper understanding of what the function knowledge points in javascript have, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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: 287
*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.