Home  >  Article  >  Web Front-end  >  JavaScript study notes (4) function function part_basic knowledge

JavaScript study notes (4) function function part_basic knowledge

WBOY
WBOYOriginal
2016-05-16 16:34:541062browse

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:

Copy code The code is as follows:


Here, we just assign the return value to a variable and then call it like a normal function:

Copy code The code is as follows:


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

Copy code The code is as follows:

function(){
return ‘Lee’; //A separate anonymous function cannot be run. Even if it can be run, it cannot be called because it has no name
}

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:

Copy code The code is as follows:

//Execute anonymous function by self-execution


Assign the return value of the anonymous function's self-execution to a variable:

Copy code The code is as follows:

//Assign the return value of the anonymous function's self-execution to the variable



Passing parameters to self-executing anonymous function:

Copy code The code is as follows:
//Pass parameters of self-executing anonymous function



javascript creates dynamic function:

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:





square is a dynamically created function. Each part of the content in the brackets after the Function object must be in string form, that is to say, it must be enclosed in quotation marks ("" or '')

This code:

var square = new Function ("x","y","var sum ; sum = x y;return sum;");

and the following code:


function square (x,y){
        var sum;
          sum = x y;
           return sum;
}


are exactly the same, except that one is a dynamic function and the other is a static function. Why should we divide the code into small pieces of code? , the advantage of dividing a string into several independent strings is that we can change the function of the function at any time by modifying some of the strings.


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:


Copy code The code is as follows:


The callback function here can be replaced by an example:

Copy code The code is as follows:


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

Copy code The code is as follows:

var arr = [1,2,3,4,5]
var a =12; // Variable: free
arr.a= 5; //Attribute: Belongs to an object
function show() //Function: Free
{
alert(‘a’);
}
arr.fn = function() //Method: Belongs to an object
{
alert(‘b’);
}

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

Copy code The code is as follows:

function class(){}
class.prototype={};
var item=new class();

Use as closure

Copy code The code is as follows:

(function(){
//Independent scope
})();

Call as constructor

The so-called constructor function is to generate a new object through this function.

Copy code The code is as follows:


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).

Copy code The code is as follows:

function Circle (xPoint, yPoint, radius) {
This.x = xPoint; // The x coordinate of the center of the circle.
This.y = yPoint; // The y coordinate of the center of the circle.
This.r = radius; // The radius of the circle.
}

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.

Copy code The code is as follows:



By convention, we should capitalize the first letter of a constructor function to distinguish it from ordinary functions.

The following two forms of defining functions are equivalent.

Copy code The code is as follows:


A variable test is clearly defined here, and its initial value is assigned to a function entity

Copy code The code is as follows:


Look at the following defined function form:

Copy code The code is as follows:


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

Copy code The code is as follows:


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

Copy code The code is as follows:


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

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Summary of native methods for obtaining styles in JavaScript_javascript tipsNext article:Summary of native methods for obtaining styles in JavaScript_javascript tips

Related articles

See more