Anonymous functions do recursion, using arguments.callee

let i=0
(function(){
	i++
    if(i<3) {arguments.callee()
    }
})()
Copy the code

Three ways to execute a function

Common functionThis calls the window object. This refers to the window object

Called as a methodThe “this” method in the object refers to the object, and external variables are equivalent to adding properties to the window object.

obj.say=test
obj.say()   // The output this refers to the obj object


test=obj.say
test()    // Outputs this to the window object
Copy the code

Called as a constructorThis in the: constructor refers to the constructor, which is the object created by the new keyword

Person() // This refers to the window object

Function context invocation pattern

None of the three basic invocation modes of a function (basic function, method, constructor) can change the direction of this

If we want to change the direction of this, we use the context call pattern (call(),apply(),bind()).

call()

Syntax: function name. call(this, argument 1, argument 2…)

apply()

Function name. apply(the object to which this points, an array or a pseudo-array) Apply () takes only two arguments

bind()

Syntax: function name. bind(new pointer to this), returns a function that will not be called itself

Call (),apply(), and bind() come from function. prototype; all functions can be called to change the orientation of this

If the parameter passed is not an object

  • Pass (122) this to the Number object
  • Pass (‘dsad’) this to a String
  • Either false or true is passed in. This points to a Boolean object
  • Undefined, null, null this points to the window