var Settings = new Proxy({
data: {
value1: 'some text',
value2: true,
},
/**
* Callback - on any property/value read
*
* @param {string} name
* @returns {void}
*/
onGet: function(name) {
console.log('Property "' + name + '" read action');
},
/**
* Callback - on any property/value write
*
* @param {string} name
* @param {mixed} value
* @returns {void}
*/
onSet: function(name, value) {
console.log('Property "' + name + '" write "' + value + '" action');
},
/**
* Load settings from storage
*
* @param {callable} callback
* @returns {void|object}
*/
load: function(callback) {
// Code
},
/**
* Save settings in storage
*
* @param {callable} callback
* @returns {void}
*/
save: function(callback) {
// Code
},
/**
* Remove all settings from storage
*
* @param {callable} callback
* @returns {void}
*/
clear: function(callback) {
// Code
}
}, {
/**
* Getter
*
* @param {object} target
* @param {string} name
* @returns {mixed}
*/
get: function(target, name) {
var result = undefined;
if (name in target.data) {
result = target.data[name];
}
if (name in target) {
result = target[name];
}
if (typeof target.onGet === 'function') {
target.onGet.call(target, name);
}
return result;
},
/**
* Setter
*
* @param {object} target
* @param {string} name
* @param {mixed} value
* @returns {mixed}
*/
set: function(target, name, value) {
if (name in target.data) {
target.data[name] = value;
// Save changes in storage
target.save.call(target, name, value);
}
if (name in target) {
target[name] = value;
}
if (typeof target.onSet === 'function') {
target.onSet.call(target, name, value);
}
return true;
}
});
var source = {
'one': {
id: 1,
name: 'Name1',
},
'two': {
id: 2,
name: 'Name2',
}};
console.log(source['one']['name']);
console.log(source['two']['name']);
Name1
Name2
var source = [{
id:123,
name: 'Name1'
},{
id: 321,
name: 'Name2'
}];
function getObject(source, id) {
return jQuery.grep(source, function(e){ return e.id == id; });
}
console.log(getObject(source, 321));
function getObject(source, id) {
for (var i = 0, len = source.length; i < len; i++) {
if (source[i].id == id) {
return source[i];
}
}
}
function getObject(source, id) {
return source.find(function (el) {
return el.id === id;
});
}
function getObject(source, id) {
return source.filter(function(el) {
return el.id === id;
})[0];
}
console.time('test_func_exec');
for (var i = 0; i < 10000000; i++) {
getObject(source, 123);
}
console.timeEnd('test_func_exec');