te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>jquery - How do I pass a function(delegate?) as a parameter to another function in Javascript and then use it - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - How do I pass a function(delegate?) as a parameter to another function in Javascript and then use it - Stack Overflow

programmeradmin4浏览0评论

I want to pass a function to another function. I think functions being passed like that are call delegates? I am having a hard time finding a good explanation for this kind of thing online. Is this the right way to do this?

function getCellContentByColumnIndex = function(row, index) {
    return $(row.children().get(index)).text();
}
function naturalSort(a, b, func) {
    //...
    var $a = func(a);
    var $b = func(b);
    //...
}

//usage  
naturalSort(x, y, getCellContentByColumnIndex);

I want to pass a function to another function. I think functions being passed like that are call delegates? I am having a hard time finding a good explanation for this kind of thing online. Is this the right way to do this?

function getCellContentByColumnIndex = function(row, index) {
    return $(row.children().get(index)).text();
}
function naturalSort(a, b, func) {
    //...
    var $a = func(a);
    var $b = func(b);
    //...
}

//usage  
naturalSort(x, y, getCellContentByColumnIndex);
Share Improve this question asked Jun 23, 2012 at 14:12 BenjaminBenjamin 3,2347 gold badges40 silver badges59 bronze badges 0
Add a ment  | 

4 Answers 4

Reset to default 7

Your code:

function getCellContentByColumnIndex = function(row, index) {
    return $(row.children().get(index)).text(); 
}

Is a syntax error. The following is a function declaration:

functon foo() {}

And this is a function expression:

var foo = function(){}

And this is a named function expression:

var foo = function bar(){}

There are a number of answers here on the differences, there is a detailed explanation in the article Named function expressions demystified which also covers many other aspects of function declarations and expressions.

The term "anonymous function" is jargon for a function expression that has no name and isn't assigned to anything, e.g.

someFn( function(){...} )

where someFn is called and passed a function that has no name. It may be assigned a name in someFn, or not. Ic could just be referenced as arguments[0].

Passing a function is not delegating, that is jargon for the practice of putting a listener on a parent element and catching bubbling events, it is preferred in cases where it can replace say a click listener on every cell in a table with a single listener on the table.

Anyhow, passing a function is just like passing any other object:

function foo(){
  alert('foo');
}

function callIt(fn) {
  fn();
}

callIt(foo);  // 'foo'

In the above, foo is passed to callIt and assigned to the local variable fn, and is then called.

You pass functions around as variables like so:

var getCellContentByColumnIndex = function(row, index) {
    return $(row.children().get(index)).text();
}

function naturalSort(a, b, func) {
    //...
    var $a = func(a);
    var $b = func(b);
    //...
}

//usage  
naturalSort(x, y, getCellContentByColumnIndex);

This is called using anonymous functions.

Anonymous functions..

var getCellContentByColumnIndex = function(row, index) {
    return $(row.children().get(index)).text();
}

will work..rest stuff of calling is already perfect in your code..:)

In JavaScript, functions are treated as first class citizens which mean you can toss them here and there like simple variables. The key is, use the FunctionName when you want to refer to function and use FunctionName() to invoke it.

this line: naturalSort(x, y, getCellContentByColumnIndex);

could have been written as

naturalSort(x, y, function (){
    return $(row.children().get(index)).text();
});

In which case it would have been called passing Anonymous Function

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论