Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/428.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在javascript中扩展exist函数_Javascript_Jquery_Function_Prototype - Fatal编程技术网

如何在javascript中扩展exist函数

如何在javascript中扩展exist函数,javascript,jquery,function,prototype,Javascript,Jquery,Function,Prototype,也许这个问题的标题是重复的,但我无法通过谷歌找到我需要的。 对于考试,我之前创建了一个js文件,在这个文件中我定义了一个函数 function fun1() { alert("a"); } 所以我在一些“点击”触发器中调用这个函数 现在,我添加了新模块,我不想改变函数1的定义,我想像这样扩展函数1 function fun1ex() { alert("b"); } 这意味着,单击“按钮”时,将显示两个警报框(“a”)和(“b”) 我可以用class和prototype吗?

也许这个问题的标题是重复的,但我无法通过谷歌找到我需要的。 对于考试,我之前创建了一个js文件,在这个文件中我定义了一个函数

function fun1()
{
    alert("a");
}
所以我在一些“点击”触发器中调用这个函数

现在,我添加了新模块,我不想改变函数1的定义,我想像这样扩展函数1

function fun1ex()
{
     alert("b");
}
这意味着,单击“按钮”时,将显示两个警报框(“a”)和(“b”) 我可以用class和prototype吗? 但我无法想象怎么做。
谢谢

只需将新功能附加到单击事件:

$("button").click(function(e){
    fun1();
});

$("button").click(function(e){
    fun1ex();
});

这将
警报(“a”)
警报(“b”)
我想我们可以这样做

function mytest(text) {
                this.text = text;
                this.fun1 = function() {
                    alert("a "+text);
                }
                this.fun2 = function() {
                    alert("b "+text);
                }
                this.fun3 = function(){
                    for(var i in this) {
                        //executeFunctionByName("mytest."+i, window)
                        //eval("mytest."+i);
                        if(typeof this[i] === "function" && i != "fun3") {
                           this[i]();
                        }
                          //alert(typeof this[i]);
                    }

                }
            }

            mytest.prototype.fun4 = function() {
                alert("c " + this.text);
            }

            var test1 = new mytest("tung");
            test1.fun3();
在这个测试中,我定义了fun3来执行test1对象中可用的所有方法(fun3除外) 在另一个项目(或js文件)中,我可以创建新的protopye函数来扩展fun3

mytest.prototype.fun4 = function() {
                    alert("c " + this.text);
                }
也许很多人都知道这一点,但我希望它对像我这样的新手有用。
谢谢

只要在
fun1ex()
内部调用
fun1ex()
?或者让click事件调用这两个函数?保持简单,不要试图用类和原型使它过于复杂。谢谢你的评论,但是button.click函数是在第一个js文件中定义的。非常感谢你的解决方案,它非常好,但是fun1()调用了很多次,第一个项目中有很多操作,我无法在第二个项目中重新编写它们。
mytest.prototype.fun4 = function() {
                    alert("c " + this.text);
                }