0

I have an object that needs certain properties to be set by execution instead of assignment. Is it possible to do this using a literal object notation?

I would like to be able to access the object's properties using this:

myObject.propertyName

...rather than this:

objInstance = new myObject();
objInstance.propertyName;

EDIT: to clarify, based on Bergi's answer, this is what I'm aiming to do:

var myObj = {
    myInfo: (function() { return myObj.getInfo('myInfo'); })(),
    getInfo: function() { 
      /* lots of execution here that would be redundant if done within myInfo */
    }
}

// access the calculated property value
myObj.myInfo;

But this gives me the error myObj is not defined

  • 2
    What is "instantialialization"? – Bergi Feb 25 '14 at 18:07
  • 5
    If you don't get useful answers it might be that I'm not the only one who didn't understand your question. In that case a little more code might be a valuable addition. – Denys Séguret Feb 25 '14 at 18:07
  • @Bergi: sorry, I thought this was less obscure term than it actually is :) http://dictionary.sensagent.com/instantialization/en-en/ – Recovering Nerdaholic Feb 25 '14 at 18:13
  • @Recovering: Ah, since there are neither templates nor static typing in JS I've never heard of it (and it doesn't really apply). I've first thought of a portmanteau from *instantiation* (creating an object) and *initialisation* (executing the constructor) – Bergi Feb 25 '14 at 18:28
  • 1
    "instantiation" ... that's the word I was looking for. I guess it overcomplexitized it :) – Recovering Nerdaholic Feb 25 '14 at 18:37
  • no, literals can only create native constructor instances. you can use Object.defineProperty to do anything you want upon execution. – dandavis Feb 25 '14 at 18:40

2 Answers2

2

I guess what you want is an IEFE, which you can put inside an object literal:

var myObject = {
    propertyName: (function() {
        var it = 5*3; // compute something and
        return it;
    }()),
    anotherFunction: function() {…}
};

myObject.propertyName // 15

Maybe you also want to use the module pattern. Have a look at Simplest/Cleanest way to implement singleton in JavaScript?.

Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • This is very close to what I need, but how do I call another function defined as a property of the object? It results in an error `[object] is not defined` – Recovering Nerdaholic Feb 25 '14 at 18:35
  • No idea what your code looks like. But you just would put the functions on the object literal as usually. – Bergi Feb 25 '14 at 18:41
  • 1
    @RecoveringNerdaholic: Or do you mean calling them from the IEFE? That won't work, as the object doesn't yet exist when it is executed. Use the module pattern then. Also check out https://stackoverflow.com/questions/4616202/self-references-in-object-literal-declarations – Bergi Feb 25 '14 at 18:43
  • @Bergi: That's it! The module pattern. Thanks! – Recovering Nerdaholic Feb 25 '14 at 18:59
0

Thanks to Bergi to finding this, here is a final example what I wanted to do:

myObj = {
   init: function() {
        this.oneProperty = anyCodeIWant...
        this.anotherProperty = moreCodeIWant...
        // and so on
        return this;
   }
}.init();

myObj.oneProperty;
myObj.anotherProperty;
// and so on