①普通的函数调用是经过给函数名或者变量名添加()的方式执行。函数
function fn(){ console.log(1); }; fn();
②构造函数,经过new关键字进行调用(也能够使用()调用,只是功能不全)this
function Student(name){ this.name = name; }; var s1 = new Student("li");
③对象中的方法,经过对象打点调用函数,而后加括号();
内部的 this 默认指向的是调用的对象本身code
var Student = { name:"lu", message: function(){ console.log(this.name + " is a student"); } } Student.message();
④事件函数,不须要加特殊符号,只要事件被触发,会自动执行函数;
内部的 this 默认指向的是事件源对象
document.onclick = function(){ console.log("hello"); }
⑤定时器、延时器中的函数,不须要加特殊符号,只要执行后,在规定的时间自动执行;
内部的 this 默认指向是window事件
setInterval(function(){ console.log(1); },1000);
this的指向是须要联系执行上下文,在调用的时候,是按照什么方式调用,指向是不同的原型
调用方式 | 非严格模式 | 备注 |
---|---|---|
普通函数调用 | window | 严格模式下是 undefined |
构造函数调用 | 实例对象 | 原型方法中 this 也是实例对象 |
对象方法调用 | 该方法所属对象 | 紧挨着的对象 |
定时器函数 | window | |
事件绑定方法 | 绑定事件对象 |