JavaScript: Object Definition Extension

Discussion in 'Javascript' started by Gene Wirchenko, Nov 11, 2011.

  1. On Sat, 12 Nov 2011 16:46:28 +0000, John G Harris

    [snip]
    That appears to be about what I was thinking of. It was
    confusing code for me. I was looking for a JavaScript keyword "base"
    at first. Why is base a property rather than declared local with var?
    Does it even matter?
    Thank you.

    Sincerely,

    Gene Wirchenko
     
    Gene Wirchenko, Nov 14, 2011
    #21
    1. Advertisements

  2. Gene Wirchenko

    beegee Guest

    There is no javascript keyword called 'base'.

    The rule to remember in Javascript is declaring a variable with 'var'
    makes it local in scope. Leaving off the var puts the variable into
    global scope. You should get into a habit of using 'var'. Even
    global variables should be var'd at the global level instead of
    implicitly for clarity. The reason for this is speed; the
    interpreter searches first at the local scope then moves up reaching
    the global scope last.

    Bob
     
    beegee, Nov 15, 2011
    #22
    1. Advertisements

  3. <snip>

    It matters very much, and here's why :

    Lets assume that we're constructing one of your Commercial objects. In
    the constructor you need to create and set the extra properties that you
    have in Commercial objects, PropertyZoning and PropertyUse. This is
    straightforward :

    this.PropertyZoning = PropertyZoning;
    this.PropertyUse = PropertyUse;

    But you also need to create and set the properties that you have in
    RealEstate objects, PropertyLocation and PropertyValue. You could do it
    directly in the same way, but this is repeating code that is already in
    the RealEstate constructor.

    Rewriting code is bad and should be avoided if possible. It is tedious,
    even with copy and paste. It is error prone : you might get it wrong.
    And one day you will want to change the base constructor so you have to
    edit code all over the program to keep it all in step. (Will you get
    that right?)

    It's much better to run the RealEstate code to do the job for you. Can
    you do this?

    First, you need to be able run RealEstate as an ordinary function as we
    don't want to build another new object. Can this be done? Yes it can!
    You simply don't write 'new' in front of it. (Attention Mr Crockford).

    Second, you need to call RealEstate with the new Commercial object as
    the 'this' value. This is easy using RealEstate's call method :

    RealEstate.call(this, [PropertyLocation, PropertyValue]);
    // 'this' is the new Commercial object

    However, back when I was writing my javascript note 'call' was new and
    not in all popular browsers so an older clumsier way was needed.
    Remember that if you do a.b() then the function b is called with the
    'this' value set to a. So, we create a property of the Commercial object
    under construction with the name 'base' (because that's what Netscape
    called it and there isn't a really better name), and use it to hold the
    function object RealEstate. Now it's easy :

    this.base(PropertyLocation, PropertyValue);
    // 'this' is the new Commercial object

    After using it you delete the temporary property 'base' because it's no
    use for any other job.

    There you have it. No matter how complicated the base constructor is,
    and no matter if the base itself is derived, it runs properly in the
    derived constructor. Your only effort is to feed it the right arguments.

    John
     
    John G Harris, Nov 15, 2011
    #23
  4. Gene Wirchenko

    beegee Guest

    Uh, I need to correct this. Leaving off the var, puts the variable
    into the 'current' scope, I believe. This is not necessarily global.
    The advise I gave still stands. If you want the variable to be in the
    scope of your function object, use the 'this' prefix.

    Bob
     
    beegee, Nov 15, 2011
    #24
  5. Gene Wirchenko

    beegee Guest

    Okay. You are right, I think I did misunderstand Crockfords object()
    method.
    No, I meant property values and a child object is one whose __PROTO__
    member points to the parent object. I cannot, however, produce an
    example where the child inherits stale parent values. This is what I
    understood that Crockford's method avoids but I either misunderstood
    or the article I read was wrong.

    Implement the incomplete set of OO functionality depending on the
    needs of the project? How is that even comprehendable to a programmer
    coming to an existing code base much less maintainable?
    It's often called factoring and it's very good for readability,
    extendability, and scalability.
    Well, you and I will have to disagree on this if you mean adding OO.
    Focus on separation of UI and data will make the codebase agile (see
    above for the 'bility's).
     
    beegee, Nov 15, 2011
    #25
  6. On Tue, 15 Nov 2011 15:43:34 +0000, John G Harris

    [snip]
    My question is why a property. Why not just
    var base=...
    As there could be multiple levels, "base" is potentially
    confusing. Visual FoxPro uses "parent".
    But if you had declared it with var, you would not need to do
    this.

    What am I missing about base's definition?
    Sincerely,

    Gene Wirchenko
     
    Gene Wirchenko, Nov 15, 2011
    #26
  7. <snip>

    To expand on what Jake said :

    You've done

    this.base = RealEstate;

    so both this.base and RealEstate refer to the same function object. When
    you call that function it does

    this.PropertyValue = PropertyValue;

    and suchlike during its execution. It's important here that 'this' is
    the new Commercial object under construction, not some other object such
    as the Global object.

    If base is declared as a local (or global) var object then the 'this'
    value will be wrong. On the other hand, if base is a property of the
    object under construction then the 'this' value will be the object under
    construction, as desired.

    Hence, base is a property of the new object, needed only temporarily for
    this specialised purpose.

    As for its name, it only lasts for 3 lines so the name is not all that
    important. As it takes a whole sentence, if not several, to say what
    it's for, I doubt if a good name is possible. E.g 'thisConveyer' says
    what it does, but not to where.

    Regardless, you can call it something else if you want to. I won't mind.

    John
     
    John G Harris, Nov 16, 2011
    #27
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.