JS中this
来源:互联网 发布:php上传图片压缩大小 编辑:程序博客网 时间:2024/04/28 21:18
1 全局函数
alert(this);//[object, Window]
2
function test(){ alert(this);};//undefined
3 对象中的方法
var obj = { name: 'obj对象', test: function() { alert(this); //object Object alert(this.name); //obj对象 }};obj.test();
结论: js中this永远指向属性或方法所属的对象
4 闭包中this
闭包函数属于全局
4.1
var name = 'this指向window对象';var obj = { test: function() { return function(){ return this.name; }; }};alert(obj.test()); //function(){return this.name};alert(obj.test()()); //this指向window对象
4.2 _this代替this
var name = 'this指向window对象';var obj = { name: '_this指向局域变量', test: function() { var _this = this; return function(){ return _this.name; }; }};alert(obj.test()()); //_this指向局域变量
0 0
- js中this
- js 中this
- js中this使用方法
- JS中this
- JS中this
- JS中this指向
- js中this理解
- js中this用法
- 浅谈JS中this
- js 中this陷阱
- js中this
- Js中this用法
- js中this 学习
- js中this详解
- js中this关键字
- js中this的总结
- js 中 this的探讨
- js中this的总结
- min-height和max-height
- [经验] 基于Xmodem的STM32的IAP升级【转】
- AD7175-2使用问题记录
- OC学习:NSURL讲解和OC中图片下载
- leetcode-67-Add Binary
- JS中this
- SDNU 1331.Kick Veges' Ass【SDNU2015暑期集训队测验I】【二分法】【8月3】
- hdu2421
- Watches OpenCart 主题模板 ABC-0088
- Unity3d
- win8下安装ubuntu12.04的悲催历程
- HDU 4352 XHXJ's LIS 数位状压DP
- 206 Reverse Linked List
- 软件开发中常遇到的问题