2

I want to create a single object. Does the below code make sense?

singleObj = new function () {
    // act as a constructor.
};

Am I hurting any good practice?

I need a constructor. A simple object literal would not be useful here.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
thom
  • 51
  • 4
  • maybe duplicate question: http://stackoverflow.com/questions/1479319/simplest-cleanest-way-to-implement-singleton-in-javascript – Andreas Köberle Aug 12 '11 at 16:59
  • 1
    Why do you think you need a constructor *without* a custom prototype? – katspaugh Aug 12 '11 at 17:02
  • I don't think this is a duplicate. The OP is invoking the `new` operator on an anonymous function. I don't think I've ever seen that before and I've been writing JavaScript for at least 13 years. It _is_ legal JavaScript, however.... – Ray Toal Aug 12 '11 at 17:02
  • *Why* wouldn't an object literal be useful there? It seems like a text-book case to *moi*. – Michael Lorton Aug 12 '11 at 17:03
  • @Malvolio, because a literal wouldn't allow private scope? – katspaugh Aug 12 '11 at 17:05
  • Because I need a constructor Malvolio. Thank you. – thom Aug 12 '11 at 17:07
  • 1
    @katspaugh -- if that's the case, he needs the [module](http://yuiblog.com/blog/2007/06/12/module-pattern/) pattern, not to screw around with the `new` operator. – Michael Lorton Aug 12 '11 at 17:08
  • Ray Toal, so, what do you think? Is it a bad idea? Thanks. – thom Aug 12 '11 at 17:08
  • 1
    @thom -- I know your response feels explanatory to you, but from the outside it is just circular. *Why* do you need a constructor, especially for a singleton? – Michael Lorton Aug 12 '11 at 17:09
  • It might be convenient not to return any object explicitly. – katspaugh Aug 12 '11 at 17:17
  • I want to register some events in constructor. Thank you Malvolio. – thom Aug 12 '11 at 17:17
  • 1
    @thom: You ought to stop repeating that. Stating that you "need" the thing that you've decided is going to be your implementation does not help us to understand _why_ you've decided that. "Why do you need a constructor?" "Because I need a constructor" – Lightness Races in Orbit Aug 12 '11 at 18:02

4 Answers4

1

If you want just a single object, in that you are never going to make one again, an object literal works perfectly here.

var x = { };

Will give you an object. In order for

var F = function() {
};

to give you an object you will need to invoke F

var x = new F();
Michael Lorton
  • 43,060
  • 26
  • 103
  • 144
zellio
  • 31,308
  • 1
  • 42
  • 61
0

you could try someting like:

var objCreate = function() {
    var obj = {};
    // do constructor logic
    return obj;
};
Warspawn
  • 46
  • 5
0

Just create a new object and then populate it. You don't need a contrustor to do this.

var singleObject = {};
singleObject.value1 = "whatever";

If you really want to use a function, then you need to actually call it.

var singleObj = new (function () {
    // act as a constructor.
})();

We can use a self executing function by creating a anonymous function function(){}, and immediately calling it with an empty argument set.

Paul Perigny
  • 973
  • 5
  • 12
  • And it's not self-executing, unless it's a function cutting it's own head. – katspaugh Aug 12 '11 at 17:06
  • When `new` "calls" the function directly you can't pass in arguments. The form I suggested will allow it. If you don't need to parametrize your constructor then there no point in calling either call forms. – Paul Perigny Aug 12 '11 at 17:08
  • Self-executing functions are commonly used to control name space pollution by creating new namespaces (closure). In essence they are used to create a set of variables that disappear at the end of a function. (ie they commit suicide.) – Paul Perigny Aug 12 '11 at 17:18
  • Local variables commit suicide upon any function's returning, so it's not a valid explanation for the term "self-executing". – katspaugh Aug 12 '11 at 17:24
0

http://www.w3schools.com/js/js_objects.asp

//Used to create an object
personObj=new Object();
personObj.firstname="John";
personObj.lastname="Doe";
personObj.age=50;
personObj.eyecolor="blue";

//Used as a constructor for the object
function person(firstname,lastname,age,eyecolor)
{
this.firstname=firstname;
this.lastname=lastname;
this.age=age;
this.eyecolor=eyecolor;
}
//how to declare objects via constructor template
var myFather=new person("John","Doe",50,"blue");
var myMother=new person("Sally","Rally",48,"green");
The Internet
  • 7,959
  • 10
  • 54
  • 89