JavaScript: Object Definition Extension

G

Gene Wirchenko

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?
There are obviously several minor variations possible. For instance, you
can make use of the 'call' function in constructors if you don't need to
go too far back in time.

Thank you.

Sincerely,

Gene Wirchenko
 
B

beegee

     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?

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
 
J

John G Harris

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?
<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
 
B

beegee

On Nov 14, 3:26 pm, Gene Wirchenko <[email protected]> wrote:
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.

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
 
B

beegee

a) There are two functions - object and F - and so two function objects.
Both are created at program start up. I don't see how either can be
called intermediate, but if one can then both are.

b) The whole point of inheritance is to inherit. You only want to avoid
inheriting if you've supplied the wrong prototype object. And the use of
the object function doesn't stop you picking up unwanted values from the
wrong kind of prototype object.

Okay. You are right, I think I did misunderstand Crockfords object()
method.
You don't mean "existing property values", do you? Inherited property
names, perhaps?

What on earth is a child object?

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.

You only emulate the features you need : the purpose being that a
particular feature is needed in your application.

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?
Calling extra functions can't be good for performance.

It's often called factoring and it's very good for readability,
extendability, and scalability.
Far better to focus on making your code maintainable in two years time
when the customer wants changes.

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).
 
G

Gene Wirchenko

On Tue, 15 Nov 2011 15:43:34 +0000, John G Harris

[snip]
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

My question is why a property. Why not just
var base=...
called it and there isn't a really better name), and use it to hold the

As there could be multiple levels, "base" is potentially
confusing. Visual FoxPro uses "parent".
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.

But if you had declared it with var, you would not need to do
this.

What am I missing about base's definition?
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.

Sincerely,

Gene Wirchenko
 
J

John G Harris

On Tue, 15 Nov 2011 15:43:34 +0000, John G Harris

[snip]
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

My question is why a property. Why not just
var base=...
called it and there isn't a really better name), and use it to hold the

As there could be multiple levels, "base" is potentially
confusing. Visual FoxPro uses "parent".
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.

But if you had declared it with var, you would not need to do
this.

What am I missing about base's definition?

<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
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,073
Messages
2,570,539
Members
47,197
Latest member
NDTShavonn

Latest Threads

Top