js中的this
来源:互联网 发布:科科 网络用语 编辑:程序博客网 时间:2024/06/05 21:55
1、在函数被直接调用时,this是指全局对象window
console.log(this); //[object Window]function f(){ console.log(this);}f(); //[object Window]
2、在函数内部嵌套的函数,该内部函数的this依然是全局变量,并不是其父函数
function f1(){ function f2(){ console.log(this); } f2();}f1(); //[object Window]
3、setInterval、setTimeout两个方法执行的函数this也指全局变量
setTimeout(function(){ console.log(this);},200); //[object Window]
4、构造函数,使用该函数实例化一个新对象,此时,函数中的this指这个新对象
function Person(name){ this.name = name;}Person.prototype.printName = function(){ console.log(this.name);}var p1 = new Person('XX');var p2 = new Person('GG');p1.printName(); //XXp2.printName(); //GG
5、函数作为对象的方法调用时,this指向该对象
var obj = { name : 'AC', fun : function(){ console.log(this.name); }};obj.fun(); //AC
阅读全文
0 0
- js中的this
- js中的this关键字
- 关于js 中的 this
- js中的this详解
- js 中的this
- JS中的this
- JS中的this用法
- js中的this基础
- JS中的this关键字
- 在js中的this
- 【JS】JavaScript中的this
- js中的this
- js 中的this
- js中的this
- JS中的this理解
- js中的this
- JS中的this
- js中的this指针
- #java#将一个数插入已经排序的数组
- 为什么100以内的数猜七次
- 数字货币与区块链关键技术(新浪分享PPT全文)
- HTML框架
- 三子棋-C语言
- js中的this
- 【机器学习理论】分类问题中常用的性能评估指标
- LintCode_003_统计数字
- 重载构造方法的应用
- C语言求一元二次函数的根
- java算法之Sqrt of x
- Python之文件使用
- zabbix3.0中文显示乱码的问题
- 系统