Why the different way to declare a class?

Discussion in 'Javascript' started by Ray, Jul 31, 2006.

  1. Ray

    Ray Guest

    Hello,

    While reading another guy's I found myself a bit confused by his way of
    declaring his class--I usually declare a class like this:

    function Foo() {
    this.blah = true;
    this.bleh = false;
    }

    Foo.prototype.blabla = function() {
    };

    and so on. However in his code he does a lot of this:

    var Foo = {
    blah : true,
    bleh : false,
    blabla : function() {
    }
    };

    My question is, how are they different? I mean I guess he's declaring
    his class JSON style. But can he even do a new() on his class? Seems
    like then he's limited to using his methods and variables statically,
    e.g.: Foo.blah, Foo.bleh, Foo.blabla() and so on.

    Is there any particular reason why one would do that?

    Thank you!
    Ray
    Ray, Jul 31, 2006
    #1
    1. Advertising

  2. Ray

    Paul Guest

    Different things for different uses.
    Ray wrote:
    > Hello,
    >
    > While reading another guy's I found myself a bit confused by his way of
    > declaring his class--I usually declare a class like this:
    >
    > function Foo() {
    > this.blah = true;
    > this.bleh = false;
    > }
    >
    > Foo.prototype.blabla = function() {
    > };


    With this technique you can make lots of foo (really instances of Foo).
    var a = new Foo();
    var b = new Foo();
    You could also create a Foo sorting function:
    function fooSort(foo1,foo2){
    var comp = 0;
    if(foo1.blah && !foo2.blah){
    comp = (-1);
    } else if(!foo1.blah && foo2.blah){
    comp = (1);
    }
    return comp;
    }
    and sort an array of foo:
    var a = new Foo();
    var b = new Foo();
    b.blah = false;
    var c = new Foo();
    var fooArray = new Array(a,b,c);
    fooArray.sort(fooSort);

    Some might avoid prototype using a form like:
    function Foo() {
    this.blah = true;
    this.bleh = false;
    this.blabla = function() {
    };
    arguments.callee.fooSorter = function fooSort(foo1,foo2){
    var comp = 0;
    if(foo1.blah && !foo2.blah){
    comp = (-1);
    } else if(!foo1.blah && foo2.blah){
    comp = (1);
    }
    return comp;
    }
    }
    // test
    var a = new Foo();
    var b = new Foo();
    b.blah = false;
    var c = new Foo();
    fooArray.sort(Foo.fooSorter);
    var res = "";
    for(var i=0;i<fooArray.length;i++){
    res += fooArray.blah + "\n";
    }
    alert(res);

    >
    > and so on. However in his code he does a lot of this:
    >
    > var Foo = {
    > blah : true,
    > bleh : false,
    > blabla : function() {
    > }
    > };

    This form is used to create a single object. It is also useful for many
    things.
    If you only need one, then this is more concise.
    >
    > My question is, how are they different? I mean I guess he's declaring
    > his class JSON style. But can he even do a new() on his class? Seems
    > like then he's limited to using his methods and variables statically,
    > e.g.: Foo.blah, Foo.bleh, Foo.blabla() and so on.
    >
    > Is there any particular reason why one would do that?

    Let the flame wars commence... ;-)

    >
    > Thank you!
    > Ray
    Paul, Jul 31, 2006
    #2
    1. Advertising

  3. In article <>, Ray
    <> writes
    >Hello,
    >
    >While reading another guy's I found myself a bit confused by his way of
    >declaring his class--I usually declare a class like this:
    >
    >function Foo() {
    > this.blah = true;
    > this.bleh = false;
    >}
    >
    >Foo.prototype.blabla = function() {
    >};
    >
    >and so on. However in his code he does a lot of this:
    >
    >var Foo = {
    > blah : true,
    > bleh : false,
    > blabla : function() {
    > }
    >};

    <snip>

    If you want only one of that kind of object then the second way is less
    typing. If you want several then the first way is less confusing and
    often more convenient.

    Incidentally, if you are using 'class' to mean a class of objects with
    something interesting in common then ok.

    If you mean objects with a rigid class definition that can't be altered
    after construction, as in Java, then very not ok.

    Either way, 'class' is a dangerous word to use in Javascript.

    John
    --
    John Harris
    John G Harris, Jul 31, 2006
    #3
  4. Ray

    Ray Guest

    John G Harris wrote:
    > If you want only one of that kind of object then the second way is less
    > typing. If you want several then the first way is less confusing and
    > often more convenient.
    >
    > Incidentally, if you are using 'class' to mean a class of objects with
    > something interesting in common then ok.


    Um, yeah, class as in a class of objects, not like in Java :)

    Thanks John!
    Ray

    >
    > If you mean objects with a rigid class definition that can't be altered
    > after construction, as in Java, then very not ok.
    >
    > Either way, 'class' is a dangerous word to use in Javascript.
    >
    > John
    > --
    > John Harris
    Ray, Aug 1, 2006
    #4
  5. Ray

    Ray Guest

    Paul wrote:
    <snip>

    Hey, thanks Paul! I learned a new trick :)

    Cheers
    Ray

    >
    > >
    > > Thank you!
    > > Ray
    Ray, Aug 1, 2006
    #5
    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. Replies:
    20
    Views:
    2,200
  2. John Black
    Replies:
    2
    Views:
    371
    jeffc
    Jun 15, 2004
  3. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    878
    Mark Rae
    Dec 21, 2006
  4. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,840
    Smokey Grindel
    Dec 2, 2006
  5. Andre
    Replies:
    3
    Views:
    1,615
    John Bode
    May 19, 2010
Loading...

Share This Page