Extending Array?

O

optimistx

Some people recommend not to add methods and properties to objects like
Array. The reason being that the names of those might collide with other
programs on the same page made by others.

The exception to this seems to be methods, which are included in the newer
versions of javascript: they might be coded if not existing already.

How would I then extend an Array? Define my own MyArray in my own namespace,
and use Array as a prototype or ancestor for it? Then use it like

myarray1 = new MyArray (...);
myarray2 = new MyArray (...);
myarray1.nicenewmethod();

or something else: simple, elegant, educational, intuitive, robust,
efficient, memoryefficient,
programmerfriendly, socially polished, expertlike, artistic, beautiful, ...
?

Example of such a code?
 
R

Richard Cornford

Some people recommend not to add methods and properties to
objects like Array.

More likely it is not adding methods/properties to the -
Array.prototype - that being recommended against.
The reason being that the names of those
might collide with other programs on the same page made by
others.

That probably isn't the main reason for recommending not modifying the
array's prototype. More the issue relates to the use of for-in to
iterate over the enumerable properties of objects, where any
properties added will enumerable, and so while you can know with
certainty whether your code used for-in or not you cannot be certain
that third party code will or will not. Thus there are circumstances
where modifying the built-in prototypes may provoke issues.
The exception to this seems to be methods, which are included in
the newer versions of javascript: they might be coded if not
existing already.

Adding those methods will result in enumerable properties, and so will
provoke the/any issues that are of relevance.
How would I then extend an Array?

Prototype based inheritance does not work with Arrays because arrays
employ a specialised internal [[Put]] in order to react to their
significant 'array index' and - length - property names. This internal
[[Put]] method is not inherited by any object for which an array
object appears either as its [[Prototype]] or on its prototype chain.
Thus any object that inherits from an array on its prototype chain may
gain all the methods of an array (and most array methods are specified
as working with any object type) but it will not gain the special
qualities that make an object an Array.

The equivalent of 'extending'/'sub-classing' and Array is usually done
by having a special method/function that creates ordinary arrays and
directly modifies those, adding any methods by assigning references to
functions to the created array's prpeties. That way the new object
retains the Array's special [[Put]] method and continues to inherit
the array methods, but also has any new properties/method needed.
Obviously the added methods/properties of these modified array objects
are enumerable, but that is true of the programmer-defined methods/
properties of any 'class' in javascript.
Define my own MyArray in my own namespace,

What do you imagine "namespace" means here?
and use Array as a prototype or ancestor for it? Then use it like

myarray1 = new MyArray (...);
myarray2 = new MyArray (...);
myarray1.nicenewmethod();

or something else: simple, elegant, educational, intuitive,
robust,
efficient, memoryefficient,
programmerfriendly, socially polished, expertlike, artistic,
beautiful, ...
?

Example of such a code?

Archives?

Richard.
 
O

optimistx

Richard said:
What do you imagine "namespace" means here?

var MYINITIALS = MYINITIALS || {}

where MYINITIALS may be e.g. SES or backwards internet address owned by me .

Thus only one global variable is defined directly by me.

___

If I understood your thorough and kind (thank you!) reply right it is good
style to write e.g.

var a = new Array(1,2,3,4);
(or even better:
var a = [1,2,3,4];
}

and define new methods and properties to a like this:

a.newmethod = f; // f is a function definition or function expression
a.newproperty = p; //

but this could produce issues:

Array.prototype.newmethod = f;
Array.prototype.newproperty =p;

[1,2,3,4].newmethod();
[1,2,3,4].p = 'interesting_value';

?

That actually answers my problem practically for almost all situations. How
much is the in memory usage?

I tried to test with Firefox 3.0.13 and Chrome in windows xp and task
manager's memory display.The results were mixed and confusing.

When generating one million very simple almost empty
objects Firefox seemed to reserve about 200 bytes for each. When a simple
method was
added to each object (not their prototype)the same method for each, the
memory usage increased with about 70 bytes each. I was expecting much
smaller value, the size of a memory pointer (8-10 bytes?).

The memory usage of Chrome was so small that I must have made some error in
the setup.

To define one method in the prototype instead of each generated object would
anyhow add a pointer (or similar?reference) to each object. Or not? Is the
difference in reality around 8-10 bytes per object? If so, and when memory
space is measured in gigabytes nowadays, it is
ridiculous to even think of saving some bytes, even kilobytes every now and
then. 'Premature optimization' ?...

Better method for measuring memory usage?
 
V

VK

How would I then extend an Array? Define my own MyArray in my own namespace,
and use Array as a prototype or ancestor for it? Then use it like

myarray1 = new MyArray (...);
myarray2 = new MyArray (...);
myarray1.nicenewmethod();

or something else: simple, elegant, educational, intuitive, robust,
efficient, memoryefficient,
programmerfriendly, socially polished, expertlike, artistic, beautiful, ...
?

<script type="text/javascript">

function SuperArray() {
this.method1 = arguments.callee.xMethod;
}
SuperArray.xMethod = function() {
window.alert(
'SuperArray says: ' + this
);
}

function SuperDuperArray() {
SuperArray.call(this);
this.method2 = arguments.callee.xMethod;
}
SuperDuperArray.xMethod = function() {
window.alert(
'SuperDuperArray says: ' + this
);
}

function SuperDuperMuperArray() {
var arr = arguments[0] || [1,2,3];
SuperDuperArray.call(arr);
arr.method3 = arguments.callee.xMethod;
return arr;
}
SuperDuperMuperArray.xMethod = function() {
window.alert(
'SuperDuperMuperArray says: ' + this
);
}

var a = new SuperDuperMuperArray([1,2,3]);
a.method1();
a.method2();
a.method3();

var b = new SuperDuperMuperArray();
b.method1();
b.method2();
b.method3();

</script>
 

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
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top