简体   繁体   English

复杂原型JavaScript中的重写功能

[英]Overriding function in complicated prototype javascript

I'm a bit stuck on a problem which I can't solve, I performed investigation on internet and on this site, but I can't find the answer to my question. 我对无法解决的问题有些困惑,我在互联网和此站点上进行了调查,但是找不到我的问题的答案。

So basically I have a javascript file, which I cannot modify, so I have another javascript file which should catch the method when it is called and override it. 因此,基本上我有一个无法修改的javascript文件,因此我还有另一个javascript文件,该文件应在调用该方法时捕获该方法并覆盖该方法。

Normally I know how it works and I already done the function overriding, but I don't know how to solve this issue. 通常,我知道它是如何工作的,并且我已经完成了函数覆盖,但是我不知道如何解决此问题。

I have a very big script, but I will show just a small piece of it: 我有一个非常大的脚本,但我只会展示其中的一小部分:

Microsoft.Office.Server.Ajax.NavResizer.prototype = {
    $6: null,
    $7: null,
......
$20:function ($p0) {
    if (this.$1E) {
        $p0.preventDefault();
    } 
},
$21: function ($p0) {
    var $0 = $p0.target;
    this.$1F = ($0 === this.$A);
    if (this.$1F || $0 === this.$B) {
        this.$1E = $0;
        this.$18 = $p0.clientX;
        this.$19 = $p0.clientY;
        Sys.UI.DomEvent.removeHandler(this.$1E, 'mousedown', this.$12);
        var $1 = document.body; Sys.UI.DomEvent.addHandler($1, 'mouseup', this.$13);
        Sys.UI.DomEvent.addHandler($1, 'mousemove', this.$14);
        $1.style.cursor = (this.$1F) ? 'e-resize' : 'n-resize';
        this.$1A = this.get_$42();
        this.$1B = this.get_$43();
        $1.focus();
        Sys.UI.DomEvent.addHandler($1, 'selectstart', this.$15);
        $p0.preventDefault();
    } 
},
$22: function ($p0) {
    this.$34($p0);
    var $0 = document.body;
    Sys.UI.DomEvent.removeHandler($0, 'mouseup', this.$13);
    Sys.UI.DomEvent.removeHandler($0, 'mousemove', this.$14);
    Sys.UI.DomEvent.addHandler($0, 'selectstart', this.$15); 
    $0.style.cursor = 'default';
    Sys.UI.DomEvent.addHandler(this.$1E, 'mousedown', this.$12);
    this.$1E = null;
},
$23: function ($p0) {
    this.$34($p0);
},
$24: function ($p0) {
    this.$26();
},
....

Basically this is the part of the script: so lets say I want to override function: $22: function ($p0) in the script in another javascript file, how do i do that? 基本上,这是脚本的一部分:所以可以说我想覆盖函数: $ 22:另一个JavaScript文件中脚本中的函数($ p0) ,我该怎么做?

I would appreciate any help. 我将不胜感激任何帮助。

A small update, some good examples were provided but they are not working. 一个小的更新,提供了一些很好的示例,但是它们不起作用。 The environment where i run this sript is SharePoint, normally when I did override I used this method: 我运行此清单的环境是SharePoint,通常当我覆盖时使用了以下方法:

var oldFixRibbonAndWorkspaceDimensions = window.FixRibbonAndWorkspaceDimensions;
window.FixRibbonAndWorkspaceDimensions = function () {
    this.MyFixRibbonAndWorkspaceDimensions();
};

function MyFixRibbonAndWorkspaceDimensions(){...}

And it didn't matter when i load the script as this function was only called when the default function was called not before not after. 而且,当我加载脚本时也没关系,因为仅当默认函数被调用之前而不是之后才调用此函数。 Just in the same time. 就在同一时间。 But with the example which were provided here, the function is trying to execute on the document.ready() 但是使用此处提供的示例,该函数正在尝试在document.ready()上执行

You want to permanently override it? 您要永久覆盖它吗? Just do this: 只要这样做:

Microsoft.Office.Server.Ajax.NavResizer.prototype.$22 = function($p0) {
    // your code.
};

As long as your script is executed after the original is defined, you're good. 只要在定义原始脚本之后执行脚本,就可以了。

Old post.. but this works for me: 旧帖子..但这对我有用:

ExecuteOrDelayUntilScriptLoaded(overrideNavResizer, "NavResizer.js");

function overrideNavResizer(){

    Microsoft.Office.Server.Ajax.NavResizer.prototype.$22 = function($p0) {
    // your code.
};
}

在您的新脚本中:

Microsoft.Office.Server.Ajax.NavResizer.prototype.$22 = function () {//your function code}

Assuming you have access to the prototype object (it's in global scope) and your scripts runs after it, that is easy: 假设您有权访问原型对象(在全局范围内),并且脚本在该对象之后运行,则很简单:

var proto = Microsoft.Office.Server.Ajax.NavResizer.prototype,
    oldMethod = proto.$22;
proto.$22 = function newMethod(args, …){
    …
};

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

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