一、函数的调用
-
普通函数
/ /内部的 this 默认指向 windowfunction fun(){ console.log(123); } fun();
-
构造函数
// 内部的 this 指向的是将来创建的实例对象function Person( name) { this.name = name; } var p1 = new Person( "zhangsan" );
-
对象方法
//内部的 this 默认指向的是调用的对象自己var o = { sayHi: function () { console.log( " haha" ); } } o. sayHi();
-
事件函数
//事件函数的内部 this 指向的是事件源document.onclick = function() { console.log("事件"); };
-
定时器、延时器的函数
//默认内部的 this 指向的是 windowsetInterval( function () { console.log( "time" ); },1000);
二、总结
ps: this 的指向是要联系执行的上下文,在调用的时候,是按照什么方式调用,指向是不一样的