Different ways to declare methods/properties of an object

Discussion in 'Javascript' started by Sergey Ilinsky, Mar 28, 2005.

  1. Well, I've been working with JS for three years and have a great
    experience here.
    But! I still have no really acceptable answer to the following
    question:
    What is the principle difference between declaring methods/properties
    in the constructor function body and via prototypes.

    Are there any real GURUs? Let's discuss the issue.

    P.S. Currently I am working on XUL implementation for IE, so there is
    a lot of OOP code written in JS to be potentially optimized
    http://www.magicofphoto.com/web/
    Sergey Ilinsky, Mar 28, 2005
    #1
    1. Advertising

  2. Sergey Ilinsky wrote:


    > What is the principle difference between declaring methods/properties
    > in the constructor function body and via prototypes.


    There is one prototype object shared by the instances created with one
    constructor function thus if you want a "class" of objects then it makes
    sense to at least put all methods on the prototype as that is far more
    efficient and far more meaningful, a method defines the dynamic
    behaviour of an object and is usually expected to be consistent for all
    instances in a "class". Thus methods should be created by adding a
    function property to the prototype e.g.

    function God (name) {
    this.name = name;
    }
    God.prototype.praise = function () {
    alert('All hail ' + this.name);
    };

    Now all instances e.g.
    var god1 = new God('Kibo');
    var god2 = new God('Xibo');
    do not have their own function property named praise but share one via
    the prototype and that is the proper way if all instances are supposed
    to have the same behaviour.

    And if you did

    function God (name) {
    this.name = name;
    this.praise = function () {
    alert('All hail ' + this.name);
    }
    }

    then each instance created with new God('someName') would have its own
    function property named praise but as the body is identical you would
    just waste storage space then.

    So having

    function SomeConstructorFunction (args) {
    ...
    }
    SomeConstructorFunction.prototype.methodName = function (...) {
    ...
    };

    is just JavaScript's operational way to declare a common method for a
    "class" of objects.


    For normal non function properties it is different, usually in object
    oriented development those properties make up the data of the object
    instance which make it unique so there it usually doesn't make sense to
    have a property on the prototype, unless for instance you wanted to have
    a default value for a property.


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Mar 28, 2005
    #2
    1. Advertising

  3. On 28/03/2005 10:49, Sergey Ilinsky wrote:

    > What is the principle difference between declaring methods/properties
    > in the constructor function body and via prototypes.


    From a performance point of view, using the prototype object uses
    less memory and allows for quicker object instantiation as the
    properties are only created once, and only exist in one place. A
    possible advantage to the use of constructor-defined properties is
    that the property will exist on the object itself and will therefore
    take less time to find during member look-up operations. That said, I
    think it would take a long prototype chain to necessitate code like that.

    As for behavioural differences, there is clearly no other way to
    create private members, or privileged public members, than through
    creation in the constructor during instantiation.

    Mike


    Hmmm, that seemed awfully rushed...

    --
    Michael Winter
    Replace ".invalid" with ".uk" to reply by e-mail.
    Michael Winter, Mar 28, 2005
    #3
    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. =?Utf-8?B?Q3VydF9DIFtNVlBd?=

    RE: Ways to restrict access to some methods in .net Components?

    =?Utf-8?B?Q3VydF9DIFtNVlBd?=, Sep 13, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    369
    =?Utf-8?B?ZG90bmV0dGVzdGVy?=
    Sep 13, 2005
  2. =?Utf-8?B?ZG90bmV0dGVzdGVy?=

    Ways to restrict access to some methods in .net Components?

    =?Utf-8?B?ZG90bmV0dGVzdGVy?=, Sep 13, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    318
    =?Utf-8?B?ZG90bmV0dGVzdGVy?=
    Sep 13, 2005
  3. Replies:
    20
    Views:
    2,199
  4. Sun
    Replies:
    27
    Views:
    741
    Steven D'Aprano
    Feb 16, 2008
  5. Kenneth McDonald
    Replies:
    5
    Views:
    311
    Kenneth McDonald
    Sep 26, 2008
Loading...

Share This Page