覆盖/重写javascript库函数 [英] Override/Rewrite a javascript library function

查看:73
本文介绍了覆盖/重写javascript库函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用开源JavaScript库 timeline.verite.co 这是一个时间轴库,在页面加载时效果很好.但是当我尝试在特定条件下重新绘制时间轴时,它开始发出奇怪的错误

我想修改库中的init函数.但是我不想在原始库中更改它,而是想在另一个单独的.js文件中重写/覆盖此函数,以便在调用此函数时,必须使用我修改后的函数,而不是转到原始函数. /p>

我不确定是否要使用原型/继承以及如何使用它来解决此问题?

解决方案

您只需为其分配新值.这是一个示例:

obj = {
        myFunction : function() {
            alert('originalValue');
        }
    }

    obj.myFunction();
    obj.myFunction = function() {
        alert('newValue');
    }
    obj.myFunction();

I am using an open source javascript library timeline.verite.co It's a timeline library which works great on page load. But when I try to repaint the timeline on certain condition, it starts giving out weird errors

I would like to modify the init function in the library. But instead of changing it in the original library itself, I would like to rewrite/override this function in another separate .js file so that when this function is called, instead of going to the original function, it must use my modified function.

I'm not sure whether to use prototype/ inheritance and how to use it to solve this problem?

解决方案

You only need to assign the new value for it. Here is an example:

obj = {
        myFunction : function() {
            alert('originalValue');
        }
    }

    obj.myFunction();
    obj.myFunction = function() {
        alert('newValue');
    }
    obj.myFunction();

这篇关于覆盖/重写javascript库函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆