JS JSON Add delete modify class code

Source: Internet
Author: User
The code is as follows Copy Code

(function () {
/**
* Schema function
* @param {string} name identification name, such as user, info, etc.
*/
function Class (name) {
var self = this;
Self._name = name;
}

/**
* Get Data
* @param {string|undefined} key to get the name of the data, or null if there is no key
* @return {Object} End Object
*/
Class.prototype.get = function (key) {

var json = This._get_data (),
I
Arr
Len

if (key) {
arr = Key.split (".");
len = arr.length;

for (i = 0; i<len; i++) {//traverse out the User.name.value space, and then let the properties of this space xl= the data
JSON = Json[arr[i]] = json[arr[i]] | | {};
}
}

return JSON;
}


/**
* Write Data
* @param the key name to write to {string} key, supported by. Delimited, if none is created as an object
* @param {Data}
* @return {Object} current instance
*/
Class.prototype.set = function (key, data) {
var self = this,
JSON = Self._get_data () | | {},//Gets the latest to ensure each update

I
arr = Key.split ("."),
Len = Arr.length,
Temp

if (len = = 1) {//Set ("User", value)
Json[key] = data;
else if (len = = 2) {//set ("User.Name", value)
Json[arr[0]] = json[arr[0]] | | {};//Register First space
JSON[ARR[0]][ARR[1]] = data;
else {//Depth space set ("USER.NAME.VALUE.XL", value)
temp = json;//temporary to a bar, with the original traversal will hang
for (i = 0; i<len-1; i++) {//traverse out the User.name.value space, and then let the properties of this space xl= the data
temp = Temp[arr[i]] = temp[arr[i]] | | {};
}
Temp[arr[i]] = data;
}


Self._set_data (Stringify (JSON));//write to Local

return self;
}


/**
* Remove Data
* @param {string} Key Name
* @return {Object} current instance
*/
Class.prototype.remove = function (key) {
Return This.set (key, undefined);
}


Class.prototype._set_data = function (value) {
Localstorage.setitem (this._name, value);
}

Class.prototype._get_data = function () {
Return Parse (Localstorage.getitem (this._name));
}

Class.prototype._remove_data = function () {
Localstorage.removeitem (This._name);
}


/**
* Parse String->json
* @param {string} str must be parsed by ' {' XL ': 1} '
* @return {Object} resolved objects
*/
function Parse (str) {
return Json.parse (str);
}


/**
* Parse json=>string
* @param {Object} JSON objects to resolve
* @return The parsed string of {string}
*/
function Stringify (JSON) {
Return json.stringify (JSON);
}

    var demo1 = new Class ("Demo1");
    demo1.set ("Msc.user.data.userId", 1). Set ("Msc.xl", "Xieliang");
    Console.log (Demo1.get ());
    Console.log (Demo1.get ("Msc.user"));
    Console.log (Demo1.get ("xxx.xxx"));//Empty {}
    Console.log, because there is no xxx space ( Demo1.get ());
    Console.log (Demo1.remove ("Msc.user"). Get ("MSC");
    Console.log (Demo1.remove ("xl.xl.x"). Get ())//In fact, this feeling is not very handsome, because there is no XL.XL but chose set created ...
} ());

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.