[英]Define function inside the module in javascript
I tried to define a function attachPicker can be called like:我试图定义一个函数 attachPicker 可以像这样调用:
(function(){
$.fn.tagPicker = function(source){
this.attachPicker();
}
})(jQuery);
I tried:我试过了:
(function(){
$.fn.tagPicker = function(source){
this.attachPicker();
//define attachPicker
$.fn.attachPicker = function(){
//code here
}
}
})(jQuery);
(function(){
$.fn.tagPicker = function(source){
this.attachPicker();
//define attachPicker
this.attachPicker = function(){
//code here
}
}
})(jQuery);
Both threw out两个都扔了
Uncaught TypeError: Object [object Object] has no method 'attachPicker'
If I did如果我做了
(function(){
$.fn.tagPicker = function(source){
this.attachPicker();
}
$.fn.attachPicker() = function(){ //codes here };
})(jQuery);
It worked.有效。 But I want to define attachPicker inside tagPicker so that it can access the source.但是我想在 tagPicker 中定义 attachPicker 以便它可以访问源。 Anyone can explain why the ones I tried didn't work and give me any suggestions?任何人都可以解释为什么我尝试的那些不起作用并给我任何建议? Of course I knew I can pass source as argument if defining attachPicker outside.当然,我知道如果在外部定义 attachPicker,我可以将源作为参数传递。 Thanks in advance!提前致谢!
Before using any function you should define that function在使用任何函数之前,您应该定义该函数
Try this code试试这个代码
(function(){
$.fn.tagPicker = function(source){
//define attachPicker
this.attachPicker = function(){
//code here
}
this.attachPicker();
}
})(jQuery);
You can test here: http://jsfiddle.net/s23py/ also you can call it by an object: http://jsfiddle.net/s23py/2/您可以在这里测试: http : //jsfiddle.net/s23py/也可以通过对象调用它: http : //jsfiddle.net/s23py/2/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.