Result depends on it
function Adder(first, second) {
this.first = first;
this.second = second;
this.sum = function () {
return this.first + this.second;
}
}
var adder = new Adder(2, 3);
adder.sum();
Introduces an additional parameter: TIME
function Adder(first, second) {
...
this.setFirst = function(first) {
this.first = first;
}
this.setSecond = function(second) {
this.second = second;
}
}
var adder = new Adder(2, 3);
... //black hole :P
adder.sum(); // =?function badGuy(param) {
param.first = 10;
param.second = -1;
}
var adder = new Adder(2, 3);
badGuy(adder) //i don't know what bad guy does
adder.sum(); // =5, right? :Pdon't mutate the parameters inside a function (seriously, don't!)
function Adder() {
this.sum = function(first, second) {
return first + second;
}
}
var adder = new Adder();
adder.sum(2, 3); // =5, always!no state
depend only on parameters
play very nice with immutable objects
------------- ---------------
| | items | |
| Loader | -------> | Presenter |
| | | |
------------- ---------------can select items in the presenter
function markSelected(item) {
item.selected = true
}------------- ---------------
| | items | |
| Loader | -------> | Presenter |
| | | |
------------- ---------------
\ ---------------
\ items | |
-------> |2nd Presenter|
| |
---------------can select items in the second presenter
item.selected = true ???------------- ---------------
| | items | |
| Loader | -------> | Presenter |
| | | |
------------- ---------------
\ ---------------
\ items | |
-------> |2nd Presenter|
| |
---------------reload data from the server
fetchData(...)
.then(loadedData => this.myData = loadedData)that easy?
the presenters have just lost the selected state
start doing all sort of complex logic
fetchData(...)
.then(loadedData => {
this.myData.length = 0;
for (let item of loadedData) {
this.myData.push(item);
}
})the presenters are f**ked again...
fetchData(...)
.then(loadedData => {
this.oldData = copy(this.myData);
this.myData.length = 0;
for (let item of loadedData) {
if (isSelectedByFirstPresenter(item, this.oldData)) {
item.selected = true;
}
this.myData.push(item);
}
})
function isSelectedByFirstPresenter(item, oldData) {
//indexOf won't work because it's a new object
return this.oldData.any((oldItem) => equals(oldItem, item) && oldItem.selected);
}what is selected and why do i know about it?
no real owner of the data
have to take care/account for others
complex equality checks for complex objects
X
let state = {
who: 'Ana',
has: 'apples',
count: 5
}
state.count = 6;
expect(state.count).to.equal(6);let state = {
who: 'Ana',
has: 'apples',
count: 5
}
let nextState = state.set('count', 6)
expect(nextState.count).to.equal(6);
expect(state.count).to.equal(5);really? :P
let brown = Range(0, 9);
let blue = brown.set(5, 'beef');