为了账号安全,请及时绑定邮箱和手机立即绑定

两次打印的this值为什么不同

两次打印的this值为什么不同

慕后森 2018-10-17 10:14:49
var VElement=function(tagName,props,children){        this.tagName=tagName;        this.props=props||{};        this.children=children||{};        this.key=props?props.key:void 666;        var count=0;        console.log(this);        $.each(this.children,function(i,child){            console.log(this);        });        this.count=count;    }    var vdom=VElement('div',{'id':'container'},['最外层']);
查看完整描述

1 回答

?
茅侃侃

TA贡献1842条经验 获得超21个赞

首先你要明白this是谁调用它它就指向谁。

在循环外面的那个consolethis因为是window调用了VElement这个函数,所以this指向window。

在循环里面$.eachthis做了修改变成了this.children,所以循环里面的consolethis是指向this.children即传进来的['最外层']


查看完整回答
反对 回复 2018-11-16
  • 1 回答
  • 0 关注
  • 493 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号