JS中的this指向
来源:互联网 发布:软件项目任务书 编辑:程序博客网 时间:2024/05/24 06:20
1、 普通函数: this指向 window
function foo() { function fn() { console.log(this); } console.log(this); // window 只用函数的调用方法来判断this的指向 } foo();
2、构造函数: this指向实例化对象
function Parent(name) { this.name = name; console.log(this); } var p1 = new Parent('zhangsan'); var p2 = new Parent('lisi');
3、方法调用 :this指向函数的调用者
function fun(val) { console.log(val); console.log(this); console.log(this.age); console.log(this === obj); } var obj = {age: '18'}; obj.say = fun; fun(); obj.say();
4、call和apply方法
var name = 'global'; var obj = { name: 'obj', dose: function () { this.name = 'dose';//当前的this指向obj(将obj.name的值改成dose) return function () { return this.name;//当前的this指向window } } }; console.log(obj.dose()());//global console.log(obj.dose().call(obj));//dose
阅读全文
0 0
- js中的this指向
- JS中的this指向
- JS中的this指向
- JS中的this指向相关
- js中的this指向问题
- js中的this指向和改变this指向的方法
- js中的this指向问题及解决方案
- 20.js中的this指向对象
- 如何理解JS中的this指向问题
- Js中的this指向问题(2)
- 彻底弄懂js中的this指向
- JS-箭头函数中的this的指向
- JS函数中的this指向问题
- 关于js中的this的指向问题
- JS this的指向
- js this指向问题
- js this指向问题
- js的this指向
- Hybrid App (一)
- 使用线程池插入数据报Could not open JDBC Connection for transaction 异常
- bg fg jobs的简单操作
- Codis源码解析——Jodis
- JS-面向对象
- JS中的this指向
- Cmake使用例程
- MYIR-ZYNQ7000系列-zturn教程(1)-从新建工程到下载bit文件
- 185. Department Top Three Salaries
- 设计模式--单例模式(Singleton)
- 全志R16平台tinav2.1系统下调通rtl8188eu(草稿)
- jdbc总结
- ArrayList源码分析(1.7.0_80)
- IAR的const,变量指定绝对地址,函数指定存取区域