Some unimportant nonsense

With my unremitting efforts, I finally turned some data in my creator center into a positive number.

Hi big rush

Some important nonsense

But I seemed a little nervous last time. Sorry, I forgot to introduce myself.

Hello everyone, I am the captain of the IT Army.

The text start

When I was visiting the Nuggets, I saw an article by the big handsome ape. Put some of the original code:

function throttle(func, delay) {
    var last = 0;
    return function () {
        var now = Date.now();
        if (now >= delay + last) {
            func.apply(this.arguments);
            last = now;
        } else {
            console.log("Time difference from last call is not enough."); }}}function resize(e) {
    console.log("Window size changed");
}
window.addEventListener('resize', throttle(resize, 300));
Copy the code

Func. Apply (this, arguments); . Not really, to be honest. So I searched my 1,024KB brain for relevant knowledge memories. Apply looks like you can change the reference to this and arguments looks like an array of classes…… Heh heh heh, doesn’t seem to help much.

In that case, let’s take it one step at a time.

About this

I vaguely remember a big man saying to me,

This only knows what object it points to at run time

Without more words, try one try.

var name = 'a', age = 0, type = 'A'
function fn () {
    console.log( this.name,'Age is'.this.age, 'Blood type is.'.this.type );
}
fn() // Expected output: a Age is 0 blood type is A
Copy the code

This is fn called in the global scope, so this will point to the global scope object Window.

var name = 'a', age = 0, type = 'A'
function fn () {
    console.log( this.name,'Age is'.this.age, 'Blood type is.'.this.type );
}
fn()
// Expected output: a Age is 0 blood type is A
var x = {
    name: 'XMAN'.age: 24.type: this.type,
    fn: fn
}
var y = {
    name: 'YMAN'.age: 25.type: 'B'.fn: fn
}
  
x.fn() // Expected Output: XMAN age 24 blood type A
y.fn() // Expected output: YMAN age 25 blood type B
Copy the code

They’re all calling the same method, and the reason they console is different is because this points to a different object. When x.fein () is executed, the calling environment is in x, so this refers to object X; Similarly, when we call y.fein (), this points to object y. Of course, there are many other cases of this that you can study for yourself. In fact, I am afraid that the whole more bad will mislead the flowers of the motherland

Apply, bind, call

Let’s look at the description of MDN

The apply() method calls a function with a given this value and arguments in the form of an array (or array-like object).

The bind() method creates a new function, and when bind() is called, this of the new function is specified as the first argument to bind(), and the remaining arguments will be used as arguments to the new function.

The call() method calls a function with a specified this value and one or more arguments given separately.

At first glance, these three seem to be similar, as if they can all modify this to point to an object, but there are definitely some differences. Try one’s hand

  var name = 'a', age = 0, type = 'A'
  function fn () {
    console.log( this.name,'Age is'.this.age, 'Blood type is.'.this.type );
  }
  var y = {
    name: 'YMAN'.age: 25.type: 'B'.fn: fn
  }
  fn() // Expected output: a Age is 0 blood type is A
  fn.call(y) // Expected output: YMAN age 25 blood type B
  fn.apply(y) // Expected output: YMAN age 25 blood type B
  fn.bind(y)() // Expected output: YMAN age 25 blood type B
Copy the code

You can see that the bind method has an extra () because bind creates a new function.

Try again:

  var name = 'a', age = 0, type = 'A'
  var x = {
    name: 'XMAN'.age: 24.type: this.type
  }
  function todo (para1, para2) {
    console.log(this.name,'Age is :'.this.age, 'The blood type is :'.this.type, 'interests:, para1, para2)
  }
  todo('basketball'.'football')            // A Age: 0 Blood type: A Hobbies: basketball and football
  todo.call(x, 'basketball'.'football')    // XMAN age: 24 Blood type: A Hobbies: basketball and football
  todo.call(x, ['basketball'.'football'])  // XMAN age: 24 blood type: A Hobby: [" basketball ", "football "] undefined
  todo.apply(x, 'basketball'.'football')   The second argument to apply must be an array or array-like object
  todo.apply(x, ['basketball'.'football']) // XMAN age: 24 Blood type: A Hobbies: basketball and football
  todo.bind(x, 'basketball'.'football') ()// XMAN age: 24 Blood type: A Hobbies: basketball and football
Copy the code

The call() method is similar to apply() except that call() takes a list of parameters, while apply() takes an array of parameters.

About the arguments

Again, let’s see what MDN says:

Arguments is an array-like object corresponding to the arguments passed to the function.

Here’s an example:

  function testArguments() {
    console.log(arguments);
    // Expected Output: ƒ (3) ["a", "B "," C ", callee: ƒ, Symbol(symbol.iterator): ƒ]
    
    console.log('arguments.length :>> '.arguments.length);
    // expected output: arguments.length :>> 3
    
    console.log(arguments[0])
    // expected output: a
    
    arguments.push('d')
    // expected output: arguments.push is not a function
  }
  testArguments('a'.'b'.'c')
Copy the code

Arguments is an array of arguments to a function (length). You can also use an index to get the corresponding data, but it does not have a method like push. Can think of is, after all, is the others Array, how to give the arguments.

conclusion

Func. Apply (this, arguments); This line of code. So the answer is……

The make-up of the gas that makes you okukyu だ agitrile (wait) doesn’t seem to come out yet, let me think more……

Here are some of my own comments: He changed the reference of this by applying, where this should refer to window; Arguments can be passed to func for an indefinite number of arguments. So you don’t really care what function it is and what parameters it takes. Japan ほ Japan, oh ~ Prime minister, I realize but not completely……

The last

Write an article for the second time, if have what didn’t write correct ask big guy to point out. Of course, I wouldn’t necessarily change it.