Open AntonRzevskiy opened 6 years ago
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.
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.
It is forbidden to directly access the property! It is necessary to securely receive or set any properties in objects