0

I am having a question about javascript. To get my code organized I have the functions gathered in one variable like this.

var helper = new function(){
    this.writeProp = function(){
       //write the property
    }
    this.getProp = function(){
       //get property
    }
    this.updateProp = function(){
       //get property
    }
}

So far the code is working but and I can call it with helper.getProp() Now the amount of functions are getting bigger so I want to call the functions like this helper.prop.get() How can I accomplish this?

My thought was the following but it's not working.

var helper = new function(){
    var prop = new function(){
       this.write = function(){
           //write the property
        }
        this.getProp = function(){
           //get property
        }
        this.updateProp = function(){
           //get property
        }
    }
}

What would be the right way to do it? Or should I not try to organize my code this way?

6
  • return prop after all. You'll get every function in prop. As prop is private variable inside your function and you can not access private variable from your function. Commented Dec 7, 2015 at 13:34
  • I'm sorry could you post a codesample. I cannot understand it from the sentense. Thank you :) Commented Dec 7, 2015 at 13:35
  • 1
    @Slive2611 Something like this jsfiddle.net/esgagaqx Commented Dec 7, 2015 at 13:37
  • 2
    Is there a reason you use new function instead of simply { ... }? Commented Dec 7, 2015 at 13:40
  • 2
    If what you have there is a singleton object, then yes, there is absolutely no reason to use new function. See: Joachim Pileborg's answer. Commented Dec 7, 2015 at 13:42

4 Answers 4

2

I don't see why helper or helper.prop should be functions. Why not objects, like

var helper = {
    prop: {
        write: function() { ... },
        ...
    }
};
Sign up to request clarification or add additional context in comments.

Comments

2

You can simply declare prop as a property of your function.

var helper = new function(){
    this.prop = new function(){
       this.write = function(){
           //write the property
        }
        this.getProp = function(){
           //get property
        }
        this.updateProp = function(){
           //get property
        }
    }
};

Try this snippet:

var helper = new function(){
    this.prop = new function(){
       this.write = function(){
           //write the property
        }
        this.getProp = function(){
           //get property
          alert("this.getProp");
        }
        this.updateProp = function(){
           //get property
        }
    }
};

helper.prop.getProp();

Comments

1

Every function in JS returns undefined unless you explicitly return something else, in this case you should return the prop to keep the chaining.

Comments

1

Try like this ..

/*************************************************************/
// Modular approach to write javascript.
/*************************************************************/


var foo = (function () {
    var publicAPI = {
        bar: function () {
            publicAPI.baz();
        },
        baz: function () {
            console.log("Baz");
        }
    };

    return publicAPI;

})();

foo.bar();

One More approach you can follow is like this.

var outerObj = {};
(function(test) {
  'use strict';
  test.sayHello = function(name) {
    return 'Hi ' + name;
  }
})(outerObj);

alert(outerObj.sayHello('Kaushik'));

Comments

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.