函数定义方式
1 function 调用在前和在后无影响
function ***() {
}
2 函数表达式(匿名函数) 需要先定义再调用
var fn = function() { }
3 new Function()
// Function里面参数都是字符串格式
var f = new Function('a','b','console.log(a+b)');
f(2,5);
f.index = 10;
console.log(f.__proto__ === Function.prototype); //true
console.log(f instanceof Object);
console.log(f.index);
所有函数都是Function的实例,函数也是对象
Function ---------> Function.prototype
函数的调用方式和this
1 普通函数调用
普通函数调用 则函数中this === window
function fn() {
console.log(this); //
console.log('hello everyone');
}
fn(); // fn.call() 普通函数调用 则函数中this === window
2 对象调用
对象调用 函数中this === 该对象
var obj = {
sayHello: function() {
console.log(this);
console.log('hi');
}
}
obj.sayHello(); // 对象调用 函数中this === 该对象
3 构造函数调用
构造函数调用 this === 实例对象
function Student() {
}
new Student(); // 构造函数调用 this === 实例对象
4 事件触发的函数
通过事件触发 this指的是绑定事件对象
btn.onclick = fun // 通过事件触发 this指的是绑定事件对象
5 定时器函数
定时器函数,函数中this === window
setInterval(function() {
console.log(this); // window
},1000);
6 立即执行函数
立即执行函数中this === window
(function() {
console.log(this);//立即执行函数中this === window
console.log(11);
})();