It was my understanding that every javascript object had a prototype even if it were just the one from Object.prototype
I have an asp.net webservice proxy that gets me participant data from the server. That part works fine. Here's the code:
atomWebServiceProxy.GetInteractionParticipants(interactionId,
function (participantsResult) {
var assetId, pendingPathFetches;
participants = participantsResult;
pendingPathFetches = participants.length;
document.title = participants.length + " participants:";
for (var i = 0; i < participants.length; i++) {
assetId = participants[i].ASSETID;
document.title += " " + participants[i].DISPLAYID;
atomWebServiceProxy.GetInteractionPath(interactionId, assetId,
function (pathResult, participant) {
participant.path = pathResult;
pathResult.participant = participant;
if (--pendingPathFetches == 0) {
setTimeout(afterInit, 20);
}
},
function (error, participant) {
alert(participant.DISPLAYID + " reported this error: " + error.get_message());
},
participants[i]
);
}
},
function (error) {
alert(error.toString());
});
You may notice that there are nested calls and I have added a path property to each participant object, expressing in an object model the fact that it is the path data for that particular participant. This also works fine.
Then I tried to add a method to the prototype like this:
var foo = participants[0];
foo.prototype.redraw = function(map) {
... //code that redraws
};
To my great surprise, I got an exception claiming that prototype is null:
JavaScript runtime error: Unable to set property 'redraw' of undefined or null reference
What is going on here?
This question seems to address the problem but I cannot see how to apply this information to this situation, since I do not know how to refer to a constructor that is not defined in my code.
In case this is useful info, inspection of participants.constructor in the immediate window at run-time appears to indicate that the constructor was Array()
Should I write a constructor that copies the fields into an object and sets a prototype? If so is there a javascript equivalent to C# reflection so I don't have to rewrite this for every type of query result object? NOTE coma answered this part of the question in comments while I was updating the question.