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('p',{'id':'container'},['最外层']);
Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
首先你要明白
this是谁调用它它就指向谁。在循环外面的那个
console的this因为是window调用了VElement这个函数,所以this指向window。在循环里面
$.each对this做了修改变成了this.children,所以循环里面的console的this是指向this.children即传进来的['最外层']//$.each 对于遍历函数的 this 指向做了修改。
$.each对于遍历函数的this指向做了修改. 你可以这么书写: