JSFiddle - React, Tailwind, and code Playground
by Adam Boduch
HTML
<script src="https://rawgit.com/lodash/lodash/master/lodash.js"></script>
JavaScript
var app = {
settings: {
version: 1
}
};
var version = _.partial(function(settings) {
return settings.version;
}, app.settings);
console.log('Testing plain object');
console.assert(version() === 1, 'version is 1');
// → true
app.settings.version = 2.0;
console.assert(version() === 2, 'version is 2');
// → true
app.settings = _.extend(app.settings, { version: 3 });
console.assert(version() === 3, 'version is 3');
// → true
app.settings = { version: 4 };
console.assert(version() === 4, 'version is 4');
// → "Assertion failed: version is 4"
var app = {};
Object.defineProperty(app, 'settings', {
set: function(value) {
this._settings = _.isPlainObject(this._settings) ?
_.extend(this._settings, value) : value;
},
get: function() {
return _.isPlainObject(this._settings) ?
this._settings : this._settings = {};
}
});
var version = _.partial(function(settings) {
return settings.version;
}, app.settings);
console.log('Testing object setter/getter');
app.settings = { version: 1 };
console.assert(version() === 1, 'version is 1');
// → true
app.settings = { version: 2 };
console.assert(version() === 2, 'version is 2');
// → true
var app = {};
Object.defineProperty(app, 'users', {
set: function(value) {
if (_.isArray(this._users)) {
this._users.length = 0;
this._users.push.apply(this._users, value);
} else {
this._users = value;
}
},
get: function() {
return _.isArray(this._users) ?
this._users : this._users = [];
}
});
var first = _.partial(_.first, app.users),
last = _.partial(_.last, app.users);
console.log('Testing array setter/getter');
app.users = [ 'user1', 'user2' ];
console.assert(first() === 'user1', 'first is "user1"');
// → true
console.assert(last() === 'user2', 'last is "user2"');
// → true
app.users = [ 'user3', 'user4' ];
console.assert(first() === 'user3', 'first is...