javascript - 如何从 JS 中的函数访问父对象“this”?
问题描述
(function( $ ) {
var TagHolder = function(element,options){
this.property1 = options.p1;
this.id = 0;
this.init = function(){
$('a').on('click',function(e){
e.preventDefault();
var id = $(this).attr('id');
this.id = id;
});
}
}
$.fn.TagHolderProperty = function(options) {
return new TagHolder(this, options);
}
})( window.jQuery );
如何逐行访问对象this
实例,以便设置 id 属性?this.property1 = options.p1;
this.id = id;
解决方案
采用
self = this;
在您的 TagHolder 函数中,然后执行
self.id = id;
在你的初始化函数中
推荐阅读
- mysql - 损坏的 mysqld 包阻止在 ubuntu 中进行任何其他安装
- sql - 替换为空或存在内部查询逻辑以减少存储过程执行时间
- django - 如何在 DRF FilterBackend 和 Serializer 之间共享验证和模式
- arrays - 通过在 c 中迭代打印字符
- python - Pandas:仅在特定列中将空白字段替换为“Na”,并混合浮动对象和空白字符串
- arrays - jq 根据另一个属性的长度选择属性值并将该值存储在文本文件中
- python - PyGame 渲染问题——移动时边缘模糊
- windows - 从 sqlplus 运行 windows cmd 命令
- ansible - 如何将字符串列表转换为字典列表?
- soap - 工作日是否有api来关闭采购订单