时间:2021-07-01 10:21:17 帮助过:11人阅读
函数的调用和this的指向
1.普通函数调用 this 指向 window
function fn() {
console.log(this);
}
window.fn();2.方法调用 this 指向 调用该方法的对象
var obj = {
fun: function () {
console.log(this);
}
}
obj.fun();var gf = {
name : "tangwei",
bar : "c++",
sayWhat : function() {
console.log(this.name + "said:love you forever");
}
}btn.onclick = function () {
console.log(this);
}setInterval(function() {
console.log(this);
}, 1000);总结:函数内部的this,是由函数调用的时候来确定其指向的。
本篇文章到这里就已经全部结束了,更多其他精彩内容可以关注PHP中文网的JavaScript教程视频栏目!
以上就是JavaScript中函数的调用和this的指向介绍(代码)的详细内容,更多请关注Gxl网其它相关文章!