-2

After moving from PHP to NodeJS (JavaScript) I am having some trouble with JavaScript's Anonymous Objects - I can't figure out how to create a constructor to an anonymous object like you would have in PHP with the __construct() function or Python's __init__(self) function. Maybe like Java [Link] JavaScript can't have anonymous constructors...

Solution:

After reading the comments and other answers, I came to find out that by using an anonymous function that returned an object you can gain this functionality:

var a = new function() {
  var constTxt = [not constructed]';

  /* emulating a constructor */
  if (true) {
     constTxt = '[constructed]';
  }

  return {
    functionABC : function() {
      return 'abc ' +  constTxt;
    },
    functionDCF : function() {
      return 'other function';
    }
  };
}; 
Community
  • 1
  • 1
Tyler Wall
  • 3,747
  • 7
  • 37
  • 52
  • Still don't understand what you're trying to accomplish. You've just shown some convoluted code as an example, but if you really want help you should describe what you're actually trying to acccomplish. – jfriend00 Jan 29 '14 at 00:45
  • can you write what the code you want to write that uses `a` would look like and what you expect the results to be? i don't know what you want to use the constructor on. – Claudiu Jan 29 '14 at 00:49
  • I changed the code to, what I think, expresses what I'd like to do more? – Tyler Wall Jan 29 '14 at 00:51
  • @wa please don't include your solution in the question itself. Instead, post it as an answer. – dandan78 Feb 28 '14 at 07:00
  • This question can be closed since the OP got the answer from comments. – Praveen Feb 28 '14 at 08:53

2 Answers2

1

You declare it as a function and that function becomes the constructor when you instantiate it with new module.exports(). The methods then go on the function prototype like this:

module.exports = function(arg) {...};

module.exports.prototype = {
    functionABC: function() {return 'abc'},
    functionDEF: function() {return 'def'}
}

var myExports = new module.exports();

myExports.functionABC("hello");

I'm not sure what you're asking about with the anonymous part of your question. The name of the object is the constructor so there is no anonymous object here.


Or, if you want a method called from the constructor, you could do that like this:

module.exports = function(arg) {
    // put whatever code you want in the constructor
    if (arg) {
        this.functionABC();
    } else {
        this.functionDEF();
    }
};

module.exports.prototype = {
    functionABC: function() {return 'abc'},
    functionDEF: function() {console.log('def');}
}

var myExports = new module.exports(true);   // will execute the constructor
jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • I don't see how this is calling `functionDEF` when `functionABC` is called? It is anonymous because there is no other assignment of the object except to module.exports? – Tyler Wall Jan 29 '14 at 00:34
  • It doesn't call functionDEF when functionABC is called. Those are two separate methods. You can call whichever one you want. If you want functionDEF called when functionABC is called, then you put `this.functionDEF()` inside the code for `functionABC()`. – jfriend00 Jan 29 '14 at 00:44
  • Right, but that's not a constructor? it should automatically call `functionDEF` i.e. `__construct()`? – Tyler Wall Jan 29 '14 at 00:52
  • The constructor is this function: `module.exports = function(arg) {...};`. You fill in whatever functionality you want in the constructor between the braces. I didn't know what you wanted there so I just left it for you to fill in. If you want to call a method in the constructor, you can as in `this.functionDEF()`. – jfriend00 Jan 29 '14 at 01:09
  • @Wallter - see additional example I've added to my answer. – jfriend00 Jan 29 '14 at 01:12
  • @Wallter—an "anonymous function" is just jargon for a function expression without the optional name, e.g. in `var foo = function(){};` the RHS is a function expresson or anonymous function. – RobG Jan 29 '14 at 02:59
  • Right I agree - this example was from the original framework where the SailsJS/NodeJS server is the one making the calls to this returned object... so... It needs to be able to call `functionABC(req, res)` - in essence, it needs to be an Object that is returned not the function you are returning. Like I mentioned in my question, I am looking for the PHP like functionality of a general `__construct()` function. – Tyler Wall Jan 29 '14 at 17:30
  • @Wallter - I don't know PHP so can't help you simulate a PHP behavior in JS. But, if you just want a function that returns an object, that is generally called a factory function and you can just make one like this using the object I defined in my answer: `function makeObj() { return new module.exports()}`. – jfriend00 Jan 29 '14 at 17:35
1

I don't really know what you want to do but in javascript any function can be used as a constructor. The fact that the function is a constructor depends on the way the function is called, a function will behave as a constructor if called with "new" before it.

Also, be carefull with "this", it refers to the "context" of the function an object (or any value at all in strict mode) and is set by how the function is called or using bind. It is the newly created object if the function is called as a constructor but only in that case.

RobG
  • 142,382
  • 31
  • 172
  • 209
Adrien
  • 618
  • 5
  • 7
  • I did learn this an agree with it - see the solution I came up with below as it answers my question by using this cool JavaScript unique functionality. – Tyler Wall Jan 29 '14 at 17:36