1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

xyz() is not a function

Discussion in 'JavaScript' started by Im The ONE, Mar 26, 2009.

  1. #1
    I wrote this piece of code and when I call me.trumped.js.calculate(); it tells me that it is not a function
    //--------define--------//
    if(!me) var me={};
    if(!me.trumped) me.trumped={};
    if(!me.trumped.js) me.trumped.js={};
      me.trumped.js= function(){
      var t = {};
      t.a = 1;
      t.b = 2;
      t.calculate = function()
      {
      	t.add = t.a+t.b;
    	document.getElementById('result).value = t.add.toFixed(2);	
      };
      return t;
    }();
    Code (markup):

     
    Im The ONE, Mar 26, 2009 IP
  2. fightrsi

    fightrsi Guest

    Messages:
    29
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Missing a single quote?
    document.getElementById('result).value = t.add.toFixed(2);
     
    fightrsi, Mar 26, 2009 IP
  3. Im The ONE

    Im The ONE Peon

    Messages:
    800
    Likes Received:
    40
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Fixed it and this didn't work.
    Forgot to mention it doesn't pop up the error, It was shown by firebug
     
    Im The ONE, Mar 27, 2009 IP
  4. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #4
    this should work:
    
    <input type="text" id="result" />
    <script>
    //--------define--------//
    if(!me) var me={};
    if(!me.trumped) me.trumped={};
    if(!me.trumped.js) me.trumped.js={};
      me.trumped.js= [B]new[/B] function(){
      var t = {};
      t.a = 1;
      t.b = 2;
      t.calculate = [B]new[/B] function()
      {
      	t.add = t.a+t.b;
    	document.getElementById('result').value = t.add.toFixed(2);	
      };
      return t;
    }
    </script>
    
    Code (markup):
     
    camjohnson95, Mar 27, 2009 IP
  5. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #5
    although that seems to call the function without it even being called.
     
    camjohnson95, Mar 27, 2009 IP
  6. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #6
    this should be right:
    
    <input type="text" id="result" />
    <script>
    //--------define--------//
    if(!me) var me={};
    if(!me.trumped) me.trumped={};
    if(!me.trumped.js) me.trumped.js={};
      me.trumped.js= new function(){
      var t = {};
      t.a = 1;
      t.b = 2;
      t.calculate = function()
      {
      	t.add = t.a+t.b;
    	document.getElementById('result').value = t.add.toFixed(2);	
      };
      return t;
    }
    me.trumped.js.calculate();
    </script>
    
    Code (markup):
     
    camjohnson95, Mar 27, 2009 IP
  7. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #7
    i'm not sure why you had the (); after the end of the function.
     
    camjohnson95, Mar 27, 2009 IP
    Im The ONE likes this.
  8. Im The ONE

    Im The ONE Peon

    Messages:
    800
    Likes Received:
    40
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Cheers mate. This works!
    Can you explain why "new function()" for me.trumped.js and just "function" for t.calculate?
     
    Im The ONE, Mar 27, 2009 IP
  9. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #9
    honestly, no idea. I'm not much good at javascript, but have had to use it a little bit lately with something similar.
     
    camjohnson95, Mar 27, 2009 IP