How to make a Class method for initialed obj?

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

    #1

    How to make a Class method for initialed obj?

    I have the following code:

    var obj = {a:0, b:1, ...}

    function f() {...}
    obj.f = f // This will make a instance method?

    How to make a Class method for the class of obj? Or what's the class of
    "obj"?
  • Lasse Reichstein Nielsen

    #2
    Re: How to make a Class method for initialed obj?

    Nick <nbdy9.nospam@h otmail.com> writes:
    [color=blue]
    > I have the following code:
    >
    > var obj = {a:0, b:1, ...}
    >
    > function f() {...}
    > obj.f = f // This will make a instance method?[/color]

    It will make the function named f a property of the object referred to
    by the variable obj. We use the word "method" merely as a shorthand
    for "property which is a function". In that sense, it is a method
    of "obj".
    [color=blue]
    > How to make a Class method for the class of obj? Or what's the class
    > of "obj"?[/color]

    There are no classes in Javascript. It is a prototype based language,
    and inheritance happens between objects, not between "classes".

    The way Javascript implements this inheritance is through helper
    functions which can also be used to initialize new objects.

    The "obj" is not a class, nor is it an instance of a class, since
    there are no classes. It is created using a literal, so it is not even
    constructed using a constructor function (although it behaves like it
    was created using the Object constructor function).

    --
    Lasse Reichstein Nielsen - lrn@hotpop.com
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
    'Faith without judgement merely degrades the spirit divine.'

    Comment

    • Douglas Crockford

      #3
      Re: How to make a Class method for initialed obj?

      > I have the following code:[color=blue]
      >
      > var obj = {a:0, b:1, ...}
      >
      > function f() {...}
      > obj.f = f // This will make a instance method?
      >
      > How to make a Class method for the class of obj? Or what's the class of
      > "obj"?[/color]

      In your example, f is a method of obj. You could have written it more
      compactly:

      var obj = {a: 0, b: 1, ..., f: function () {}};

      JavaScript is a Class-Free language, but it is possible to use it in the
      old style. See http://www.crockford.com/javascript/inheritance.html.

      Comment

      • Nick

        #4
        Re: How to make a Class method for initialed obj?

        According to the book "Javascript The Definitive Guide" (O'Reilly), a
        Class constructor can be done. For example,

        function Circle(radius) {
        this.r = radius;
        }

        And Class variable/function can be done:
        Circle.PI = 3.14159;
        function Circle_max(a,b) { ... }
        Circle.max = Circle_max;

        And instance method is a little bit cumbersome:

        function Circle_area() { ... };
        Circle.prototyp e.area = Circle_area;

        To instanize a circle, just do,

        var c = Circle(23.0);

        So it looks not clear that what the class of an initialized object is.
        i.e. var obj = { a:1, b:2, f:function () {...}, .... }


        Lasse Reichstein Nielsen wrote:[color=blue]
        > Nick <nbdy9.nospam@h otmail.com> writes:
        >
        >[color=green]
        >>I have the following code:
        >>
        >>var obj = {a:0, b:1, ...}
        >>
        >>function f() {...}
        >>obj.f = f // This will make a instance method?[/color]
        >
        >
        > It will make the function named f a property of the object referred to
        > by the variable obj. We use the word "method" merely as a shorthand
        > for "property which is a function". In that sense, it is a method
        > of "obj".
        >
        >[color=green]
        >>How to make a Class method for the class of obj? Or what's the class
        >>of "obj"?[/color]
        >
        >
        > There are no classes in Javascript. It is a prototype based language,
        > and inheritance happens between objects, not between "classes".
        >
        > The way Javascript implements this inheritance is through helper
        > functions which can also be used to initialize new objects.
        >
        > The "obj" is not a class, nor is it an instance of a class, since
        > there are no classes. It is created using a literal, so it is not even
        > constructed using a constructor function (although it behaves like it
        > was created using the Object constructor function).
        >[/color]

        Comment

        • Lasse Reichstein Nielsen

          #5
          Re: How to make a Class method for initialed obj?

          Nick <nbdy9.nospam@h otmail.com> writes:
          [color=blue]
          > According to the book "Javascript The Definitive Guide" (O'Reilly), a
          > Class constructor can be done. For example,[/color]

          You can do something that in *some* ways resemble a class, but it
          isn't. The Javascript "class" (i.e., a constructor function) is better
          equated to a Java constructor than to a class. For instance, you
          *can't* do inheritance between "classes" (constructor functions).
          Inheritance happens between objects in Javascript, not "classes".
          So, if anything corresponds to a Java class, it is the prototype,
          not the constructor.

          Try this:
          ---
          function Foo(){}
          function Bar(){};
          var foo = new Foo();
          var bar = new Bar();
          Bar.prototype=F oo.prototype;
          alert([foo instanceof Foo,
          foo instanceof Bar,
          bar instanceof Foo,
          bar instanceof Bar]);
          ---
          Which four booleans do you expect it to alert?
          It alerts
          true, true, false, false

          Whoa! The "bar" object is no longer an instance of the "class"
          function that constructed it. The "foo" object is an instance of two
          different "classes". In fact, foo is an "instance" of Foo.prototype
          (i.e., has it as a prototype), no matter where you store that
          object. Inheritance has nothing to do with the Foo and Bar functions,
          they just facilitate the creation of objects.

          [color=blue]
          > function Circle(radius) {
          > this.r = radius;
          > }[/color]

          This is a constructor function. It is used to create objects with a
          common (empty) prototype (Circle.prototy pe).
          [color=blue]
          > And Class variable/function can be done:
          > Circle.PI = 3.14159;[/color]

          That should correspond to Java's static fields in that they are
          properties of the "class".
          [color=blue]
          > function Circle_max(a,b) { ... }
          > Circle.max = Circle_max;[/color]

          And a static/class method.

          So far the metaphor holds.
          [color=blue]
          > And instance method is a little bit cumbersome:
          >
          > function Circle_area() { ... };
          > Circle.prototyp e.area = Circle_area;[/color]

          There is not a huge difference between that and
          function Circle() {
          ...
          this.area = Circle_area;
          }
          when you think in class-based patterns. It is only in the prototype
          based view that there is a difference between a function in the object
          itself and in the prototype.
          [color=blue]
          > To instanize a circle, just do,
          >
          > var c = Circle(23.0);[/color]

          Yes, that creates an object. You can think of Circle as its class, but
          that's all in your head (and it can be a useful way to think too). But
          in the language, there is no class.
          [color=blue]
          > So it looks not clear that what the class of an initialized object is.
          > i.e. var obj = { a:1, b:2, f:function () {...}, .... }[/color]

          There is no class. The object is an "instanceof " the constructor
          function Object, but all (non-host) objects are. Its "constructo r"
          property also points to the Object function.

          Thinking in prototypes, it is simple what happens: The new object
          is created. Its prototype is the original value of Object.prototyp e.
          That's the entire story.


          More func with "inheritanc e":
          ---
          function Foo(){}
          var foo = new Foo();

          function Bar(){}
          Bar.prototype = foo;
          var bar = new Bar();

          function Baz(){}
          Baz.prototype=b ar;
          var baz = new Baz();

          Bar.prototype = new Object();
          alert([baz instanceof Baz, baz instanceof Bar, baz instanceof Foo);
          ---
          Guess the result :)
          [color=blue]
          > Lasse Reichstein Nielsen wrote:[/color]

          Please don't top post.
          /L
          --
          Lasse Reichstein Nielsen - lrn@hotpop.com
          DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
          'Faith without judgement merely degrades the spirit divine.'

          Comment

          • Nick

            #6
            Re: How to make a Class method for initialed obj?

            One correction:

            function Foo(){}
            function Bar(){};
            var foo = new Foo();
            var bar = new Bar();
            Bar.prototype=F oo.prototype;
            alert([foo instanceof Foo,
            foo instanceof Bar,
            bar instanceof Foo,
            bar instanceof Bar]);
            ---
            shows: true, true, false, true
            instead of: true,true,false , false

            Anyway the O'Reilly book use term Class, which may not be proper for
            prototype based OO language.

            So the answer of my question should be,

            Object obj = { .... } is and only is an instance of Object.

            Is it a correct statement?



            Lasse Reichstein Nielsen wrote:[color=blue]
            > Nick <nbdy9.nospam@h otmail.com> writes:
            >
            >[color=green]
            >>According to the book "Javascript The Definitive Guide" (O'Reilly), a
            >>Class constructor can be done. For example,[/color]
            >
            >
            > You can do something that in *some* ways resemble a class, but it
            > isn't. The Javascript "class" (i.e., a constructor function) is better
            > equated to a Java constructor than to a class. For instance, you
            > *can't* do inheritance between "classes" (constructor functions).
            > Inheritance happens between objects in Javascript, not "classes".
            > So, if anything corresponds to a Java class, it is the prototype,
            > not the constructor.
            >
            > Try this:
            > ---
            > function Foo(){}
            > function Bar(){};
            > var foo = new Foo();
            > var bar = new Bar();
            > Bar.prototype=F oo.prototype;
            > alert([foo instanceof Foo,
            > foo instanceof Bar,
            > bar instanceof Foo,
            > bar instanceof Bar]);
            > ---
            > Which four booleans do you expect it to alert?
            > It alerts
            > true, true, false, false
            >
            > Whoa! The "bar" object is no longer an instance of the "class"
            > function that constructed it. The "foo" object is an instance of two
            > different "classes". In fact, foo is an "instance" of Foo.prototype
            > (i.e., has it as a prototype), no matter where you store that
            > object. Inheritance has nothing to do with the Foo and Bar functions,
            > they just facilitate the creation of objects.
            >
            >
            >[color=green]
            >>function Circle(radius) {
            >> this.r = radius;
            >>}[/color]
            >
            >
            > This is a constructor function. It is used to create objects with a
            > common (empty) prototype (Circle.prototy pe).
            >
            >[color=green]
            >>And Class variable/function can be done:
            >>Circle.PI = 3.14159;[/color]
            >
            >
            > That should correspond to Java's static fields in that they are
            > properties of the "class".
            >
            >[color=green]
            >>function Circle_max(a,b) { ... }
            >>Circle.max = Circle_max;[/color]
            >
            >
            > And a static/class method.
            >
            > So far the metaphor holds.
            >
            >[color=green]
            >>And instance method is a little bit cumbersome:
            >>
            >>function Circle_area() { ... };
            >>Circle.protot ype.area = Circle_area;[/color]
            >
            >
            > There is not a huge difference between that and
            > function Circle() {
            > ...
            > this.area = Circle_area;
            > }
            > when you think in class-based patterns. It is only in the prototype
            > based view that there is a difference between a function in the object
            > itself and in the prototype.
            >
            >[color=green]
            >>To instanize a circle, just do,
            >>
            >>var c = Circle(23.0);[/color]
            >
            >
            > Yes, that creates an object. You can think of Circle as its class, but
            > that's all in your head (and it can be a useful way to think too). But
            > in the language, there is no class.
            >
            >[color=green]
            >>So it looks not clear that what the class of an initialized object is.
            >>i.e. var obj = { a:1, b:2, f:function () {...}, .... }[/color]
            >
            >
            > There is no class. The object is an "instanceof " the constructor
            > function Object, but all (non-host) objects are. Its "constructo r"
            > property also points to the Object function.
            >
            > Thinking in prototypes, it is simple what happens: The new object
            > is created. Its prototype is the original value of Object.prototyp e.
            > That's the entire story.
            >
            >
            > More func with "inheritanc e":
            > ---
            > function Foo(){}
            > var foo = new Foo();
            >
            > function Bar(){}
            > Bar.prototype = foo;
            > var bar = new Bar();
            >
            > function Baz(){}
            > Baz.prototype=b ar;
            > var baz = new Baz();
            >
            > Bar.prototype = new Object();
            > alert([baz instanceof Baz, baz instanceof Bar, baz instanceof Foo);
            > ---
            > Guess the result :)
            >
            >[color=green]
            >>Lasse Reichstein Nielsen wrote:[/color]
            >
            >
            > Please don't top post.
            > /L[/color]

            Comment

            • Douglas Crockford

              #7
              Re: How to make a Class method for initialed obj?

              > So the answer of my question should be,[color=blue]
              >
              > Object obj = { .... } is and only is an instance of Object.
              >
              > Is it a correct statement?[/color]

              Syntactically, no. This isn't Java. There is no form like

              Object obj = ...

              I don't use instanceof. I don't trust it. I do make very heavy use of
              constructor functions to create objects, and prototypes to reduce memory
              usage, and augmentation to specialize objects.

              Class hierarchies can be very shallow because you don't need to make a
              new class to each minor variation.

              Even in static class-based languages like Java, I think it is bad form
              to have to routinely cast or query objects about their types. But I am
              in the minority on that.


              Comment

              • Lasse Reichstein Nielsen

                #8
                Re: How to make a Class method for initialed obj?

                Nick <nbdy9.nospam@h otmail.com> writes:
                [color=blue]
                > One correction:
                >
                > function Foo(){}
                > function Bar(){};
                > var foo = new Foo();
                > var bar = new Bar();
                > Bar.prototype=F oo.prototype;
                > alert([foo instanceof Foo,
                > foo instanceof Bar,
                > bar instanceof Foo,
                > bar instanceof Bar]);
                > ---
                > shows: true, true, false, true
                > instead of: true,true,false , false[/color]

                You are using Mozilla? I only checked the code in Opera, but checking
                now, it seems that IE agrees with me, and Mozilla with you.

                Reading the ECMAScript standard, I'd say that Mozilla is in violation.
                [color=blue]
                > So the answer of my question should be,
                >
                > Object obj = { .... } is and only is an instance of Object.
                >
                > Is it a correct statement?[/color]

                Almost (i.e., in most cases yes). The object "obj" has as its prototype
                the original value of Object.prototyp e. That makes it an instance of
                Object (and that can't be changed, since the prototype property of Object
                is read-only and don't-delete).

                It is also an instance of any other function with the same prototype:

                var x = {x:42};
                function Foo(){};
                Foo.prototype = Object.prototyp e;
                alert(x instanceof Foo);

                /L 'please don't top post'
                --
                Lasse Reichstein Nielsen - lrn@hotpop.com
                DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
                'Faith without judgement merely degrades the spirit divine.'

                Comment

                • Nick

                  #9
                  Re: How to make a Class method for initialed obj?

                  Lasse Reichstein Nielsen wrote:[color=blue]
                  > Nick <nbdy9.nospam@h otmail.com> writes:
                  >
                  >[color=green]
                  >>One correction:
                  >>
                  >> function Foo(){}
                  >> function Bar(){};
                  >> var foo = new Foo();
                  >> var bar = new Bar();
                  >> Bar.prototype=F oo.prototype;
                  >> alert([foo instanceof Foo,
                  >> foo instanceof Bar,
                  >> bar instanceof Foo,
                  >> bar instanceof Bar]);
                  >>---
                  >>shows: true, true, false, true
                  >>instead of: true,true,false , false[/color][/color]

                  Yes, I am using Mozilla. As Douglas (in the thread) said, instanceof is
                  not trustable. And it really scares me away from using the advance OO
                  features of Javascript since even Mozilla can implement it differently.
                  That's too bad - although most of the tasks can be done easily without
                  the OO approach.
                  [color=blue]
                  >
                  >
                  > You are using Mozilla? I only checked the code in Opera, but checking
                  > now, it seems that IE agrees with me, and Mozilla with you.
                  >
                  > Reading the ECMAScript standard, I'd say that Mozilla is in violation.
                  >
                  >[color=green]
                  >>So the answer of my question should be,
                  >>
                  >>Object obj = { .... } is and only is an instance of Object.
                  >>
                  >>Is it a correct statement?[/color]
                  >
                  >
                  > Almost (i.e., in most cases yes). The object "obj" has as its prototype
                  > the original value of Object.prototyp e. That makes it an instance of
                  > Object (and that can't be changed, since the prototype property of Object
                  > is read-only and don't-delete).
                  >
                  > It is also an instance of any other function with the same prototype:
                  >
                  > var x = {x:42};
                  > function Foo(){};
                  > Foo.prototype = Object.prototyp e;
                  > alert(x instanceof Foo);
                  >
                  > /L 'please don't top post'[/color]

                  Comment

                  Working...