This example analyzes the use of JavaScript objects in JSON format. Share to everyone for your reference, specific as follows:
Format:
ObjectName = {
property1:value1,
property2:value2,
...,
Propertyn:valuen
}
The property is the object's properties, and value is the object's value, which can be a string, number, or object.
Example one: values are strings and numbers
var user={
Name: "user1",
age:18
};
Example two: values are strings and objects
The Var user={//user itself is an object
name: "user1",
job:{//job is also an object
salary:3000,
title:programmer
}
}
Example three: A value can also be a function
var user={//user itself is an object
name: "user1",
age:18,
getname:function () {//getname for method
return THIS.name
}
}
The following definition formats for the Zpvisitor class are often used in actual enterprise projects:
var zpvisitor = {init:function () {var url = zpconstant.ajax_server + "&a=hello" + "&sid=" + math.random ();
$.getjson (Result) {//#TODO Cross-domain request?
if (result.done) {var logininfo = result.data.name + " Hello!"; if (!result.data.login) {logininfo + = " <a href=\" "+ Zpconstant.myzp_domain +" Index.php?c=member&a=logi
n\ "> Login </a>/<a href=\" "+ Zpconstant.myzp_domain +" index.php?c=member&a=regist\ "> Registration </a>"; else {logininfo = " <a href=\" "+ Zpconstant.myzp_domain + index.php?c=member&a=logout\" > Logoff </
A> ";
$ ("#login-info"). HTML (logininfo);
else {Popalert (result.msg); }
});
End Getjson},//End Init method//Collection commodity collect:function (PID) {var pid = parseint (PID);
if (isNaN (PID)) return false;
var url = zpconstant.ajax_server + "&a=collect&pid=" + pid + "&sid=" + math.random ();
$.getjson (url,function (Result) {if (Result.done) { Popalert (RESULT.MSG);
else {Popalert (result.msg); //End Getjson method},//End Collect method//Browse history vhistory: {Get:function (callback) {var items_str = GetCookie (Zpcon Stant.
View_history_ckname);
if (items_str = null) return; var items = Items_str.split (","). Reverse (). Join (","), url = zpconstant.ajax_server + "&a=viewhistory +" &items
= "+ Items +" &sid= "+ math.random ();
TODO requests Data $.getjson to the server (url,function (response) {if (Response.done) {callback (response.data,items);
else {return false;
//End Getjson method},//End Get Method add:function (PID) {var pid = parseint (PID);
if (isNaN (PID)) return;
var cookiestr = GetCookie (zpconstant.view_history_ckname);
if (cookiestr) {var PIDs = Cookiestr.split (","), _temppids = []; for (Var i=0;i<pids.length;i++) {if (pids[i]!= pid) {//If already exists, remove previously written data _temppids.push (parseint (pids[i))
);
} _temppids.push (PID);//Put the new data to the end of the array Setcookie (Zpconstant.view_history_ckname, _temppids.reverse (). Slice (0,5). Reverse (). Join (","));
else {Setcookie (zpconstant.view_history_ckname, PID);
},//End Add Method Clear:function () {Setcookie (Zpconstant.view_history_ckname, ',-100);
},//End vhistory//shopping cart deleted merchandise record drophistory:function (spid) {var cookiestr = GetCookie (zpconstant.cartdel_ckname);
if (cookiestr) {var Idsdel = Cookiestr.split (",");
for (Var i=0;i<idsdel.length;i++) {Idsdel[i] = parseint (idsdel[i));
} if ($.inarray (spid,idsdel) = = 1) {Idsdel.push (spid);
Setcookie (Zpconstant.cartdel_ckname, Idsdel.join (","));
} else {Setcookie (zpconstant.cartdel_ckname, spid);
}//End drophistory}//End Zpvisitor
PS: Here again for you to recommend a few JSON online tools, I believe that you can use in future development:
Online JSON code inspection, inspection, landscaping, formatting tools:
Http://tools.jb51.net/code/json
JSON Online formatting tool:
Http://tools.jb51.net/code/jsonformat
Online Xml/json Mutual Conversion tool:
Http://tools.jb51.net/code/xmljson
JSON code online Format/beautify/compress/edit/Convert tools:
Http://tools.jb51.net/code/jsoncodeformat
C Language Style/html/css/json code formatting landscaping Tools:
Http://tools.jb51.net/code/ccode_html_css_json
More readers interested in JavaScript-related content can view this site: "JavaScript in the JSON Operation tips Summary", "JavaScript switching effects and techniques summary", "JavaScript Search Algorithm Skills summary", " JavaScript animation effects and tips Summary, "JavaScript Error and debugging skills Summary", "JavaScript data structure and algorithm skills summary", "JavaScript traversal algorithm and Skills summary" and "JavaScript Mathematical operation Usage Summary"
I hope this article will help you with JavaScript programming.