JavaScript Object and Prototypes

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #16
    Ok, now that I think I understand how Objects work in JavaScript, I'm off to look up how to implement a Singleton pattern using JavaScript.

    Comment

    • gits
      Recognized Expert Moderator Expert
      • May 2007
      • 5390

      #17
      ;) nope ... you could even do (for a specific instance):

      Code:
      function MY_OBJ() {}
      
      MY_OBJ.prototype.my_func = function() {};
      
      var o = new MY_OBJ;
      
      o.my_func = function() { // new code here };
      kind regards

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5390

        #18
        hmmm ... a singleton :) something like this:

        Code:
        function MY_OBJ() {
            if (this.o != null) {
                return MY_OBJ.prototype.o;
            } else {
                MY_OBJ.prototype.o = this;
            }
        }
        
        MY_OBJ.prototype.o = null;
        
        MY_OBJ.prototype.my_func = function() {};
         
        var o = new MY_OBJ;
        
        o.foo = 'foo';
        
        alert(o.foo);
        
        var a = new MY_OBJ;
        
        a.foo = 'bar';
        
        alert(a.foo);
        alert(o.foo);
        kind regards

        Comment

        • Frinavale
          Recognized Expert Expert
          • Oct 2006
          • 9749

          #19
          :D

          You know what's funny?
          I decided that I don't actually want to use the Singleton in my solution.

          ;)

          Thanks again Gits!

          Comment

          • gits
            Recognized Expert Moderator Expert
            • May 2007
            • 5390

            #20
            *lol* ... but it's quite cool ;) ... nope ... serious ... when you have a look at it it makes use of the things we talked about before ... when we first create an instance we overwrite the prototype of the class and when the second instance should be created we just return a ref to the first created object ;)

            kind regards

            Comment

            • Frinavale
              Recognized Expert Expert
              • Oct 2006
              • 9749

              #21
              Originally posted by gits
              when you have a look at it it makes use of the things we talked about before ...
              I was thinking the same thing.
              It is pretty cool, and thanks to you it makes a lot of sense to me now.

              I have a long way to go with JavaScript. As my ASP.NET applications become more complicated I'm finding that I have to use JavaScript more and more. It's a really good thing that you've helped me understand these basics now because I have a feeling everything's about to become a lot more complicated in my future.

              Comment

              • gits
                Recognized Expert Moderator Expert
                • May 2007
                • 5390

                #22
                you know where to find help ;) ... JavaScript is quite cool!! :)

                kind regards,
                gits

                Comment

                • Dormilich
                  Recognized Expert Expert
                  • Aug 2008
                  • 8694

                  #23
                  Originally posted by Frinavale
                  What is the name of the ":" notation so that I can look this up?
                  "Object Literal" (just to give that info)

                  Comment

                  • Frinavale
                    Recognized Expert Expert
                    • Oct 2006
                    • 9749

                    #24
                    Originally posted by gits
                    ok .... let's have a closer look at it :) ...
                    Code:
                    var foo = {
                        a: 'some_value',
                        b: function() { // whatever code }
                    };
                    here you create an instance of an object directly and you declare the properties a and b ... where b stores a reference to the function-code that is assigned to it. note: when you have a look at this ... this is where JSON is not far away ;) just have that as a string from a response and eval it and you have a native js-object ready for use.
                    I started debugging my original Ajax enabled .net server control (again...becaus e it's not working in IE8... in fact it's crashing IE8 RC1). Upon researching a way around my problems I encountered numerous references to JSON with regards to these types of controls. Curious, I finally looked into JSON and now understand what you were referring to in your comment here.

                    Originally posted by gits
                    PS: be aware of the use of the this keyword when working with javascript object ... this always relates to the caller of a function ... so that might be tricky when starting to use objects in JavaScript.
                    I find it ironic that I had to experience this problem first hand myself even though you warned me about ""this"".

                    Comment

                    Working...