"OOP JavaScript"


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: "OOP JavaScript"

  1. #1
    Nir Guest

    "OOP JavaScript"


    Hi,

    I'm experiencing a certain problem.
    I've defined a CTest class in the following way :

    function CTest() {
    this.string = "Testing 1 2 3";
    this.show = show;

    function show()
    {
    alert(this.string);
    }
    }

    this.string is a data member of CTest, but when I try the following lines-it
    fails :
    var o = new CTest();
    o.show;
    The allocation of CTest works OK, but the alert of this.string doesn't work

    Any suggestions ?
    thanx in advance,
    Nir.


  2. #2
    Tom Duffy Guest

    Re: "OOP JavaScript"


    Hello Nir:

    Move your show() function outside of the CTest function. OOP in Javascript
    is far different than that of other truly OO languages like Java. When you
    say this.show = show you are actually defining a property of CTest that happens
    to be a method. The method is defined as a function and, as such, must be
    located in its own code block. The use of the word 'class' doesn't mean
    the same thing as in other OO languages. Think of CTest() as a constructor
    instead and the script block itself as the 'class'.

    HTH,

    Tom

    "Nir" <nir@effective-i.com> wrote:
    >
    >Hi,
    >
    >I'm experiencing a certain problem.
    >I've defined a CTest class in the following way :
    >
    >function CTest() {
    > this.string = "Testing 1 2 3";
    > this.show = show;
    >
    > function show()
    > {
    > alert(this.string);
    > }
    >}
    >
    >this.string is a data member of CTest, but when I try the following lines-it
    >fails :
    >var o = new CTest();
    >o.show;
    >The allocation of CTest works OK, but the alert of this.string doesn't work
    >
    >Any suggestions ?
    >thanx in advance,
    >Nir.
    >



  3. #3
    Steve Berzins Guest

    Re: "OOP JavaScript"

    o.show();

    the other guy is right kinda, o.show is a property, o.show() is a method
    call, javascript NEEDS the parens to tell it that, period.
    so just moving the method out of the other function wouldn't solve the
    problem.


    --
    Steve Berzins, MCSD
    Senior Software Design Engineer
    Shavlik Technologies, LLC MCSPP
    www.shavlik.com
    e.shavlik.com
    steveb@shavlik.com
    "Nir" <nir@effective-i.com> wrote in message
    news:3ac33b99$1@news.devx.com...
    >
    > Hi,
    >
    > I'm experiencing a certain problem.
    > I've defined a CTest class in the following way :
    >
    > function CTest() {
    > this.string = "Testing 1 2 3";
    > this.show = show;
    >
    > function show()
    > {
    > alert(this.string);
    > }
    > }
    >
    > this.string is a data member of CTest, but when I try the following

    lines-it
    > fails :
    > var o = new CTest();
    > o.show;
    > The allocation of CTest works OK, but the alert of this.string doesn't

    work
    >
    > Any suggestions ?
    > thanx in advance,
    > Nir.
    >




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center