var Something= new Something() What does it mean ?

Discussion in 'Javascript' started by pamelafluente@libero.it, Oct 5, 2006.

  1. Guest

    Hi,

    I was "studying" the famous (public code) BusyBox. I see the
    instruction:
    var busyBox = new BusyBox

    as in

    var busyBox = new BusyBox("BusyBox1", "busyBox", 4, "gears_ani_",
    ".gif", 125, 147, 206)


    What that syntax "var busyBox = new ... " actually means ?? What is
    NEW. I use all the time constructors in .NET, but I am unsure about
    Javascript. So far I have never seen it in javascript.

    Does it mean I can create an object with properties, fields, methods,
    etc ?

    -P
    , Oct 5, 2006
    #1
    1. Advertising

  2. VK Guest

    wrote:
    > What that syntax "var busyBox = new ... " actually means ?? What is
    > NEW.


    new ConstructorName() means "make me a new instance of this kind of
    object and return a reference to this instance".

    <script type="text/javascript">

    function Dell() {
    this.producer = 'Dell, Inc.';
    this.URL = 'http://www.dell.com';
    }

    function OptiFlex (processor, memory) {
    Dell.call(this);
    this.processor = processor || 'Athlon 1.2GHz';
    this.memory = memory || '128Mb';
    }

    var myComputer = new OptiFlex();
    alert(myComputer.producer);
    alert(myComputer.processor);
    alert(myComputer instanceof OptiFlex);
    </script>
    VK, Oct 5, 2006
    #2
    1. Advertising

  3. Guest

    VK ha scritto:

    > wrote:
    > > What that syntax "var busyBox = new ... " actually means ?? What is
    > > NEW.

    >
    > new ConstructorName() means "make me a new instance of this kind of
    > object and return a reference to this instance".



    This is really nice. I didn't get that it is possible to "create"
    objects in javascript. Beautiful!

    I see you defined fields. Is it possible to define also "methods" and
    "properties" like in .net?
    And don't tell me we also have inheritance or interfaces! Do we?

    -P

    >
    > <script type="text/javascript">
    >
    > function Dell() {
    > this.producer = 'Dell, Inc.';
    > this.URL = 'http://www.dell.com';
    > }
    >
    > function OptiFlex (processor, memory) {
    > Dell.call(this);
    > this.processor = processor || 'Athlon 1.2GHz';
    > this.memory = memory || '128Mb';
    > }
    >
    > var myComputer = new OptiFlex();
    > alert(myComputer.producer);
    > alert(myComputer.processor);
    > alert(myComputer instanceof OptiFlex);
    > </script>
    , Oct 5, 2006
    #3
  4. VK Guest

    wrote:
    > I see you defined fields. Is it possible to define also "methods" and
    > "properties" like in .net?


    in .Net what? ;-) There is no such language, that's an interface atop
    of a set of languages - including JScript. You must be working with C#,
    and yes, fields (simple properties) and methods are welcome. You only
    cannot define compound properties with getter and setter (simply
    speaking something called as property but acting like method() )

    function Dell() {
    this.producer = 'Dell, Inc.';
    this.URL = 'http://www.dell.com';
    }

    function OptiFlex (processor, memory) {
    Dell.call(this);
    this.processor = processor || 'Athlon 1.2GHz';
    this.memory = memory || '128Mb';
    this.showConfig = function() {
    alert(this.processor + ' ' + this.memory);
    }
    }

    var myComputer = new OptiFlex();
    alert(myComputer.producer);
    alert(myComputer.processor);
    alert(myComputer instanceof OptiFlex);
    myComputer.showConfig();



    > And don't tell me we also have inheritance or interfaces!


    Sorry sis, no core implemented interfaces yet :-( :)

    And with inheritance it is complicated matter, because natively
    JavaScript/JScript implements prototype-based (not class-based)
    inheritance. It is flexible enough to be whatever you want it to be, so
    class inheritance can be very well emulated. In my sample I used the
    "classy" approach as I felt that would be the most pleasing for your
    eyes :)

    Yet it is not a real hard-coded inheritance like in C-languages. Say
    OptiFlex() and Dell() constructor are not in "super - extends"
    relations, these are just two independent functions fith the same core
    constructor. A production chain is being made only at the moment of
    instance creation and it disappear right after (try study
    OptiFlex.constructor and Dell.constructor)

    This emulation can be fully sufficient though for your tasks. If not,
    study the real mechanics of the prototype inheritance (look at
    ..prototype property and its manipulations).
    VK, Oct 5, 2006
    #4
  5. Guest

    VK ha scritto:

    > wrote:
    > > I see you defined fields. Is it possible to define also "methods" and
    > > "properties" like in .net?

    >
    > in .Net what? ;-) There is no such language, that's an interface atop
    > of a set of languages - including JScript.


    :)) to me they look all the same ;)

    You must be working with C#,
    > and yes, fields (simple properties) and methods are welcome. You only
    > cannot define compound properties with getter and setter (simply
    > speaking something called as property but acting like method() )


    Actually, it seems to me that not many people are organizing
    javascript
    into "objects". Is this a right impression? Or just didn't see enough
    code ?

    Also it's probably a little confusing (to me) that the object
    definition "looks like" a function.

    > > And don't tell me we also have inheritance or interfaces!

    >
    > Sorry sis, no core implemented interfaces yet :-( :)
    >
    > And with inheritance it is complicated matter, because natively
    > JavaScript/JScript implements prototype-based (not class-based)
    > inheritance. It is flexible enough to be whatever you want it to be, so
    > class inheritance can be very well emulated. In my sample I used the
    > "classy" approach as I felt that would be the most pleasing for your
    > eyes :)
    >
    > Yet it is not a real hard-coded inheritance like in C-languages. Say
    > OptiFlex() and Dell() constructor are not in "super - extends"
    > relations, these are just two independent functions fith the same core
    > constructor. A production chain is being made only at the moment of
    > instance creation and it disappear right after (try study
    > OptiFlex.constructor and Dell.constructor)
    >
    > This emulation can be fully sufficient though for your tasks. If not,
    > study the real mechanics of the prototype inheritance (look at
    > .prototype property and its manipulations).


    I need to digest these concepts, to see the difference with the
    concepts I am used to ...

    Thanks for the nice examples...

    -P
    , Oct 5, 2006
    #5
  6. Ian Collins Guest

    wrote:
    >
    > Actually, it seems to me that not many people are organizing
    > javascript
    > into "objects". Is this a right impression? Or just didn't see enough
    > code ?
    >

    Probably the latter.

    > Also it's probably a little confusing (to me) that the object
    > definition "looks like" a function.
    >

    It is. JavaScript functions are objects.


    --
    Ian Collins.
    Ian Collins, Oct 5, 2006
    #6
  7. VK Guest

    wrote:
    > :)) to me they look all the same ;)


    Aha, ".Net rulez", I know... :)

    > Actually, it seems to me that not many people are organizing
    > javascript into "objects". Is this a right impression?


    More like an allusion :) The trick is of *how* do they organize their
    objects. One of powers of JavaScript is that it was made a bit like
    Esperanto, so everyone would find a similarity to scream "Hey, it's
    just like in my language!" Some people see just a classy inheritance
    (with some limitations to work around) and just stay in their. That
    maybe helpful if you are using javascript in conjuction with another
    C-like language and you don't want to switch the "programming pattern"
    in your mind every single minute. I'm the one of this kind very often.
    You may look at prototype.js library (google for "prototype.js") for a
    sample of classy emulation in a prototype-based language.

    > Also it's probably a little confusing (to me) that the object
    > definition "looks like" a function.


    In javascript the call context defines everything. If you call a
    function as constructor it acts like consructor, if as a function - it
    acts as a function. "Do what's you want but do don't fling me in dat
    brier-patch" :)

    > I need to digest these concepts, to see the difference with the
    > concepts I am used to ...


    I cannot sampling prototype inheritance at 3am :) but if you search
    this newsgroup for "prototype inheritance" you'll find something useful
    for sure. And there are real gurus here to explain all details.
    VK, Oct 5, 2006
    #7
  8. wrote:
    > VK ha scritto:
    >
    >> wrote:
    >>> What that syntax "var busyBox = new ... " actually means ?? What is
    >>> NEW.

    >>
    >> new ConstructorName() means "make me a new instance of this
    >> kind of object and return a reference to this instance".


    It would be more accurate to say that his creates a new instance of a
    javascript object and then performs a number of (implied or direct)
    actions upon that object. The javascript object is dynamic and can have
    properties added to it at any time. Constructor functions are just a
    mechanism for having like changes applied to new Objects, so that the
    resulting objects can be regarded as being of some 'type' or 'class'.

    > This is really nice. I didn't get that it is possible to "create"
    > objects in javascript. Beautiful!


    It is actually almost impossible not to create objects in javascript.
    Presumably you mean objects of some programmer defined 'type' or
    'class'.

    > I see you defined fields. Is it possible to define also "methods" and
    > "properties" like in .net?


    Methods are just references to functions assigned to properties of
    objects _and_ called as through property accessors (as the - this -
    value in a function is determined _only_ by how the function is called.
    That is, there is no inherent relationship between a function that is
    used as a method and any object instance that is using it.).

    > And don't tell me we also have inheritance or interfaces! Do we?

    <snip>

    Native inheritance is through the prototype chain. An object instance
    is made into the prototype of any new object, usually by assigning a
    reference to that object to the - prototype - property of the
    constructor (before it is use to create new objects) or by modifying
    the object that is already the default prototype of the constructor. If
    an attempt is made to look up properties on an object and that object
    does not have such properties itself its prototype is examined to see
    if it has such a property and if it does the value of that property is
    used (else the prototype's prototype is examined (if it has one)).

    Thus the object on the prototype of a newly created object provide
    default values (including default methods) for that object. Once a
    different value is written to the object itself the value on objects on
    the prototype chain are 'masked' and their values will no longer be
    returned if the value of the property is read from the object instance.

    The prototype chain cannot branch (it is a chain not a tree) so there
    is no equivalent to multiple inheritance in javascript (though the
    concept can be implemented once an appreciation of 'classes' in
    javascript not being a native characteristic of the language but
    instead being a programmer imposed design concept that is manifest in
    making similar modifications to instances of the single native object
    'class'.

    Javascript's prototype inheritance usually resembles something like:-

    function Dell() {
    ;
    }
    Dell.prototype.producer = 'Dell, Inc.';
    Dell.prototype.URL = 'http://www.dell.com';


    function OptiFlex (processor, memory) {
    if(processor){
    this.processor = processor;
    }
    if(memory){
    this.memory = memory;
    }
    }
    OptiFlex.prototype = new Dell();
    OptiFlex.prototype.processor = 'Athlon 1.2GHz';
    OptiFlex.prototype.memory = '128Mb';
    As there are no 'classes' in javascript and all objects are the
    actually the same type of object, just modified in differing ways, the
    sense in which an interface is defined is only multiple objects having
    the sharing a sub-set of defined methods and properties. It is trivial
    to implement that.

    Because the objects in javascript are dynamic, in addition to defining
    the like-sub-set of properties and methods that might be called an
    interface on groups of 'classes', and interface may be added to an
    object. For example, suppose an interface known as "X" defined - get -,
    - put - and - size - methods, a factory function could be used that
    would take an object of any 'class' as its argument and add the
    interface to the object:-

    function augmentWithX_Interface(obj){
    var storage = [];
    obj.get = function(key){
    ...
    };
    obj.put = function(key, value){
    ...
    };
    obj.size = fucntion(){
    ...
    };
    return obj;
    }

    var dellWithX = augmentWithX_Interface( new Dell() );

    (Note: this example is closure-based in its mechanism so it is
    something you will not want to even consider using until you fully
    understand prototype inheritance).

    Richard.
    Richard Cornford, Oct 5, 2006
    #8
  9. wrote:

    [snip]

    > Actually, it seems to me that not many people are organizing
    > javascript
    > into "objects". Is this a right impression? Or just didn't see enough
    > code ?


    Hi

    If you search this news group or on a web search engine, you will find
    a vast amount of writing and web sites on the subject of object
    oriented style programming in Javascript.

    Also have a look at a good book:<URL : http://www.davidflanagan.com/>

    > Also it's probably a little confusing (to me) that the object
    > definition "looks like" a function.


    In rough terms, a Function in Javascript can act as a function, a
    constructor for your own objects, and a method of those objects. The
    "this" keyword is central to its function as a constructor and a
    method, and your research should include understanding "this"
    thoroughly.

    Regards

    Julian Turner
    Julian Turner, Oct 5, 2006
    #9
  10. Guest

    Julian Turner ha scritto:

    > wrote:
    >
    > [snip]
    >
    > > Actually, it seems to me that not many people are organizing
    > > javascript
    > > into "objects". Is this a right impression? Or just didn't see enough
    > > code ?

    >
    > Hi
    >
    > If you search this news group or on a web search engine, you will find
    > a vast amount of writing and web sites on the subject of object
    > oriented style programming in Javascript.
    >
    > Also have a look at a good book:<URL : http://www.davidflanagan.com/>



    Thanks you very much for all the nice inputs. (I doubt any book can
    compete with you guys!! : ) )


    -P

    >
    > > Also it's probably a little confusing (to me) that the object
    > > definition "looks like" a function.

    >
    > In rough terms, a Function in Javascript can act as a function, a
    > constructor for your own objects, and a method of those objects. The
    > "this" keyword is central to its function as a constructor and a
    > method, and your research should include understanding "this"
    > thoroughly.
    >
    > Regards
    >
    > Julian Turner
    , Oct 5, 2006
    #10
    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. Fred
    Replies:
    3
    Views:
    326
    Alf P. Steinbach
    Aug 10, 2003
  2. Ming
    Replies:
    5
    Views:
    517
  3. Chih-Chao Lam

    What does *var mean?

    Chih-Chao Lam, Aug 25, 2006, in forum: Ruby
    Replies:
    12
    Views:
    162
  4. Magicloud Magiclouds

    What does $var mean?

    Magicloud Magiclouds, Feb 1, 2008, in forum: Ruby
    Replies:
    3
    Views:
    122
    Thomas Wieczorek
    Feb 1, 2008
  5. Lison Allen

    What does "var a = a || {};" mean?

    Lison Allen, Mar 26, 2011, in forum: Javascript
    Replies:
    0
    Views:
    91
    Lison Allen
    Mar 26, 2011
Loading...

Share This Page