views:

53

answers:

2

I have a Web Service and it always brings back one object from my class that i defined on the server side.

in success method of $.ajax function i always get the this object. And on the client side i want to add couple of functions to display its properties quickier.

Is there a way do that ? (JSON object will return, i am worrying about that)

+1  A: 

You can extend javascript objects and add methods to it:

var json = { name: 'john' };
json.print = function() {
    alert('my name is ' + this.name);
};
json.print();
Darin Dimitrov
What is the difference of json.print = ... and json.prototype.print = .... ?
uzay95
A: 

As you already told me in your last question about this topic

http://stackoverflow.com/questions/3359676/how-can-i-add-a-function-to-json-object-which-has-type-attribute

you want to have a global functionally. Since you still can't add a function into json you should declare a global function like:

var props = (function(){
   var spacing = '';

   function props(json, deep) {
      if(typeof json === 'object'){
         for(var prop in json){
             if(typeof json[prop] === 'object'){             
                spacing += '   ';
                props(json[prop], true); 
             }
             else{                
               console.log(spacing, prop, ': ', json[prop]);
             }
         }
      }
   }

   return props;
}());
jAndy
Thank you for your help(really) but in this question i am not looking for inner properties of response object. Just wanted to add a couple of functions to every responses. By the way this answer better than the answers for http://stackoverflow.com/questions/3358811/finding-sub-properties-of-javascript-object
uzay95