Calling internal 'public' methods within js objects

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • bizt

    Calling internal 'public' methods within js objects

    Hi,

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

    function MyObject() {
    this.alertMe = function() {
    alert('hello');
    };
    this.alertMeAga in() {
    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
  • VK

    #2
    Re: Calling internal 'public' methods within js objects

    On May 21, 8:59 pm, bizt <bissa...@yahoo .co.ukwrote:
    Hi,
    >
    Below I have a simple object / function thing (still getting head
    round these) declaration:
    >
    function MyObject() {
    this.alertMe = function() {
    alert('hello');
    };
    this.alertMeAga in() {
    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.alertMeAga in = function() {
    $.alertMe();
    };
    }

    var obj = new MyObject;
    obj.alertMeAgai n(); // alerts "bar"


    Comment

    • Gregor Kofler

      #3
      Re: Calling internal 'public' methods within js objects

      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.alertMeAga in() {
      this.alertMe(); // wrong syntax
      };
      }
      Did you mean something like this.alertMeAga in = function() { }?

      Otherwise you're trying to call the undefined MyObject::alert MeAgain 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

      Comment

      • bizt

        #4
        Re: Calling internal 'public' methods within js objects

        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

        Comment

        • VK

          #5
          Re: Calling internal 'public' methods within js objects

          On May 22, 2:25 am, bizt <bissa...@yahoo .co.ukwrote:
          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.alertMeAga in = 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.

          Comment

          • RobG

            #6
            Re: Calling internal 'public' methods within js objects

            On May 22, 2:59 am, bizt <bissa...@yahoo .co.ukwrote:
            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.alertMeAga in() {
            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_in...te_static.html >


            --
            Rob

            Comment

            Working...