最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to add execute custom function in switch statement - Stack Overflow

programmeradmin0浏览0评论

I am trying to execute a switch statement based on the argument passed to the function below and I tried to execute different functions based on the argument passed. But I am getting Uncaught ReferenceError: of another function in the beginning of the .js file when i tried a function in the third case as shown below.

function getjobCon(reqType){

       var identifier= $( "#single" ).val();

          switch (reqType) { 
                            case 'job': 
                                alert('you clicked job');
                                break;
                            case 'title': 
                                alert('you clicked title');
                                break;
                            case 'designation': function(identifier){
                                 $(this).addClass('slate');

                            }                                   
                                break;      

                            }
                    }

can someone tell me how I can do this ?. I tried alot but couldn't get it corrected.

JS Fiddle - /

Note - I am executing this function when different buttons are clicked. And I am changing the class of the button in the third case where I am trying to execute another function.

I am trying to execute a switch statement based on the argument passed to the function below and I tried to execute different functions based on the argument passed. But I am getting Uncaught ReferenceError: of another function in the beginning of the .js file when i tried a function in the third case as shown below.

function getjobCon(reqType){

       var identifier= $( "#single" ).val();

          switch (reqType) { 
                            case 'job': 
                                alert('you clicked job');
                                break;
                            case 'title': 
                                alert('you clicked title');
                                break;
                            case 'designation': function(identifier){
                                 $(this).addClass('slate');

                            }                                   
                                break;      

                            }
                    }

can someone tell me how I can do this ?. I tried alot but couldn't get it corrected.

JS Fiddle - http://jsfiddle/ck221xxk/

Note - I am executing this function when different buttons are clicked. And I am changing the class of the button in the third case where I am trying to execute another function.

Share Improve this question edited Aug 6, 2015 at 23:54 BigB asked Aug 6, 2015 at 23:38 BigBBigB 791 silver badge9 bronze badges 7
  • A fiddle or pen might help – code Commented Aug 6, 2015 at 23:42
  • I will add a fiddle. Thanks. – BigB Commented Aug 6, 2015 at 23:42
  • Added. If I name the function or if I declare another function, it works. But I don't want to name it. – BigB Commented Aug 6, 2015 at 23:55
  • why anyone would want to wrap a line(s) of code in an anonymous function, just to do it, is beyond me. Just put the code within the case statement. Unless you just want to make it difficult to read. – WhiteHat Commented Aug 7, 2015 at 0:16
  • I will be making an ajax call in the function with the variable. Can start the ajax call in place of the function ? – BigB Commented Aug 7, 2015 at 0:19
 |  Show 2 more ments

3 Answers 3

Reset to default 2

There are a few issues with your code.

  1. The function you have specified is not being called (the switch statement is not calling it). A switch statement is just a construct within JavaScript, and a : in a switch statement, does not mean = as it does in a object literal. (Didn't notice this myself first, so thanks @WhiteHat).
  2. this changes value from function to function. this in your outer function may mean "the current HTML-node" but inside the anonymous function, it will most likely refer to window unless something different is defined.
  3. Defining in-line functions like that is a syntax error.

You can of course have functions in-line; but you need to wrap it in brackets (), and call it with () call(), apply() or similar.

I will use call() since the first argument specified in call() is the this-argument, meaning that i can keep this "the same" in both contexts.

function getjobCon(reqType) {

  var identifier= $( "#single" ).val();

  switch (reqType) { 
      case 'job': 
          alert('you clicked job');
          break;
      case 'title': 
          alert('you clicked title');
          break;
      case 'designation': 
          (function(identifier){

           $(this).addClass('slate');

          }).call(this, identifier);                                   
          break;      
      }
   }
}

However, unless you badly need a new namespace, I don't see any reason to do this, any of the other answers would probably suffice.

just remove the function statement, no need for it, or simply call your own as shown here...

function getjobCon(reqType){
   var identifier= $( "#single" ).val();

      switch (reqType) { 
                        case 'job': 
                            alert('you clicked job');
                            break;
                        case 'title': 
                            alert('you clicked title');
                            break;
                        case 'designation': 
                            customFunc(identifier);
                            break;      
                        }
                }

function customFunc(identifier) {
  // put code here
}

You can just call your next function inside of that case

function getjobCon(reqType) {
  var identifier = $("#single").val();

  switch (reqType) {
    case 'job':
      alert('you clicked job');
      break;
    case 'title':
      alert('you clicked title');
      break;
    case 'designation':
      doStuff(identifier);
      break;
  }
};

function doStuff(identifier){
  // execute other code here
};

发布评论

评论列表(0)

  1. 暂无评论