简体   繁体   English

如何在Javascript中覆盖函数

[英]How Can I Override A Function In Javascript

I have the following scripts: 我有以下脚本:

<script ... jquery-1.9.1.js"></script>
<script ... dataTables.js"></script>    
<script ... columnFilter.js"></script>

The following code exists in columnFilter.js: columnFilter.js中存在以下代码:

(function ($) {

   $.fn.columnFilter = function (options) {
       //some code...

       function _fnCreateCheckbox(oTable, aData) {
           //some code...
       }

   };

})(jQuery);

What I would like to do is override function _fnCreateCheckbox(oTable, aData) with my own code. 我想要做的是使用我自己的代码覆盖function _fnCreateCheckbox(oTable, aData) Im fairly new to javascript, so would appreciate an example. 我是一个相当新的JavaScript,所以将欣赏一个例子。

I have tried simply grabbing the code above and adding it to it's own <script> tags, but that didn't work. 我试过简单地抓住上面的代码并将其添加到它自己的<script>标签中,但这不起作用。 It completely stopped the columnFilter.js from working (which is as expected I guess). 它完全阻止了columnFilter.js的工作(我猜这是预期的)。 Not really sure what else to try. 不确定还有什么可以尝试的。

function _fnCreateCheckbox(oTable, aData) {

Only exists in the scope in which it was created as (function ($) { creates a function scope. You must edit it there. You can't override it outside the function. 仅存在于创建它的作用域中(function ($) {创建一个函数作用域。你必须在那里编辑它。你不能在函数之外覆盖它。

EDIT: On a related note 编辑:在相关的说明

If you are crafty with JS and you are trying to get that function to do something else only sometimes, you could pass some extra variables into your columnFilter plugin/function call and handle them in that function to do something else. 如果你对JS非常狡猾并且你试图让这个函数有时只做其他事情,你可以将一些额外的变量传递给你的columnFilter插件/函数调用,并在该函数中处理它们以做其他事情。 I have no idea what column filter is, but let's pretend to call it on an element like so: 我不知道什么是列过滤器,但让我们假装在一个元素上调用它:

el.columnFilter({optionA: true, optionB: false});

If you wanted to do something else based on some data you have you could do, 如果你想根据你可以做的一些数据做其他事情,

el.columnFilter({optionA: true, optionB: false, extraOption: true});

Then in your script, depending on what your entire script does: 然后在您的脚本中,根据您的整个脚本执行的操作:

$.fn.columnFilter = function (options) {
   //some code...
   if(options.extraOption){
       function _fnCreateCheckbox(oTable, aData) {
           //some default code...
       }
   } else {
       function _fnCreateCheckbox(oTable, aData) {
           //my other code...
       }
   }
};

This is a crude example, but just to display your options. 这是一个粗略的例子,但只是为了显示您的选择。

I suppose you import the columnFilter.js file from some external source. 我想你从一些外部源导入columnFilter.js文件。

One option could be to copy the columnFilter.js file to your project's directory, modify it as you please and then import it from your project's directory . 一个选项可能是将columnFilter.js文件复制到项目的目录中,根据需要进行修改,然后从项目目录中导入它

You can override a function by reassigning its prototype. 您可以通过重新分配原型来覆盖函数。 It is generally advised against though. 但通常建议不要这样做。

var d = new Date();

alert(d.getFullYear()); // 2013

Date.prototype.getFullYear = function() { return "Full Year"; }

alert(d.getFullYear()); // "Full Year"

http://jsfiddle.net/js5YS/ http://jsfiddle.net/js5YS/

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

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