道格拉斯·克罗克福德(Douglas Crockford)的“Javascript:The Good Parts”第5.
Douglas Crockford's “Javascript: The Good Parts” Chapter 5.5
这与这个问题非常相似
我的问题更多是关于示例代码。
法典:
var eventuality = function (that) {
var registry = {};
that.fire = function (event) {
// Fire an event on an object. The event can be either
// a string containing the name of the event or an
// object containing a type property containing the
// name of the event. Handlers registered by the 'on'
// method that match the event name will be invoked.
var array,
func,
handler,
i,
type = typeof event === 'string' ?
event : event.type;
// If an array of handlers exist for this event, then
// loop through it and execute the handlers in order.
if (registry.hasOwnProperty(type)) {
array = registry[type];
for (i = 0; i < array.length; i += 1) {
handler = array[i];
// A handler record contains a method and an optional
// array of parameters. If the method is a name, look
// up the function.
func = handler.method;
if (typeof func === 'string') {
func = this[func];
}
// Invoke a handler. If the record contained
// parameters, then pass them. Otherwise, pass the
// event object.
func.apply(this,
handler.parameters || [event]);
}
}
return this;
};
that.on = function (type, method, parameters) {
// Register an event. Make a handler record. Put it
// in a handler array, making one if it doesn't yet
// exist for this type.
var handler = {
method: method,
parameters: parameters
};
if (registry.hasOwnProperty(type)) {
registry[type].push(handler);
} else {
registry[type] = [handler];
}
return this;
};
return that;
}
在代码中,我不明白这一行,func = handler.method;
。
这怎么能工作?我的意思是handler.method应该是未定义的,对吧?
谢谢
这里
定义了handler.method
(当然它必须作为参数传入):
that.on = function (type, method, parameters) {
var handler = {
method: method, // defines the method property of the handler object
parameters: parameters // defines the parameters property of the handler object
};
阅读 MDN 上的使用对象 - .method
引用handler
对象的 method
属性
handler
是存储在registry
中的对象:
array = registry[type];
for (i = 0; i < array.length; i += 1) {
handler = array[i];
它是在that.on
方法中添加的:
var handler = {
method: method,
parameters: parameters
};
if (registry.hasOwnProperty(type)) {
registry[type].push(handler);
} else {
registry[type] = [handler];
}
它显然具有method
属性。
相关文章:
- JavaScript Get the child id
- Javascript and the DOM issues
- javascript onclick, onload, onmouseenter -- the basics
- the meaning of jQuery javascript: Array.prototype.slice.call
- 任何 JavaScript 库的性能都可以像 Cut The Rope JavaScript 实现一样好吗?
- “Javascript: The Good Parts”中的闭包示例
- pjax losing the rails javascript
- 道格拉斯·克罗克福德(Douglas Crockford)的“Javascript:The Good Parts”第5.
- Javascript - Jquery on the fly id retrieving
- JavaScript UI, requirejs and the Flash Of Unstyled Content
- Javascript explain the use of 'this'
- Catch the window.open from javascript
- JavaScript: The Good Parts - chapter 8, function.apply()
- Javascript Objects and the DOM
- 如何在忽略文章(a,an,the)的情况下对javascript数组进行排序
- Javascript "return this" meet the "return&quo
- parsing a url for the id - Javascript
- Vaadin Widget does not run the javascript?
- w3c dom and the javascript
- The javascript/nodejs concept