假设我有以下插件,包含在立即调用的函数表达式中:
$.fn.someFunc = function(tags, options) {
// Containing element
$self = $(this);
opts = $.extend({}, $.fn.someFunc.defaults, options);
// More code here
return $self;
}
$.fn.someFunc.defaults = {
property: { prop: value, prop2: value2 },
anotherProp : { prop: value, prop2: value2 }
}
如何让我的数组 $.fn.someFunc.defaults
访问我的 $self
变量?假设我想将 $.fn.someFunc.defaults.property.prop
的默认值设置为依赖于插件已提供访问权限的元素的某些固有属性的值?如:
$.fn.someFunc.defaults = {
property: { prop: $self.width(), prop2: value2 }, // This currently does not work
anotherProp : { prop: value, prop2: value2 }
}
当前尝试这样做会导致 ReferenceError: $self is not defined
错误。有什么想法吗?
最佳答案
你不能这样做。因为 $self
是 someFunc
的局部变量,所以您需要将它传递给 defaultoptions
。
试试这个:
将默认选项设置为函数
$.fn.someFunc.defaults = function(){
return { property: { prop: this.width(), prop2: value2 }, // This currently does not work
anotherProp : { prop: value, prop2: value2 }};
}
关于javascript - 我怎样才能让我的默认属性数组访问我的插件中传递的 jQuery 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25469268/