最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Calling function of $.fn.function - Stack Overflow

programmeradmin3浏览0评论

The problem is the following. Theres is a function custom jquery function with another function inside, f.e.:

$.fn.slides = function(args){

 function foo(args){

  }

}

My question is now: How can I call the method foo.

The problem is the following. Theres is a function custom jquery function with another function inside, f.e.:

$.fn.slides = function(args){

 function foo(args){

  }

}

My question is now: How can I call the method foo.

Share Improve this question asked Mar 27, 2013 at 11:35 rakedenrakeden 1921 silver badge8 bronze badges 1
  • 2 From outside the plugin? You can't. You'd have to expose the function one way or another. – Felix Kling Commented Mar 27, 2013 at 11:37
Add a ment  | 

2 Answers 2

Reset to default 5

foo is not a method. It is a local function. There is no way to access it from outside the function in which it is defined unless you modify that function to expose it.

For example (and I do not remend creating globals, you should probably attach the function to some other object):

$.fn.slides = function(args){
   function foo(args){ }
   window.foo = foo;
}

You can't call it from outside the function, unless you return an object which has foo attached to it, something like this:

$.fn.slides = function(args){

    this.foo = function (args){

    }

    return this;
}

$('blah').slides(args).foo(args);

Inside the function you can use it as a regular function:

$.fn.slides = function(args) {

    function foo(args) {

    }

    foo(args);
}
发布评论

评论列表(0)

  1. 暂无评论