作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
它与此非常相似question
我的问题更多是关于示例代码。
代码:
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
};
Have a read of Working with Objects on the MDN - .method
引用 handler
对象的 method
属性
关于javascript - 道格拉斯克罗克福德的 “Javascript: The Good Parts” 第 5.5 章,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10417540/
我是一名优秀的程序员,十分优秀!