js之this
来源:互联网 发布:淘宝评价规则 编辑:程序博客网 时间:2024/05/17 23:47
this所指向的对象是由this所在的执行域决定的,而不是由this所在的定义域决定
对象的方法调用,this指向当前对象:obj.fn()、ojb['fn']()。 //.fn()、['fn']前的对象
var name = "this=window";var o = { name: "this=o", f: function(){ return this; }};var o1 ={ name: "this=o1", f: o.f}var a = o1.f();console.log(a.name);//this=o1var o2 = { name:"this=o2", f:function(){ return o.f; }};var b=o2.f()();console.log(b.name);//window
a1.f(),a1的方法,所以this指向a1
o2.f()(),分开看,o2.f()返回的是o.f,o.f这里并没有执行,是一个函数。o2.f()(),函数自执行,this指向window
function f(){ return this;};var p = { name:"对象p", me:f, p1:{ name:"对象p1", me:f, p2:{ name:'对象p2', me:f } }};var who = p.p1.p2.me();console.log(who.name);//对象p2who = p.p1.me();console.log(who.name);//对象p1who = p.me();console.log(who.name);//对象p这里的都是调用方法,方法由谁调用,this指向谁()
第一个me()调用前是p2,第二个是p1,第三个是p,分别对应p2,p1,p。
1 0
- js之this 关键词
- js之this
- js之this
- js之this指针
- JS之prototype this
- js之浅谈this
- 理解JS之this进阶
- js之this的用法
- JS学习之this关键字
- JS学习之this关键字
- js学习总结之this
- js学习笔记之this
- 前端之js-this指针
- 深入理解JS之this进阶篇
- js之this,请问你是谁
- js老生常谈之this,constructor ,prototype
- js老生常谈之this,constructor ,prototype
- JS之This篇-菜鸟的福利
- UTM 用户线程模型
- javaEE
- CSS圣杯布局与双飞翼布局
- 牛客网每日打卡-Java基础-20160326
- 机器学习实战--apriori
- js之this
- bfs模板
- HDU 5651 xiaoxin juju needs help
- The type javax.servlet.ServletRequest cannot be resolved. It is indirectly referenced from required
- 计算机网络协议层
- 兰州出差记之第一篇
- 变量定义与const变量定义
- HDU 5654 xiaoxin and his watermelon candy
- Laravel学习笔记-1-环境搭建