How to wrap return type of ajax call in success with function
开发者_如何学PythonI 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)
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();
As you already told me in your last question about this topic
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;
}());
精彩评论