jquery oop覆盖方法函数

nab*_*zan 9 javascript oop jquery overriding

嗨,我想知道如果我的方法声明如下,我怎么能覆盖方法函数:

(function ($) {
    $.extend({
        tablesorter: new
        function () {
            function buildHeaders(table) {
                console.log('ORIGINAL HEADERS');
            }

            this.construct = function (settings) {
                return this.each(function () {
                    $headers = buildHeaders(this);
                });
            }
        }
    });

    $.fn.extend({
        tablesorter: $.tablesorter.construct
    });
})(jQuery);
Run Code Online (Sandbox Code Playgroud)

我的目标是完全重写tablesorter buildHeaders函数.

(function ($) {
    var originalMethod = $.fn.tablesorter;
    $.fn.tablesorter = function() {
        console.log('overiding');

        function buildHeaders(table) {
            console.log('OVERRIDE HEADERS');
        }
        originalMethod.apply(this, arguments);
    }
})(jQuery);
Run Code Online (Sandbox Code Playgroud)

这不起作用......任何帮助都会很棒.谢谢!

Sub*_*has 20

简答:不,你不能.

函数内部的函数(即buildHeaders另一函数内的函数)是私有的,不能被覆盖.拿这个简单的例子猜猜输出:

// A very simple function inside a function
test = function() {
  function buildHeaders() {
    alert("original buildHeaders called");
  }

  buildHeaders();
}

// Now lets take a backup of the "test" function
oldTest = test;

// And try to override a private function
test = function() {
  function buildHeaders() {
    alert("duplicate buildHeaders called");
  }

  oldTest.apply(this, arguments);
}

// Call
test();
Run Code Online (Sandbox Code Playgroud)

猜输出?

为什么?

我认为你是在Java(或类似)背景下尝试这一点,你可以覆盖实际的方法.在Javascript中,您不会覆盖函数,而是替换它们.即

function x() { }    // Original function
oldX = x            // Original function is now oldX
x = function() { }  // x is now a **NEW** function
                    // we did not override, we replaced

// At this point, oldX and x are two different functions
// You just swapped their names, but x is no longer the original x
Run Code Online (Sandbox Code Playgroud)

这部分很清楚.现在转到第二部分,私有/本地变量:

function x() {
  var y = 0;
}
x();
alert(y); // No, you cannot access "y" from outside
Run Code Online (Sandbox Code Playgroud)

但是我们采取:

function x() {
  y = 0;  // Without "var"
}
x();
alert(y); // Alerts "0"!!
Run Code Online (Sandbox Code Playgroud)

如果你把var y = 0它变成私有的那个功能.如果你不这样做,它就变成了全局范围(技术上是scoped,但是现在让我们把它留下来).

第三部分,函数内部的函数默认是私有的.按照同样的例子,

function x() {
  function y() { }
  // this is the same as saying:
  var y = function() { }
  // note "var y", so you can't override this from outside
}
Run Code Online (Sandbox Code Playgroud)

因此,如果您通常在函数内部定义一个函数function x() { function y() { } },那么它y就是私有函数x.与你结合你永远不能覆盖javascript中的功能,你只能替换.因此y,除了原始x函数之外,您将永远无法访问或修改它.

只有替代品

只有在有权访问自定义实现时,才能使用自定义实现替换该函数.所以你必须编辑原始函数,或者以某种方式你必须保存对函数buildHeaders 外部的引用.即你必须做其中一个:

// ...
tablesorter: new function() {
  this.buildHeaders = function() { }
  // ...
}

// and later, you can replace this:
tablesorter.buildHeaders = function() { // alternative code }
Run Code Online (Sandbox Code Playgroud)

您将能够覆盖该函数,因为它不是私有的,并且您有一个访问它的句柄.

编辑:次要语法