AntonRzevskiy / TableEdit

Plugin for creating an editable table from an array, textarea, table and not only. You can easily add and delete rows, cells. The plugin contains enough options and callback functions for quick customization for your task.
GNU General Public License v3.0
4 stars 2 forks source link

Getters & Setters #18

Open AntonRzevskiy opened 6 years ago

AntonRzevskiy commented 6 years ago

It is forbidden to directly access the property! It is necessary to securely receive or set any properties in objects

AntonRzevskiy commented 6 years ago

Example of Get & Set

var example = {

    'getProp': function( obj, prop ) {

        var chain = prop.split('.');

        var cur = obj;

        for( var p = 0; p < chain.length; p++ ) {

            if( cur.hasOwnProperty( chain[p] ) ) {

                cur = cur[ chain[p] ];

                continue;
            }

            return undefined;
        }

        return cur;
    },

    'setProp': function( obj, prop, val ) {

        var chain = prop.split('.');

        var tmp = [];
        var save = {};
        var a, b;

        for( var p = chain.length - 1, lastKey = chain.length - 1; p >= 0; p-- ) {

            a = {};

            if( p === lastKey ) {

                a[ chain[p] ] = val;

                tmp.push( a );

                continue;
            }

            else {

                b = tmp.pop();

                a[ chain[p] ] = b;

                tmp.push( a );
            }

        }

        save = tmp.pop();

        $.extend(true, obj, save);

        if( chain.length > 1 ) {

            chain.pop();

            return this.getProp( obj, chain.join('.') );
        }

        return obj;
    },

};

Setter return close parent object for cache result.

This is an announcement of the features that can be included in the release of the plugin.

AntonRzevskiy commented 6 years ago

From the example above it is seen that the methods will work longer than if their code were written arbitrarily.

From the point of view of optimization, it is necessary to find the benefit of using it.