Advanced explanation of javascript functions

Advanced explanation of javascript functions

Function definition method

function fn(){}//named function var fun=function(){}//anonymous function// new fn=new Funcion("parameter 1","parameter 2","function body"), rarely used.
//All functions are instance objects of Function (functions are also objects)
var fn = new Funcion("a","b","console.log(a+b)")
console.log(fn instanceof Object) //true

Function call (6 types)

this points to the problem

1. Ordinary function: window

2. Object method: instance object obj1

3. Constructor: instance object. This in the prototype object also points to the instance object ldh

4. Bind event function: event caller button1

5. Timer: window

6. Execute function immediately: window

Change the this pointer inside the function: call(), apply(), bind(),

If we don't need to call some functions immediately, but want to change the this pointer inside the function, use bind

Strict Mode

Enable for the entire script or for a function: "use strict";

Strict model syntax specification:

1. Variables must be declared before use

2. We cannot delete declared variables at will

3. This in the function in the global scope under the strict model is undefined

4. The constructor does not add a new call, this points to undefined, and assigning a value to undefined will result in an error (it previously pointed to window, which is equivalent to adding properties to window)

5. The timer's this still points to window. Events, objects, or pointing to the caller.

6. Parameters cannot have the same name

7. Functions must be declared at the top level. The new version of JavaScript will introduce "block-level scope" (introduced in ES6). To keep pace with newer versions, it is not allowed to declare a function inside a non-function code block.

Higher-order functions

Definition: A higher-order function is a function that operates on other functions, either receiving functions as parameters (callback functions) or returning functions as output.

Closures

A closure is a function that has access to variables in the scope of another function. Simply put, a scope can access local variables inside another function.

The role of closures: extending the scope of variables

Closure Exercise:

It is known that binding events and timers are asynchronous operations and will not be executed immediately.

(function(i){...})(i) The function will be executed immediately, and the parameters will be passed to the parentheses at the end. The parentheses inside the function will receive the parameters again. An immediately executed function is also called a small closure, and all functions inside it can access its internal variables.

(1) Click to output the current index number (common in interviews)

(2) Delay three seconds to output the content in <li>

(3)

Thinking about closure:

Recursion: A function that calls itself needs an end condition

Deep copy and shallow copy:

1. Shallow copy: only the top layer is copied, and the addresses of the deep objects are only copied, so changes to the original deep data will cause changes to the copied deep data

Object.assign(objNew,objOld)

2. Deep copy: copy all deep data values ​​to the new object. Data modifications of the new and old objects do not affect each other.

You may also be interested in:
  • Front-end advanced teaching you to use javascript storage function
  • JS array advanced examples [several array function usages]
  • Advanced JS function inheritance usage example analysis
  • Advanced JS function prototy usage example analysis
  • Advanced usage tips for split and join functions in JavaScript

<<:  Teach you how to build Redis cluster mode and sentinel mode with docker in 5 minutes

>>:  CSS element hiding principle and display:none and visibility:hidden

Blog    

Recommend

js to implement a simple bullet screen system

This article shares the specific code of native j...

How to delete special character file names or directories in Linux

Delete a file by its inode number First use ls -i...

Alibaba Cloud Server Tomcat cannot be accessed

Table of contents 1. Introduction 2. Solution 2.1...

jQuery implements the function of adding and deleting employee information

This article shares the specific code of jQuery t...

MySQL 8.0.19 installation and configuration method graphic tutorial

This article records the installation and configu...

How to set up vscode remote connection to server docker container

Table of contents Pull the image Run the image (g...

Solution to index failure in MySQL due to different field character sets

What is an index? Why create an index? Indexes ar...

An article teaches you to write clean JavaScript code

Table of contents 1. Variables Use meaningful nam...

JavaScript to achieve tab switching effect

This article shares the specific code of JavaScri...

Configure Java development environment in Ubuntu 20.04 LTS

Download the Java Development Kit jdk The downloa...

Method of realizing automated deployment based on Docker+Jenkins

Use Code Cloud to build a Git code storage wareho...

CSS3+HTML5+JS realizes the shrinking and expanding animation effect of a block

When I was working on a project recently, I found...

Example of how to install nginx to a specified directory

Due to company requirements, two nginx servers in...