473,387 Members | 1,724 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Inheriting from built-in objects

This is in IE 6...

I'm trying to inherit from built-in JS objects - Array, specifically.
However, I'm having some difficulties. I can add array elements to
the child object, and when I retrieve them, they're the same as what I
added. The problem is, the 'length' property remains zero no matter
how many elements the child contains:

<pair.js>
function pair_getValue()
{
return this.value;
}

function Pair( key, value )
{
this.key = key;
this.value = value;
}

Pair.prototype.getValue = pair_getValue;
<end of pair.js>

<map.js>
function map_add( key, value )
{
if( null != this.findKey( key )) return false;

this[ this.getSize() ] = new Pair( key, value );
// this.getSize() will return 0 - ignore for now!

return true;
}

function map_findKey( key )
{
// returns array index of key or null if not found
}

function map_getSize()
{
return this.length;
}

function map_getValue( key )
{
var i = this.findKey( key );

if( null == i ) return null;
return this[ i ].getValue();
}

function Map
{
}

Map.prototype = new Array();
Map.prototype.add = map_add;
Map.prototype.findKey = map_findKey;
Map.prototype.getSize = map_getSize;
Map.prototype.getValue = map_getValue;
<end of map.js>

<example>
var test = new Map();
test.add( 'some unique key', 'some value' );
test.getValue( 'some unique key' ); // Returns 'some value'
test.getSize(); // Returns 0 (zero)?!?
<end of example>

I also tried setting the 'this.length' property explicitly. The value
persists in the function where it was set, but returns to zero
afterwards. Does this have anything to do with it being native code?

The point of this map is to make using cookies easier (the full Map
object can parse strings) by making the contents atomic. This means
another object: Cookie. This inherits from Map. This brings another
problem: both objects have 'serialize' functions. Map concatenates
the key/value pairs using a given separator and the equals character.
Cookie then appends the 'expires', 'path', 'domain' and 'secure'
fields (if set) to the Map values. So far, I've used this structure:

function cookie_serialize()
{
var temp;

// ******************************************
temp = this.base.prototype.serialize();
// ******************************************
if( this.domain ) temp = temp + ';DOMAIN=' + this.domain;
// Date.toGMTString() was depreciated in v1.3
// and just calls Date.toUDTString()
if( this.expires ) temp = temp + ';EXPIRES=' +
this.expires.toUDTString();
if( this.path ) temp = temp + ';PATH=' + this.path;
if( this.secure ) temp = temp + ';SECURE';
return temp;
}

function Cookie()
{
this.base = Map;
// Set property defaults...
}

Cookie.prototype = new Map();
Cookie.prototype.serialize = cookie_serialize();

It works: Map.serialize (map_serialize) is called, but is it correct?
Why doesn't this.base.serialize() work?

Thanks in advance,
Mike

--
Michael Winter
M.Winter@[no-spam]blueyonder.co.uk (remove [no-spam] to reply)
Jul 20 '05 #1
3 3769
> I'm trying to inherit from built-in JS objects - Array, specifically.
However, I'm having some difficulties. I can add array elements to
the child object, and when I retrieve them, they're the same as what I
added. The problem is, the 'length' property remains zero no matter
how many elements the child contains: I also tried setting the 'this.length' property explicitly. The value
persists in the function where it was set, but returns to zero
afterwards. Does this have anything to do with it being native code?


Stick with objects if you can. You can't inherit array-ness. You can inherit the
values of a particular array. As you are discovering, that does not buy you very
much.

However, you can add methods to all arrays. For example,

Array.method('sum', function (s) {
if (!isNumber(s)) {
s = 0;
}
for (var i = 0; i < this.length; ++i) {
s += this[i];
}
return s;
});

var ten = [0, 1, 2, 3, 4].sum();

This adds a sum method to all arrays. (For the 'method' method, see
http://www.crockford.com/javascript/inheritance.html)

Jul 20 '05 #2
"Douglas Crockford" wrote on 11/11/2003:
I'm trying to inherit from built-in JS objects - Array, specifically. However, I'm having some difficulties. I can add array elements to the child object, and when I retrieve them, they're the same as what I added. The problem is, the 'length' property remains zero no matter how many elements the child contains:
I also tried setting the 'this.length' property explicitly. The value persists in the function where it was set, but returns to zero
afterwards. Does this have anything to do with it being native

code?
Stick with objects if you can. You can't inherit array-ness. You can inherit the values of a particular array. As you are discovering, that does not buy you very much.


I can appreciate that. Upon reflection, I don't think that Map, as an
array, would be very useful. It would have been in my original
implementation, but not so much now.

What about method overriding, and explicitly calling overridden parent
methods. Is this the proper way to go about it?

function map_serialize()
{
return 'some-string';
}

function Map
{
}

Map.prototype.serialize = map_serialize;

function cookie_serialize()
{
// Want to call Map.serialize() here:
this.parent.prototype.serialize(); // returns 'some-string' OK, but
correct?
}

function Cookie
{
this.parent = Map;
}

Cookie.prototype = new Map();
Cookie.prototype.serialize = cookie_serialize;

I'm also looking for an explanation as to how the Map object
referenced by Cookie.parent and Cookie.prototype are the same. This
approach is shown in the Client-side JavaScript Guide (v1.3), but no
reasoning is given. I'm not complaining that it works, just curious
why.

Mike

--
Michael Winter
M.Winter@[no-spam]blueyonder.co.uk (remove [no-spam] to reply)
Jul 20 '05 #3
> > Stick with objects if you can. You can't inherit array-ness.
You can inherit the values of a particular array.
As you are discovering, that does not buy you very
much.


I can appreciate that. Upon reflection, I don't think that Map, as an
array, would be very useful. It would have been in my original
implementation, but not so much now.

What about method overriding, and explicitly calling overridden parent
methods. Is this the proper way to go about it?


Check out the uber function in
http://www.crockford.com/javascript/inheritance.html

But caution: That is old-school thinking. Calling super classes in fine for
static languages, but this is a very dynamic language. Open your mind to the
many forms of object augmentation.

Jul 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Fuzzyman | last post by:
Hello, To create a classic (old style) class, I write : class foo: pass To do the equivalent as a new style class, I write : class foo(object):
15
by: JustSomeGuy | last post by:
this doesn't want to compile.... class image : public std::list<element> { element getElement(key k) const { image::iterator iter; for (iter=begin(); iter != end(); ++iter) { element...
1
by: john diss | last post by:
hello there everyone.. I have created a class called "ProcessLog" inheriting from XmlDocument and two classes ("UploadedItem", "ProcessError") inheriting from XmlElement. I then have two...
2
by: Peter Bates | last post by:
Hi, I'm just getting used to XSDObjectGen and i have the following question. Can i use a class inherited from a class generated by XSDObjectGen with XmlSerialize? Specifically, I have many...
4
by: Michael C# | last post by:
Does anyone know of any sample code for inheriting from the FileDialog or CommonDialog classes? I need to create a customized OpenFileDialog with some additional combo boxes on it. Thanks
4
by: Jeremy | last post by:
Hi Guys, How do I get all the inheriting types of a specific type I guess I can get this using reflection... Could someone pls point me to an example or resource thanks
1
by: Mantorok | last post by:
I have some controls such as a linkbutton and it is inheriting the colour from the style-sheet - how can I override this? Thanks Kev
2
by: Charles Law | last post by:
I want a set of controls that all have a border, like a group box. I thought I would create a base control containing just a group box from which my set of controls could inherit. Assuming that...
5
by: Ed Willis | last post by:
What is the best way to make a window read only for an inherited window. How do I access the all the controls such as textboxes etc. to make everything read only? I have an update window that I am...
6
by: Rob | last post by:
I have built a class called DTLineScrub that inherits Regex. All works well with the code below until I try to do a replace on a string. It then tells me that the object was not instantiated. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.