Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Oct 2011
    Posts
    56
    Thanks
    1
    Thanked 0 Times in 0 Posts

    What's wrong with this code?

    Hello, if you could please tell me what I did wrong to make this code not work. Can you put a variable in a function callback ?

    <script type="text/javascript">
    var aaa = prompt('number?')
    var obja = new Const(aaa);
    functin Const(numb){
    this.x=numb;
    alert(obja.x);
    }
    </script>

  • #2
    Gütkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    It's function, not functin.

    Also, alert(obja.x) from within the constructor doesn't make any sense. The constructor is not supposed to know about some arbitrary instance. You probably mean alert(this.x).
    .My new Javascript tutorial site: http://reallifejs.com/
    .Latest article: Calculators — Tiny jQuery calculator, Full-fledged OOP calculator, Big number calculator
    .Latest quick-bit: Including jQuery — Environment-aware minification and CDNs with local fallback

  • #3
    New Coder
    Join Date
    Oct 2011
    Posts
    56
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Ok, thanks how about this one?

    var aaa = prompt('number?');
    var bbb = prompt('number?');
    var obja = new Const(aaa);
    var objb = new Const(bbb);
    function Const(aaa,bbb){
    this.x=aaa;
    this.y=bbb;
    alert(this.x+this.y);
    }
    the point is to add two numbers. The numbers are obtained through a prompt, then defined to a variable, then put inside a callback function. The problem I've having is getting both in there at the same time.

  • #4
    Gütkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    I remember mentioning this already: Please spend some time on working through a beginner's tutorial. If you did, all of those issues you are currently having would disappear.

    You obviously set up your constructor to expect two parameters, yet instead of providing those two parameters when instantiating the object, you only provide one. Two times. That doesn't make any sense.

    The rather obvious solution is
    PHP Code:
    var obja = new Const(aaabbb); 
    .My new Javascript tutorial site: http://reallifejs.com/
    .Latest article: Calculators — Tiny jQuery calculator, Full-fledged OOP calculator, Big number calculator
    .Latest quick-bit: Including jQuery — Environment-aware minification and CDNs with local fallback


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •