1. Function definition and invocation

1.1 Definition of functions

  1. Function keyword (named function)
  2. Function expressions (anonymous functions)
  3. new Function()
var fn = new Function(Parameters' 1 '.Parameters' 2 '. .'Function body')
Copy the code
  • All arguments in Function must be strings
  • The third way is inefficient in execution, inconvenient to write, and less used
  • All functions are instances of Function
Var fn = function() {} var fn = function() {} new function('arg1','arg2', 'fn) var f = new Function (' a', 'b', the console log (a + b) ') f (1, 2) / / 3Copy the code

All functions are Function instance objects

1.2 How to call functions

  1. Common function
  2. Object methods
  3. The constructor
  4. Bind event function
  5. Timer function
  6. Execute function immediately
// A normal function
function fn() {
	console.log('Front-end LAN Feng Blog')
}
fn() // Front-end LAN Feng blog

// Object method
var obj = {
	say: function () {
  	console.log('Front-end LAN Feng Blog')
  }
}
obj.say() // Front-end LAN Feng blog

// constructor
function Star() {}new Star()

// Bind the event function
btn.onclick = function() {} // Call this function when the button is clicked

// The timer function
setInterval(function() {}, 1000)

// Execute the function immediately
(function() {
	console.log('Front-end LAN Feng Blog') // Automatic call}) ()Copy the code

2. this

2.1 Reference to this in functions

The orientation of this is determined when the function is called by the method that determines the orientation of this, usually to the caller. Call way

Call way This points to the
Ordinary function calls window
Constructor call The methods inside the prototype object also point to the instance object
Object method call The object to which the method belongs
Event binding method Bind event object
Timer function

window
Execute function immediately window

2.2 Change the this pointer inside the function

Javascript provides a number of function methods to help us more elegantly handle this pointing problems inside functions. Common methods include bind(), call(), and apply(). Let’s take a closer look at the three methods and see how they change the “this” reference.

  1. Call method

The call() method calls an object, simply the way a function is called, but it can change the function’s this reference.

fn.call(thisArg, arg1, arg2, ...)
Copy the code
var obj = {
		name: 'lanfeng'
}
function fn(a, b) {
	console.log(this) 
  console.log(a+b) 
}
fn(1.2)// Point to window, 3
fn.call(obj, 1.2) // point to obj, 3

// Implement inheritance
function Father(uname, age, sex) {
	this.uname = uname
  this.age = age
  this.sex = sex
}
function Son (uname, age, sex) {
	Father.call(this,uname, age, sex)
}
var son = new Son('Ada'.18.'woman')
Copy the code

Call: The first one can call the function, and the second one can change the main role of this to call in the function to implement inheritance

  1. The apply method

The apply () method calls a function. The call method can change the direction of the function’s this, but it is different from the call method. The parameters must be passed in an array

fun.apply(thisArg, [argsArray])
Copy the code
  • ThisArg: Specifies the value of this when fun runs
  • ArgsArray: The value passed must be contained in the array
  • The return value is the return value of the function, because it is the calling function
var obj = {
		name: 'lanfeng'
}
function fn(a, b) {
	console.log(this) 
  console.log(a+b) 
}
fn(1.2)// Point to window, 3
fn.apply(obj, [1.2]) // point to obj, 3
Copy the code

Apply: the first one can call the function, and the second one can change the parameter in the function that this refers to apply

var arr = [1.66.3.99.4]
var max = Math.max.apply(Math, arr) 
var min = Math.min.apply(Math, arr) 
console.log(max, min) / / 1 99
Copy the code
  1. The bind method

The bind() method does not call the function, but can change the this reference inside the function

fn.bind(thisArg, arg1, arg2, ...)
Copy the code
  • ThisArg: this value specified when the fn function is run
  • Arg1, arg2: Other arguments to pass
  • Returns a copy of the original function modified with the specified this value and initialization arguments
var obj = {
		name: 'lanfeng'
}
function fn(a, b) {
	console.log(this) 
  console.log(a+b) 
}
fn(1.2)// Point to window, 3
var f = fn.bind(obj, 1.2)
f()

Copy the code

Bind: If you don’t need to call a function immediately, but you want to change the this reference inside the function, use bind

var btn = document.querySelector('button')
// The previous usage
btn.onclick = function() {
  var that = this
	this.disabled = true
  setTimeout(function(){
  	that.disabled = false
  }, 3000)}/ / bind usage
btn.onclick = function() {
	this.disabled = true
  setTimeout(function(){
  	this.disabled = false
  }.bind(this), 3000)}Copy the code

2.3 Call apply bind summary

Similarity: Both can change the this point inside the function:

  1. Call and Apply call the function and change the this reference inside the function
  2. Call does not pass the same arguments as apply. Call passes arguments in the form arg1, arg2… Apply must be an array
  3. Bind does not call functions; it can change the this pointer inside functions

Main application scenarios:

  1. Call often does inheritance
  2. Apply is often associated with arrays, such as using mathematical objects to maximize and minimize arrays
  3. Bind does not call functions, but it also wants to change the this pointer, such as the timer’s internal this pointer

conclusion

In this article, we will share the javascript function definition, usage, this and several methods to change this reference.