0

So I am building a little app and my JavaScript code is based off the Module pattern because I think it's a really clean method of coding in JavaScript. However I have seen two examples one which uses the below code and the other does not:

return {
    someMethod: someMethod,
    otherMethod: otherMethod
}

What is the purpose to the above code and is it needed?

1
  • 1
    It's just code to return an object. The purpose depends on the context, but it's just an object literal. Commented May 7, 2016 at 13:31

2 Answers 2

1

It's merely returning an object. The code:

return {
   someMethod: someMethod,
   otherMethod: otherMethod
}

is exactly identical to:

var someObject = {
    someMethod: someMethod,
    otherMethod: otherMethod
}

return someObject;
Sign up to request clarification or add additional context in comments.

4 Comments

Okay but I could still call the Methods without it? That's why I wasn't sure how it makes any difference to the code.
It's exactly the same except you don't have to explicitly use a variable to store the object you're creating. It makes exactly zero difference to how it works.
There is a tradition among functional programmers of avoiding declaring names. FP people prefer anonymous functions and value literals wherever possible to avoid having to decide what to name the thing you're declaring. The reason for that that in pure FP, variables are not allowed (only constants are allowed). Thus in a non-pure FP language FP programmers avoid declaring variables and function names.
@Elevant without seeing more code, the answer to the question of whether you can "call the Methods" is unknown. If "the Methods" are local variables to the function containing that return statement, then the answer is "no you cannot".
0
// create an scope by declaring anonymous function and immediately executing it
// the return value will be assigned to myModule...
var myModule = (function(){

    // return the parts of the scope to be exposed publicly
    return {
        someMethod: someMethod,
        otherMethod: otherMethod
    }

    function someMethod(item){ return myPrivateSquareFunction(item) * 2; };
    function otherMethod(item){ return item * 5; };
    // private function can be called from in the module's scope, but not externally
    function myPrivateSquareFunction(item){ return item * item; }

})();

// now, out here you can call `otherMethod`, and `someMethod` (which itself calls `myPrivateSquareFunction`) but you can not call `myPrivateSquareFunction`...

console.log(someMethod(3)); // 18
console.log(otherMethod(3)); // 15
console.log(myPrivateSquareFunction(3)); // ERROR!

By using this pattern, you can choose what implementation details remain private, and what needs to be exposed to the module. This helps you to break up your code into discreet chunks that can be worked on and tested in isolation without the rest of the application complicating it's function. Then, when you have lots of small chunks of code that do their own job correctly, and properly separated, it is an easier task to manage to assemble them into a more complex application.

Comments

Your Answer

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