Calling internal 'public' methods within js objects

Discussion in 'Javascript' started by bizt, May 21, 2008.

  1. bizt

    bizt Guest

    Hi,

    Below I have a simple object / function thing (still getting head
    round these) declaration:

    function MyObject() {
    this.alertMe = function() {
    alert('hello');
    };
    this.alertMeAgain() {
    this.alertMe(); // wrong syntax
    };
    }

    Notice how in the method - alertMeAgain - I have attempted to call the
    other public function, alertMe(). Now this doesnt work as I have come
    to know - how do I call internal methods from another methods with the
    same object??

    Also, I pretty new to using custom objects in js. Is this the most
    common method of building objects in js? I come from using PHP which
    uses a quite similar means of declaring classes so I like this one.
    Cheers

    Burnsy
    bizt, May 21, 2008
    #1
    1. Advertising

  2. bizt

    VK Guest

    On May 21, 8:59 pm, bizt <> wrote:
    > Hi,
    >
    > Below I have a simple object / function thing (still getting head
    > round these) declaration:
    >
    > function MyObject() {
    > this.alertMe = function() {
    > alert('hello');
    > };
    > this.alertMeAgain() {
    > this.alertMe(); // wrong syntax
    > };
    >
    > }
    >
    > Notice how in the method - alertMeAgain - I have attempted to call the
    > other public function, alertMe(). Now this doesnt work as I have come
    > to know - how do I call internal methods from another methods with the
    > same object??
    >
    > Also, I pretty new to using custom objects in js. Is this the most
    > common method of building objects in js? I come from using PHP which
    > uses a quite similar means of declaring classes so I like this one.


    Such methods creates closures on each call - it is hugely ineffective
    unless your methods must to preserve states between calls (static in
    VB sense). In such case make an instance reference holder like:

    function MyObject() {
    var $ = this; // holder
    var foo = 'bar'; // protected var
    this.alertMe = function() {
    alert(foo);
    };
    this.alertMeAgain = function() {
    $.alertMe();
    };
    }

    var obj = new MyObject;
    obj.alertMeAgain(); // alerts "bar"
    VK, May 21, 2008
    #2
    1. Advertising

  3. bizt meinte:
    > Hi,
    >
    > Below I have a simple object / function thing (still getting head
    > round these) declaration:
    >
    > function MyObject() {
    > this.alertMe = function() {
    > alert('hello');
    > };
    > this.alertMeAgain() {
    > this.alertMe(); // wrong syntax
    > };
    > }


    Did you mean something like this.alertMeAgain = function() { }?

    Otherwise you're trying to call the undefined MyObject::alertMeAgain and
    the semicolon behind that statement is missing.

    Gregor


    --
    http://photo.gregorkofler.at ::: Landschafts- und Reisefotografie
    http://web.gregorkofler.com ::: meine JS-Spielwiese
    http://www.image2d.com ::: Bildagentur für den alpinen Raum
    Gregor Kofler, May 21, 2008
    #3
  4. bizt

    bizt Guest

    > Such methods creates closures on each call - it is hugely ineffective
    > unless your methods must to preserve states between calls (static in
    > VB sense).


    Basically i prefer to use something where my vars are encapsulated as
    i need to have individual instances of some in each object (i have
    ajax requests in my real ones using xmlhtmlrequest objects and may be
    sending requests simultaniously - this has given problem if i use the
    same object in quick succession). I would first think to use objects
    in other languages but dont have much experiece using javascript
    objects - this is my only knowledge.

    Anyway, when you speak of creating closures and preserve states - what
    do you mean? Sorry, again new to this. I assume an instance reference
    wont create a seperate instance of my object. is this correct? want to
    keep memory usage low as possible. thanks

    burnsy
    bizt, May 21, 2008
    #4
  5. bizt

    VK Guest

    On May 22, 2:25 am, bizt <> wrote:
    > > Such methods creates closures on each call - it is hugely ineffective
    > > unless your methods must to preserve states between calls (static in
    > > VB sense).

    >
    > Basically i prefer to use something where my vars are encapsulated as
    > i need to have individual instances of some in each object (i have
    > ajax requests in my real ones using xmlhtmlrequest objects and may be
    > sending requests simultaniously - this has given problem if i use the
    > same object in quick succession). I would first think to use objects
    > in other languages but dont have much experiece using javascript
    > objects - this is my only knowledge.
    >
    > Anyway, when you speak of creating closures and preserve states - what
    > do you mean?


    Look at the sample again:

    function MyObject() {
    var $ = this; // holder
    var foo = 'bar'; // protected var
    this.alertMe = function() {
    alert(foo);
    };
    this.alertMeAgain = function() {
    $.alertMe();
    };
    }

    the new object instance goes into constructor, gets its properties and
    leaves: but because of inner functions assigned to alertMe and
    alertMeAgain - the whole current context of MyObject is preserved for
    this particular instance, so you can address $ and foo from instance
    method any time later. It is like as if each instance would take take
    with it its own clone copy of the constructor. If you know Perl then
    it is very close to its sub closures:
    sub outer {
    my $x = $_[0];
    local *inner = sub {return $x + 2};
    return 3 + inner();
    }
    The difference is that in Javascript you can nest functions directly
    as well:
    function outer() {
    function inner() {
    }
    }

    > Sorry, again new to this. I assume an instance reference
    > wont create a seperate instance of my object. is this correct?


    If you mean like
    var a = new MyObject;
    var b = a;
    then now, of course b=a doesn't clone an instance, it remains only one
    pointed by both a and b.
    VK, May 22, 2008
    #5
  6. bizt

    RobG Guest

    On May 22, 2:59 am, bizt <> wrote:
    > Hi,
    >
    > Below I have a simple object / function thing (still getting head
    > round these) declaration:


    In javascript, functions *are* objects.

    >
    > function MyObject() {


    Identifiers starting with a capital letter are usually reserved for
    constructors, i.e. functions that you intend to call with the new
    operator. Is that what you are doing here?

    How you call a function determines the value of its this keyword, so
    I'll assume MyObject is a constructor and that you are calling it
    something like:

    var newObject = new MyObject();


    > this.alertMe = function() {
    > alert('hello');
    > };


    If MyObject is called as a constructor, its this keyword is set to a
    reference to a new object. The above line assigns an anonymous
    function to an alertMe property of that object.


    > this.alertMeAgain() {
    > this.alertMe(); // wrong syntax
    > };


    Here you attempt to call the alertMeAgain property of the newly
    created object, but you haven't added that property or given it a
    value so of course it gives an error.


    > }
    >
    > Notice how in the method - alertMeAgain - I have attempted to call the
    > other public function, alertMe().


    An alertMeAgain property was not added to the object, nor was an
    attempt made to assign a function to it.


    > Now this doesnt work as I have come
    > to know - how do I call internal methods from another methods with the
    > same object??


    Firstly you have to declare the or initialise the methods, then assign
    them to properties of an appropriate object. You can then call them.
    Further advice may be possible if you show a bit more code so we can
    tell if you are trying to use a constructor or plain object.


    > Also, I pretty new to using custom objects in js. Is this the most
    > common method of building objects in js? I come from using PHP which
    > uses a quite similar means of declaring classes so I like this one.


    There are a variety of ways to build objects with properties that are
    similar to classes in other languages. I think it is best to learn
    the standard prototype iheritance method, then go from there. There
    are a number of patterns for emulating public and private methods in
    javascript, which one you choose is up to you. e.g.

    <URL: http://www.litotes.demon.co.uk/js_info/private_static.html >


    --
    Rob
    RobG, May 22, 2008
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Charles A. Lackman
    Replies:
    1
    Views:
    1,341
    smith
    Dec 8, 2004
  2. SpamProof
    Replies:
    0
    Views:
    551
    SpamProof
    Oct 21, 2003
  3. Alf P. Steinbach
    Replies:
    11
    Views:
    710
    David Lowndes
    May 7, 2004
  4. Andreas Schmitt
    Replies:
    6
    Views:
    370
    Andreas Schmitt
    Jul 29, 2005
  5. Robert
    Replies:
    16
    Views:
    191
Loading...

Share This Page