Consider this object:
function Cow() {
var self = this;
self.color = "blue";
self.weight=55;
}
Cow.prototype.Speak = function(){
var self=this;
alert('moo. i\'m a ' + self.color + ' cow.');
}
var bessy = new Cow();
I'm running into an issue when I try to do $.ajax(...) and pass in 'bessy' as the data parameter. My intention is for the data properties to be serialized and passed over the wire But it will actually execute Speak() upon the ajax call. This is an overly simple example, but highlights the problem I'm having.
To solve this, I've created a function that accepts an object and conditionally deletes members (such as functions). I then pass a copy of the target object to that function, and use my new simplified copy of the object for the ajax call. This seems really cumbersome. Is this a common problem? How do others deal with this? It seems like overkill to create separate DTO JS objects for this purpose because then, when I add a new property, it would need to be added in two places.
Would appreciate any thoughts.
Thanks... -Ben
this, it would be easy to instead pass that property object rather than the entire instance, and would only require modifying that one object to add more properties.Cow.prototypebtw.optionsare usually handled. You have an instance, in this caseCow, that has methods (Speak) and options (weightandcolor).