【发布时间】:2016-02-19 20:04:16
【问题描述】:
(JS、jQuery 和 jqTree 的新手)
我正在尝试覆盖一个方法 (JqTreeWidget.prototype.openNode) 从一个 .js 文件 (tree.jquery.js) 在另一个(我的拥有custom.js)。
我读过to override a js method in general, I just need to redefine it。因此,我尝试在方法上执行此操作,我认为我无法访问具有原始方法的变量 (JqTreeWidget)。我认为挑战在于原始方法位于tree.jquery.js(源)中,它与我想要进行覆盖的其他custom.js 文件分开。
这个问题的目标是让我在我的custom.js 中写something like this(<reference to JqTreeWidget.prototype.openNode> 将是这个问题的答案):
var originalMethod = <reference to JqTreeWidget.prototype.openNode>;
// Override of originalMethod
<reference to JqTreeWidget.prototype.openNode> = function( node, slide ){
// my code I want to happen 1st here
changeAncestorHeightRecursively( node, true);
// my code is done, and now I'm ready to call the original method
originalMethod.call( this, node, slide );
}
我认为这将是进行覆盖的最非侵入性方式,而无需实际侵入 tree.jquery.js 源。
请在http://codepen.io/cellepo/pen/LGoaQx 上查看我的custom.js
单独的源 tree.jquery.js 是在该 codepen 的 JS 设置中外部添加的。
我怎样才能(从我的custom.js 文件中)访问源文件(tree.jquery.js)中的JqTreeWidget 变量? 有可能吗? JqTreeWidget 不在tree.jquery.js 范围之外,还是不是全局变量?我希望treeContainer.tree.prototype 会得到它,但到目前为止我还没有运气......
谢谢!
【问题讨论】:
-
上方的 CodePen,forked & updated with Accepted Answer here.
标签: javascript jquery jqtree