it understanding every javascript object had prototype if 1 object.prototype
i have asp.net webservice proxy gets me participant data server. part works fine. here's code:
atomwebserviceproxy.getinteractionparticipants(interactionid, function (participantsresult) { var assetid, pendingpathfetches; participants = participantsresult; pendingpathfetches = participants.length; document.title = participants.length + " participants:"; (var = 0; < 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 error: " + error.get_message()); }, participants[i] ); } }, function (error) { alert(error.tostring()); });
you may notice there nested calls , have added path property each participant object, expressing in object model fact path data particular participant. works fine.
then tried add method prototype this:
var foo = participants[0]; foo.prototype.redraw = function(map) { ... //code redraws };
to great surprise, got exception claiming prototype null:
javascript runtime error: unable set property 'redraw' of undefined or null reference
what going on here?
this question seems address problem cannot see how apply information situation, since not know how refer constructor not defined in code.
in case useful info, inspection of participants.constructor in immediate window @ run-time appears indicate constructor array()
should write constructor copies fields object , sets prototype? if there javascript equivalent c# reflection don't have rewrite every type of query result object? note coma answered part of question in comments while updating question.
in particular case, you're better off adding function object itself, not trying add prototype.
but answer question "how can set prototype when don't know name of constructor?":
you can't set prototype of existing object (not in standard way). prototype assigned @ construction time , cannot (in standard way) changed. can change properties of object's prototype, not object is prototype.
the prototype of object not available property on called prototype
. prototype
property relates functions. it's object assigned prototype of objects created via function using new
expession (e.g., new foo()
assigns foo.prototype
prototype of newly-created object).
you can prototype of object in es5 via object.getprototypeof
. in pre-es5 implementations, it's available via non-standard __proto__
property. (and in of implementations, __proto__
can written to, why said couldn't swap out prototype object in standard way — can in non-standard way in implementations support it.)
so in es5 environment, extend prototype of object this:
object.getprototypeof(theobject).newstuff = "foo";
but beware that changes prototype, , other objects may using it! example:
// constructor function, , prototype assign objects created function foo() { } foo.prototype.hi = function() { console.log("hi"); }; // create 2 objects var f1 = new foo(); var f2 = new foo(); // add prototype of `f1`: object.getprototypeof(f1).there = function() { console.log("there"); }; // since `f1` , `f2` *share* same prototype object, // `f2` has too! f2.there(); // "there"
Comments
Post a Comment