简体   繁体   English

覆盖JavaScript中的“私有”功能

[英]Override “private” function in JavaScript

I'm monkey-patching some of the jQuery's Draggable code*. 我正在修补一些jQuery的Draggable代码*。

The goal is to avoid modifying the original source files and patch dynamically one of the internal functions. 目标是避免修改原始源文件并动态修补其中一个内部函数。

The function _generatePosition is declared like this: _generatePosition函数声明如下:

(function($) {

    $.widget("ui.draggable", $.ui.mouse, {
        ...
        _generatePosition: function(event) {
            ...
        }
    }
})(jQuery);

Is it possible to achieve the dynamic replacement of it? 有可能实现动态替换吗?


*So it calculates the snapping grid relative to the top of parent element and not relative to the top of element being dragged. *因此它计算相对于父元素顶部的捕捉网格,而不是相对于被拖动元素的顶部。 See here for more details. 有关详细信息,请参见此处

You can manipulate individual instances: 您可以操纵单个实例:

.draggable().data("draggable")._generatePosition = function() {};

Or modify the prototype, affecting all instances: 或修改原型,影响所有实例:

$.ui.draggable.prototype._generatePosition = function() {};

Edit for below comments: It seems you can edit these (after the last widget re-write), but I would still steer clear. 编辑以下评论:似乎你可以编辑这些(在最后一个小部件重写之后),但我仍然会明确指出。 Here's an example of the base method , you can modify from there if you wish, but keep in mind this can and probably will break in a future release. 这是基本方法的一个示例,如果您愿意,可以从那里进行修改,但请记住,这可以并且可能会在将来的版本中中断 Also any "inheritors" of the widget won't pick up these changes, not sure if that's an issue. 此外,窗口小部件的任何“继承者”都不会接收这些更改,也不确定这是否是一个问题。


As for the reason, to deny you access isn't the reason really (not in this case). 至于原因,拒绝你的访问并不是真正的原因(不是在这种情况下)。 In library cases like this it's more to be clean than deny you access, or because the library may want to change architecture later, and still break as few people as possible when they do so...letting you only access the "public" members of their code gives the authors more flexibility in changing anything that's "private". 在这样的图书馆案例中,它更干净而不是拒绝您访问, 或者因为图书馆可能希望稍后更改架构,并且当他们这样做时仍然尽可能少地打破...让您只访问“公共”成员他们的代码使作者能够更灵活地改变任何“私人”的东西。

Case in point: jQuery UI 1.8 moved a lot of code into the position utility , allowing a lot of private code cleanup that you didn't see happen, since it was all private before this allowed a fairly big optimization/code reduction without breaking people left and right. 一个很好的例子:jQuery UI 1.8将大量代码移动到位置实用程序中 ,允许很多私有代码清理,你没有看到发生,因为它之前都是私有的,允许在不破坏人的情况下进行相当大的优化/代码减少左和右。

You can actually modify these, but only on a per-element basic as far as I know. 你可以实际修改这些,但据我所知,只能在每个元素的基础上修改。 But you could easily create your own $.fn.draggable wrapper, and just call the original wrapper and run this: draggableElement.data('draggable')._generatePosition = fn 但是你可以轻松创建自己的$ .fn.draggable包装器,只需调用原始包装器并运行它: draggableElement.data('draggable')._generatePosition = fn

  • As Jörn Zaefferer pointed out, you could also modify the draggable prototype, by using $.ui.draggable.prototype._generatePosition = fn 正如JörnZaefferer指出的那样,你也可以使用$.ui.draggable.prototype._generatePosition = fn来修改可拖动的原型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM