0

I am new to scripting side and again need your help to write better code. I am using init function which is a collection of all functions, but now there seems to be few functions that I want to be executed if certain condition is true.

SAMPLE SCRIPT

 $(document).ready(function() {

var app = {
init: function(){

  // Variable to check condition
  var abc = 0;

  // Default functions
  this.Function1();
  this.Function2();
  this.Function3(); // abc variable will change to 1 if condition is true

  // Conditional functions
  if ( abc === 1 ) {
     this.ConditionFunction1();
     this.ConditionFunction2();
  },

  Function1: function(){ // some Code },
  Function2: function(){ // some Code },
  Function3: function(){
                if(true) { abc == 1; } 
  },
  ConditionFunction1: function(){ // some Code },
  ConditionFunction2: function(){ // some Code }

}
app.init();
});
6
  • It's not clear what you're trying to do here - abc is hard-coded to 0, so your condition will never hit. If you want to perform some logic when the abc value changes, I'd suggest you use an event based patter instead. Commented Dec 12, 2017 at 11:16
  • Those Function: are inside Init(), you should place them outside. Commented Dec 12, 2017 at 11:16
  • @RoryMcCrossan friend can u explain a bit please about the event based patter? 'abc' is some dummy variable that i was trying to apply condition. Commented Dec 12, 2017 at 11:18
  • @Zorkind which functions to put outside? the condition functions will cacheDOM some elements if called. Commented Dec 12, 2017 at 11:20
  • after the conditional you place some functions with comma, i figure those should be out of Init()? the way it is, everything is inside Init() except for ConditionalFunctions that is Commented Dec 12, 2017 at 11:23

1 Answer 1

1

With rewriting and by orientate on your code you could write it like this:

    $(document).ready(function () {

    var app = {
        init: function () {

            // Variable to check condition
            var abc = 0;
            var Function1 = function () { // some Code
                console.log("Function1")
            }

            Function2 = function () { // some Code
                console.log("Function2")

            }

            Function3 = function () {
                if (true) {
                    console.log("Function3")
                    abc == 1;
                }
            }

            ConditionFunction1 = function () { // some Code
                console.log("ConditionFunction1")
            }

            ConditionFunction2 = function () { // some Code
                console.log("ConditionFunction2")
            }
            // Default functions
            Function1();
            Function2();
            Function3(); // abc variable will change to 1 if condition is true

            // Conditional functions
            if (abc === 1) {
                ConditionFunction1();
                ConditionFunction2();
            }
        }
    }
    app.init();
});
Sign up to request clarification or add additional context in comments.

1 Comment

never thought it would be that easy :) Thanks friend

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.