1

Code of horror:

var namespace = new function ()
{
    this.saySomething = new function( something )
    {
        console.log( something );
    };
}

namespace.saySomething( "whatever" );

This outputs "undefined" when I expected it to output "whatever". How come? And what would the fix be? My goal is nothing more than to have a namespace where I put some helper functions.

0

5 Answers 5

3

new function is generally wonky. Rob W's answer is absolutely correct (+1'd), but I would personally go even further and remove all the newing:

var namespace = 
{
    saySomething: function( something )
    {
        console.log( something );
    }
}

namespace.saySomething( "whatever" );

If you want to be able to create distinct instances, do it the right way:

function Namespace() {}
Namespace.prototype.saySomething = function (something)
{
    console.log(something);
}

// Usage:
var foo = new Namespace();
foo.saySomething("Hallllooooo!");

...though you did say "singleton," so I'm guessing this isn't relevant to your goal.

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

1 Comment

Thanx. Yeah you're right, the prototype object for me in this particular situation is a little bit overkill. And I never intended to create a full blown singleton pattern either. So that's why I mixed together the words in the topic like so: "namespace/singleton". Appreciate your answer!
2

You don't need to use new before the functions. new is only used for instantiating objects, when a constructor is already defined.

Your code should look like:

var namespace = {
    saySomething: function(something) {
        console.log(something);
    }
}

namespace.saySomething("whatever");

1 Comment

I wouldn't use this method because somebody might use your namespace and can override and do namespace.saySomething = function( someotherthing ) { console.log( someotherthing ); /* and some more stuff here.*/ }.
2

Remove new before function(something){...}. Currently, saySomething is an instance of the anonymous constructor.

1 Comment

Oh but of course, can't believe I missed that. Been awake for too long. Thank you so much for a superfast answer!
1

I would use something like the following because you also want to make sure that namespace doesn't get overridden by some other code.

var namespace = (function() {
      return {
          saySomething: function(something) {
               console.log(something);
          }
      };
})();

namespace.saySomething("whatever");

Comments

0

This should do it, sample http://jsfiddle.net/Jssxy/

var namespace = {
    saySomething : function(something) {
        console.log(something);
    }
}

namespace.saySomething( "whatever" );​

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.