oo code

I

Ira

Hi, new to the group. I've been doing some object-oriented coding
with JavaScript.
What I've been using works, but if there's a better way, awesome.

I've got a prototype-defined method:

MyObj.prototype.equals = function(name) {
return (this.name == name);
}

And I've got an array of objects along with a lookup function that
returns an index for success, -1 for fail

var ObjArray = new Array();

Array.prototype.lookup_table = function(table_name) {
for (var i = 0; i < ObjArray.length; i++) {
if (this.equals(name)) {
return i;
}
}
return -1;
}

The problem (if you havent' noticed yet) is defining a general-purpose
lookup function for Arrays.
The next iteration of my code will probably have a sort of 'mother'
object that has an Array of objects as a member and then have the
lookup as a prototype of the mother-object unless I can think of a
better solution.
Any suggestion would be appreciated.

Cheers
 
L

-Lost

Ira said:
Hi, new to the group. I've been doing some object-oriented coding
with JavaScript.
What I've been using works, but if there's a better way, awesome.

Nope, as far as I know using prototype is the only way to insure that all of your object
instances have that method or property available.

Of course, if this is not the desired effect you could assign on a per-object basis.

var newObject = new Object();
newObject.funcType = new Function('return arguments.callee.name;');

funcType will only exist for newObject.
I've got a prototype-defined method:

MyObj.prototype.equals = function(name) {
return (this.name == name);
}

And I've got an array of objects along with a lookup function that
returns an index for success, -1 for fail

var ObjArray = new Array();

Array.prototype.lookup_table = function(table_name) {
for (var i = 0; i < ObjArray.length; i++) {
if (this.equals(name)) {
return i;
}
}
return -1;
}


And you realize that you overloaded the Array object and not *your* custom array object,
right?
The problem (if you havent' noticed yet) is defining a general-purpose
lookup function for Arrays.

I thought the above is what you were referring to.

You do, or do not want to overload the Array object?

<snip>

-Lost
 
R

RobG

Nope, as far as I know using prototype is the only way to insure that all of your object
instances have that method or property available.

The prototype property is one mechanism for inheritance, there are
others such a constructor, e.g.:

function C(){
this.publicFunction = function(){alert('hey')};
}

var a = new C();
var b = new C();

Both a and b have a publicFunction method.
 
R

RobG

Hi, new to the group. I've been doing some object-oriented coding
with JavaScript.
What I've been using works, but if there's a better way, awesome.

I've got a prototype-defined method:

MyObj.prototype.equals = function(name) {
return (this.name == name);

}

Presumably somewhere previously you declared MyObj as a constructor
function:

function MyObj( name ){
this.name = name;
}

so now you can do:

var a = new MyObj('foo');
alert( a.equals('foo') ); // true

Which means that a's name is equivalent to the string 'foo'. A better
name for the method is 'checkName' or something that indicates what it
does. To me, the equals function should check if the variable passed
references the same object or not (which is a pretty trivial
function).

And I've got an array of objects along with a lookup function that
returns an index for success, -1 for fail

var ObjArray = new Array();

Array.prototype.lookup_table = function(table_name) {
for (var i = 0; i < ObjArray.length; i++) {
if (this.equals(name)) {
return i;
}
}
return -1;

}


Where is table_name used in the function?

The problem (if you havent' noticed yet) is defining a general-purpose
lookup function for Arrays.

No, I hadn't - but thanks for the hint. Are you trying to see if one
of the elements in the array contains a value that is equivalent to
the string passed to the function? i.e.

Array.prototype.lookup_table = function(s) {
var i = this.length;
while (i--) {
if (this == s) return i;
}
return -1;
}

Probably better to return null if a value isn't found, or nothing - in
which case it will return undefined.

The next iteration of my code will probably have a sort of 'mother'
object that has an Array of objects as a member and then have the
lookup as a prototype of the mother-object unless I can think of a
better solution.
Any suggestion would be appreciated.

You should make the lookup function a method of your 'mother object'
so it doesn't extend the native Array object.
 

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

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top