我正在尝试理解一个chrome扩展,它显示html页面中任何选定元素的xpath。 我遇到了这个我无法理解的代码。
'use strict';
// Extension namespace.
var xh = xh || {};
xh.bind = function(object, method) {
return function() {
return method.apply(object, arguments);
};
};
////////////////////////////////////////////////////////////////////////////////
// xh.Bar class definition
xh.Bar = function() {
this.boundShowBar_ = xh.bind(this, this.showBar_);
this.boundHandleRequest_ = xh.bind(this, this.handleRequest_);
this.boundMouseMove_ = xh.bind(this, this.mouseMove_);
this.boundKeyDown_ = xh.bind(this, this.keyDown_);
chrome.extension.onMessage.addListener(this.boundHandleRequest_);
this.barFrame_ = document.createElement('iframe');
this.barFrame_.src = chrome.extension.getURL('bar.html');
this.barFrame_.id = 'xh-bar';
this.barFrame_.className = 'top';
this.barFrame_.style.height = '0';
// Temporarily make bar 'hidden' and add it to the DOM. Once the bar's html
// has loaded, it will send us a message with its height, at which point we'll
// set this.barHeightInPx_, remove it from the DOM, and make it 'visible'.
// We'll add it back to the DOM on the first bar request.
//this.barFrame_.style.visibility = 'hidden';
document.body.appendChild(this.barFrame_);
document.addEventListener('keydown', this.boundKeyDown_);
};
xh.Bar.prototype.active_ = false;
xh.Bar.prototype.barFrame_ = null;
xh.Bar.prototype.barHeightInPx_ = 0;
xh.Bar.prototype.currEl_ = null;
xh.Bar.prototype.boundHandleRequest_ = null;
xh.Bar.prototype.boundMouseMove_ = null;
xh.Bar.prototype.boundKeyDown_ = null;
绑定功能究竟在做什么? 调用bind时使用的方法未在代码中的任何位置定义。
答案 0 :(得分:0)
bind(thisArg : Object, [param1 : Object, [param2 : Object, [...]]]) : Function
返回一个新函数,当调用它时,它将等于thisArg,第一个参数等于param1,第二个参数等于param2等。
我们主要使用Bind()方法调用具有显式设置的值的函数。换句话说,bind()允许我们在调用函数或方法时轻松设置将绑定到哪个特定对象。