2

The exampleCode is:

// 4. execute tower.init latter read from bellow (1)
tower={
  init:function(){ console.log('horee im here now, finaly after a day code');}
}

// 3. app object
app ={ 
  publish:function(what,data){
     data.nextModule.init();
  }
}
// 2. then call this funct
var fires = function(){
   return app.publish('subscriber',{ nextModule : 'tower'});
}
// 1. execute this first
fires();

Explanation Problem

  • When i fire fires() 1.
  • The 2. isfine app.publish('text',{nextModule:'tower');
  • Bypassed to 3. app.publish(text,data)

My problem is

I want to convert data.nextmodule --> into an Object or Function then call the tower module.init()

data.nextModule.init() cannot executed because nextModule is String

how to make this code run like this

data.'tower'.init();

I have reading this reference

Answer by Houshalter converting an object to a string !not an String into an Object

Can node js to that?
Can we covert to object as easy as JSON.stringify(data) ?

UPDATE ERROR on terminal

         throw err;
            ^
 TypeError: Object tower has no method `init`
Community
  • 1
  • 1
azl
  • 175
  • 1
  • 1
  • 7
  • 1
    Are you looking fro bracket notation? See here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_Accessors – elclanrs Aug 20 '14 at 07:32
  • Node.js uses Chrome's V8 javascript engine which implements ECMAScript as specified in ECMA-262. So yes, if you can do it in the browser you can do it in Node. – Matt Harrison Aug 20 '14 at 07:37
  • its throw err `data["nextModule"].init();` `typeError: Object tower has no method init..` – azl Aug 20 '14 at 08:33

2 Answers2

2

Two things, first change the identifier new to something else.

Second,

data.newxtModule is a string that represents variable in your script. All variables are part of the GLOBAL object. So you can just retrieve that variable by passing that variable's name in string format to the GLOBAL object. And you will be able to call the init() of tower.

Simply change this line,

data.nextModule.init();

To,

GLOBAL[data.nextModule].init();

Final code should look like this.

// 4. execute tower.init latter read from bellow (1)
tower={
  init:function(){ console.log('horee im here now, finaly after a day code');}
}

// 3. app object
app ={ 
  publish:function(what,data){
     GLOBAL[data.nextModule].init();
  }
}
// 2. then call this funct
var new1 = function(){
   return app.publish('subscriber',{ nextModule : 'tower'});
}
// 1. execute this first
new1();

Here is Fiddle

AdityaParab
  • 7,024
  • 3
  • 27
  • 40
  • ofcourse my real code is not an `new` its `fire()` i will edit but its still throw err has no method init.. js can do that why node canot..?? – azl Aug 20 '14 at 08:38
  • i try it on browser with js its works.. but it still has error on nodejs what is GLOBAL[]? its kind of variable or array? can we convert thos string like we convert `JSON.stringify(data)`? thanks.. – azl Aug 20 '14 at 08:50
  • You can think of GLOBAL in Node to be just like window in browser. It holds all the variables that are declared globally! Whether or not you can stringify GLOBAL object depends on presence of cyclic links within the object. If your GLOBAL object contains cycles (one property referencing to other property and so on) the JSON.stringify will throw error. GLOBAL is most likely to have cyclic properties. – AdityaParab Aug 20 '14 at 09:06
  • also, your `data` is an object `stringify`ing it would stop data.nextModule from working. If you `stringify`the data paramenter, it will return you string and you won't be able to call nextModule on string :) – AdityaParab Aug 20 '14 at 09:08
1

Change 'tower' to tower.

By the way, you'd better do not use the key word 'new'.

Ivan
  • 21
  • 2
  • i edit and try `fires()` its still object has no method.. how about fix by tryig `convert(data)` into data obj – azl Aug 20 '14 at 08:41
  • but it works well if you update the string 'tower' without the quotation marks. here is the [Fiddle](http://jsfiddle.net/1nev0hhd/2/) – Ivan Aug 21 '14 at 01:54