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

prototypejs - How to pass a parameter to an inline javascript function - Stack Overflow

programmeradmin2浏览0评论

I'm trying to pass story_id into the innermost inline function, but its always 0 even though its not 0 in the outer function. How do I pass this value in?

function activateSearch(){
  if($('story_filter')) Event.observe('story_filter', 'keyup',
    function(event) {
      $('stories_table').select(".story").each(
        function(story) {
          story_id = story.id.split('_')[1];
          story.select('.tableCell', '.indexCardContent').each(
            function(task, story_id) {
              hideStoryRow(story_id);
              task_filter = new RegExp($F('story_filter'), "i");
              if (task.innerHTML.match( task_filter ))
              {
                  showStoryRow(story_id);
                  throw $break;
              }
            }
          );
        }
      );
    }
  );
}

I'm trying to pass story_id into the innermost inline function, but its always 0 even though its not 0 in the outer function. How do I pass this value in?

function activateSearch(){
  if($('story_filter')) Event.observe('story_filter', 'keyup',
    function(event) {
      $('stories_table').select(".story").each(
        function(story) {
          story_id = story.id.split('_')[1];
          story.select('.tableCell', '.indexCardContent').each(
            function(task, story_id) {
              hideStoryRow(story_id);
              task_filter = new RegExp($F('story_filter'), "i");
              if (task.innerHTML.match( task_filter ))
              {
                  showStoryRow(story_id);
                  throw $break;
              }
            }
          );
        }
      );
    }
  );
}
Share Improve this question edited Dec 29, 2011 at 15:51 Rob W 349k87 gold badges807 silver badges682 bronze badges asked Apr 23, 2009 at 13:57 BrianBrian 4,9303 gold badges34 silver badges55 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 7

All you have to do to make this work is change this line:

    function(task, story_id) {

to this:

    function(task) {

You don't have to pass story_id anywhere -- it's already in scope in the inner function!

You don't. The each method takes the function definition and then calls that function for each element in the Array. You'd need to write your own custom each method if you wanted to use the inline function definition.

For both readability and functionality, I would break out the inner most inline function and then for each element in the list, call out to your function.

you've got a scoping issue.

quick fix: define story_id as a global.

var story_id;

function activateSearch() { ....
发布评论

评论列表(0)

  1. 暂无评论