JS function calling mode and this analysis


Function call and this

① Ordinary function calls are executed by adding () to the function name or variable name.

function fn(){ 

② Constructor, called through the new keyword (you can also call with (), but the function is incomplete)

function Student(name){
   this.name = name;
var s1 = new Student("li");

③ Object, call the function through object punctuation, and then add parentheses ();
The internal this defaults to the calling object itself

var Student = {
   message: function(){
       console.log(this.name + " is a student");

④ The event function does not need to add special symbols. As long as the event is triggered, the function will be executed automatically;
The default source for this event is this

document.onclick = function(){

⑤ The functions in timer and delay device do not need to add special symbols, as long as they are executed automatically at the specified time;
The default point of the internal this is window


The point of this needs to be related to the execution context. When calling, the method of calling is different

Call mode Non strict mode remarks
Ordinary function call window Undefined in strict mode
constructor call Instance object This is also an instance object in the prototype method
Object method call The object to which the method belongs Object next to
Timer Functions window
Event binding method Bind event object