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; } ?>javascript - What is the meaning of 'function(event)' in js - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - What is the meaning of 'function(event)' in js - Stack Overflow

programmeradmin1浏览0评论

I don't know the meaning of the sentence 'function(event)'

Event.add(apple,'click',function(event) {
    Event.stopPropagation(event);
});

Isn't the argument 'event' is the unique keyword of javascript?

Is keyword can be an argument of some function?

I understand the meaning of below code :

function(test) {
alert(test);
}

But I don't understand this one :

function(event)...

Can any one give an explanation about that to me?

I don't know the meaning of the sentence 'function(event)'

Event.add(apple,'click',function(event) {
    Event.stopPropagation(event);
});

Isn't the argument 'event' is the unique keyword of javascript?

Is keyword can be an argument of some function?

I understand the meaning of below code :

function(test) {
alert(test);
}

But I don't understand this one :

function(event)...

Can any one give an explanation about that to me?

Share Improve this question asked Aug 31, 2014 at 17:20 goJsongoJson 2332 gold badges6 silver badges13 bronze badges 2
  • 1 As you can see here: developer.mozilla/en-US/docs/Web/JavaScript/Reference/… event is not a reserved word in javascript grammar. – pfooti Commented Aug 31, 2014 at 17:27
  • In this case, function sent into Event.add as its third argument is so-called event handler - it will be called when the corresponding event (click here) occurs, and the event object itself will be used as its first param. From purely technical point-of-view, it doesn't matter what name is used. But for readability reasons, using event here matters much. – raina77ow Commented Aug 31, 2014 at 17:28
Add a ment  | 

5 Answers 5

Reset to default 6

The event object is always passed to the handler and contains a lot of useful information what has happened.

Different types of events provide different properties. For example, the onclick event object contains:

  • event.target - the reference to clicked element. IE uses event.srcElement instead.
  • event.clientX / event.clientY - coordinates of the pointer at the moment of click.

Information about which button was clicked and other properties. Please visit this link.
It answers all your questions very simply

Source http://javascript.info/tutorial/obtaining-event-object

Example:

Like if in HTML you have assigned an event like this

<button onclick="alert(event)">See the event</button>

then

function alert(event) {
    // event.type contains whether this event was invoked in the result of a click etc
    // event.target would contain the reference to the element which invoked this method/event
}

It is an anonymous function, that is a function without name, that sends the event object. That object contains information about the event itself. It is always passed as first object/variable.

It is defining an anonymous function object. This code:

function foo(bar) { ... }

Is functionally similar to:

var foo = function (bar) { ... };

(Except that in the first case the name foo and the creation and assignment of the function object are hoisted to the top of the scope, while in the second case only the name foo is hoisted; foo won't hold the function until the assignment executes.)

Effectively, the code you posted is calling Event.add() and passing a function to it as the third argument, but rather than declaring the function ahead of time it is creating the function object inline.

Another way to write the code block in your question is:

function handler(event) {
    Event.stopPropagation(event);
}

Event.add(apple, 'click', handler);

Except that the code in your question does not introduce the handler name.


Note that there is no such method Event.stopPropagation(). However, the event object will have a stopPropagation(), so the capital E was probably a typo. It's likely that the intent was to use function (event) { event.stopPropagation(); }.

event is just a variable that's passed to event listener functions such as Event.add, element.on. It's not reserved (although Event is, which is why you can use Event.add), and you can name it whatever you like.

The event argument is used to pass information about the event that has happened (the click on apple in this case), which can be used to retrieve data about the event or manipulate it.


function(){...} is an anonymous function, which means that you don't need to name it, you can just declare it inline, and the function will be passed as an argument, as if you said

function foo (event) {
    ...
}
Event.add(apple, "click", foo);

but you don't need to declare it before hand. It does e at the disadvantage of not being duplicable, for instance when clearing an event handler.

Look at the event variable and you will all understand :)

function (event) {
  console.log({ event });
}
发布评论

评论列表(0)

  1. 暂无评论