Javascript 串联函数

Javascript 串联函数,javascript,function,concatenation,Javascript,Function,Concatenation,这背后的想法是用鼠标滚轮键盘制作动画,并在进入和退出时滑动。每个部分都有不同的动画。 所有内容都被包装在一个全局变量中。这是一个更大的样本 var siteGlobal = (function(){ init(); var init = function(){ bindEvents(); } // then i got my function to bind

这背后的想法是用鼠标滚轮键盘制作动画,并在进入和退出时滑动。每个部分都有不同的动画。 所有内容都被包装在一个全局变量中。这是一个更大的样本

  var siteGlobal = (function(){

            init();

            var init = function(){
               bindEvents();
            }

             // then i got my function to bind events
             var bindEvents = function(){
                $(document).on('mousewheel', mouseNav());
                $(document).on('keyup', mouseNav());
             }

             // then i got my function here for capture the event
             var mouseNav = function(){
               // the code here for capturing direction or keyboard
               // and then check next section
             }

             var nextSection = function(){
               // Here we check if there is prev() or next() section
               // if there is do the change on the section
             }

            var switchSection = function(nextsection){
              // Get the current section and remove active class
              // get the next section - add active class
              // get the name of the function with data-name attribute
              // trow the animation 
              var funcEnter = window['section'+ Name + 'Enter'];
            } 


           // Let's pretend section is call Intro 
           var sectionIntroEnter = function(){
              // animation code here
           }

          var sectionIntroExit = function(){
             // animation code here
           }
     }();
到目前为止一切都很好,直到调用funcEnter()为止,什么都没有发生
我仍然坚持调用这些函数…抱歉,伙计们,我真的不是一个javascript程序员,我正在学习过程中,通过这种方式,我可以很容易地阅读,所以我喜欢继续使用这种“编码”方式…有人有线索吗?谢谢

您的连接是正确的,但是如果您不创建全局函数来实现这一点会更好。相反,将它们放在您自己的对象中,并通过那里访问函数

var sectionFuncs={
A:{
输入:函数(){
console.log('输入A');
},
退出:函数(){
log(“退出A”);
}
},
B:{
输入:函数(){
console.log('进入B');
},
退出:函数(){
console.log(“退出B”);
}
}
};
函数onClick(){
var section=this.getAttribute('data-section');
var函数=sectionFuncs[节];
函数。enter();
log('In between…');
函数exit();
}
var buttons=document.queryselectoral('button');
对于(变量i=0;i
A

B
您可以拥有一个包含这些函数的对象,并通过名称键入:

var enterExitFns = {
    intro: {
        enter: function () {
            // animation code for intro enter
        },
        exit: function () {
            // animation code for intro exit
        }
    },
    details: {
        enter: function () {
            // animation code for details enter
        },
        exit: function () {
            // animation code for details exit
        }
    }
};

var name = activeSection.attr('data-name');

enterExitFns[name].enter();

不要使用太多的变量名。使用具有可通过编程方式访问的属性的对象。那么,您的代码是否不起作用?你的问题是什么?如果所有函数都是全局函数,那么
window['section'+name+'Enter']
应该可以工作,您可以调用
func1()。不,它根本不起作用。控制台没有抛出任何错误。mousewheel事件是Trigger,我不是一个javascript程序员……我正在学习这个过程,我很想继续使用我现在编写的代码,稍后继续寻找更好的方法。。所以我要写一段更大的代码,这样你们就可以确切地看到我是如何写我的东西的,这是公平的。至少您知道代码的哪些部分需要改进:)