Home > Article > Web Front-end > JavaScript study notes (4) function function part_basic knowledge
A function is an event-driven or reusable block of code that executes when it is called.
Jscript supports two types of functions: one is the internal function of the language (such as eval()), and the other is created by yourself.
A variable declared inside a JavaScript function (using var) is a local variable, so it can only be accessed inside the function. (The scope of this variable is local).
You can use local variables with the same name in different functions because only the function in which the variable is declared will recognize the variable.
How to call functions
1. Ordinary call: functionName (actual parameters...)
2. Call through a variable pointing to the function:
var myVar = function name;
myVar(actual parameter...);
Function that returns a function
1. When a function has no clear return value, the returned value is "undefined".
2. When a function has a return value, whatever the return value is is returned.
We can return a function to the place where it was called by using the return statement.
When using the return statement, the function stops execution and returns the specified value.
Functions usually return a unique value, so this value may also be another function:
Here, we just assign the return value to a variable and then call it like a normal function:
If you want the returned function to be executed immediately, you can also use box()() to execute this code.
The parameters of all ECMAScript functions are passed by value, which means that the parameters are not passed by reference.
PS: If there is pass by reference, then the variable in the function will be a global variable and can also be accessed externally.
(1) Value type: numeric value, Boolean value, null, undefined.
(2) Reference type: object, array, function.
Reference type value: refers to those objects stored in heap memory, which means that what is saved in the variable is actually just a pointer. This pointer executes another location in the memory, and the object is saved at that location;
Create anonymous function
This kind of anonymous function has many uses in JQuery. Declare an anonymous function directly and use it immediately. The advantage of using anonymous functions is that you don't have to define a function that is used once and then don't use it, and it also avoids the problem of naming conflicts. There is no concept of namespace in js, so it is easy for function names to conflict. In the event of a naming conflict, the last declared one shall prevail.
Execute an anonymous function via self-execution:
Assign the return value of the anonymous function's self-execution to a variable:
JavaScript supports the creation of dynamic functions. Dynamic functions must be defined using Function objects (Function is an object in JavaScript and is fixed. It is stipulated that the "F" of the Function object must be capitalized. When it is a function, we know It is a keyword used when defining a function: function funName(x, y). When it is Function (when F is capitalized), we know it is an object in JavaScript)
The basic format for creating a dynamic function: var variable name = new Function("Parameter 1", "Parameter 2", "Parameter n", "Execution statement");
Look at the following piece of code:
This code:
var square = new Function ("x","y","var sum ; sum = x y;return sum;");
and the following code:
Callback function
Callback is the calling process of a function. So let’s start by understanding this calling process. Function a has one parameter, which is function b. When function a is executed, function b is executed. Then this process is called callback.
In fact, Chinese is also easy to understand: callback, callback, means call back. Finish function a in advance and call function b later.
One thing must be clear here: function b is passed to function a in the form of a parameter, then function b is called a callback function.
Most of the effect functions in jquery involve callback functions. jquery effect function
For example:
The callback function here can be replaced by an example:
Callback actually means that after a function is executed, the function currently executed is the so-called callback function. How about it? It’s easy to understand...
The difference between methods and functions
In fact, methods are functions, but methods are objects to which they belong.
As we all know, binding a function to the click event
Syntax:
$(selector).click(function)
Parameter Description
function is optional. Specifies a function to run when a click event occurs.
This form is often seen in jquery. It uses function as a parameter of the method and adds an event handling function to the method.
js global function
Global functions are not the same concept as the properties or methods of built-in objects. Global functions do not belong to any built-in object.
JavaScript contains the following 7 global functions, which are used to complete some common functions:
escape( ), eval( ), isFinite( ), isNaN( ), parseFloat( ),
parseInt( ), unescape( ).
Several functions of functions
Used as a class constructor
Use as closure
Call as constructor
The so-called constructor function is to generate a new object through this function.
Objects can be created and initialized using the new operator in conjunction with predefined constructors like Object(), Date(), and Function(). A powerful feature of object-oriented programming is the ability to define custom constructors to create custom objects for use in scripts. Created a custom constructor so that objects with defined properties can be created. Below is an example of a custom function (note the use of the this keyword).
When calling the Circle constructor, give the value of the center point and the radius of the circle (all these elements are required to fully define a unique circle object). At the end of the day the Circle object contains three properties. Here's how to instantiate a Circle object.
var aCircle = new Circle(5, 11, 99);
The advantage of using a constructor function is that it can receive some parameters when creating an object.
The following two forms of defining functions are equivalent.
A variable test is clearly defined here, and its initial value is assigned to a function entity
Look at the following defined function form:
Obviously, the first function didn’t work. It’s strange, isn’t it? We know that the JavaScript parsing engine does not execute the code line by line, but executes the code section by section. In the analysis and execution of the same program, the defined function statements will be executed first, so the code logic of the first definition has been overwritten by the second one, so when the same function is called twice, only the second one will be executed.
function as value
Function is not only a syntax in js, but also a value. That is to say, the function can be assigned to a variable, stored in a property of an object or an element of an array, and passed as a parameter to another function.
The name of the function is actually invisible, it is just the name of the variable, which refers to the function object
In addition to assigning functions to variables, you can also assign functions to attributes of objects. When a function is called as an attribute of an object, the function is called a method
prototype attribute
Each function contains the prototype attribute, which points to a reference to an object. This object is called the prototype object.
For details, see: JavaScript study notes (5) Prototype and prototype chain
Higher-order functions
The higher-order function here is not the higher-order function in higher mathematics. The so-called higher-order function is a function that operates on a function. It receives one or more functions as parameters and returns a new function