let appState={
title: {color: 'red',text: '标题'},
content:{color:'green',text:'内容'}
}
function renderTitle(title) {
let titleEle=document.querySelector('#title');
titleEle.innerHTML=title.text;
titleEle.style.color=title.color;
}
function renderContent(content) {
let contentEle=document.querySelector('#content');
contentEle.innerHTML=content.text;
contentEle.style.color=content.color;
}
function renderApp(appState) {
renderTitle(appState.title);
renderContent(appState.content);
}
renderApp(appState);
let appState={
title: {color: 'red',text: '标题'},
content:{color:'green',text:'内容'}
}
function renderTitle(title) {
let titleEle=document.querySelector('#title');
titleEle.innerHTML=title.text;
titleEle.style.color=title.color;
}
function renderContent(content) {
let contentEle=document.querySelector('#content');
contentEle.innerHTML=content.text;
contentEle.style.color=content.color;
}
function renderApp(appState) {
renderTitle(appState.title);
renderContent(appState.content);
}
function dispatch(action) {
switch (action.type) {
case 'UPDATE_TITLE_COLOR':
appState.title.color=action.color;
break;
case 'UPDATE_CONTENT_CONTENT':
appState.content.text=action.text;
break;
default:
break;
}
}
dispatch({type:'UPDATE_TITLE_COLOR',color:'purple'});
dispatch({type:'UPDATE_CONTENT_CONTENT',text:'新标题'});
renderApp(appState);
function renderTitle(title) {
let titleEle=document.querySelector('#title');
titleEle.innerHTML=title.text;
titleEle.style.color=title.color;
}
function renderContent(content) {
let contentEle=document.querySelector('#content');
contentEle.innerHTML=content.text;
contentEle.style.color=content.color;
}
function renderApp(appState) {
renderTitle(appState.title);
renderContent(appState.content);
}
function createStore(reducer) {
let state;
function getState() {
return state;
}
function dispatch(action) {
state=reducer(state,action);
}
dispatch({});
return {
getState,
dispatch
}
}
let initState={
title: {color: 'red',text: '标题'},
content:{color:'green',text:'内容'}
}
let reducer=function (state=initState,action) {
switch (action.type) {
case 'UPDATE_TITLE_COLOR':
return {...state,title: {...state.title,color:action.color}};
case 'UPDATE_CONTENT_CONTENT':
return {...state,content: {...state.content,text:action.text}};
break;
default:
return state;
}
}
let store=createStore(reducer);
renderApp(store.getState());
setTimeout(function () {
store.dispatch({type:'UPDATE_TITLE_COLOR',color:'purple'});
store.dispatch({type:'UPDATE_CONTENT_CONTENT',text:'新标题'});
renderApp(store.getState());
},2000);
function renderTitle(title) {
let titleEle=document.querySelector('#title');
titleEle.innerHTML=title.text;
titleEle.style.color=title.color;
}
function renderContent(content) {
let contentEle=document.querySelector('#content');
contentEle.innerHTML=content.text;
contentEle.style.color=content.color;
}
function render() {
renderTitle(store.getState().title);
renderContent(store.getState().content);
}
function createStore(reducer) {
let state;
let listeners=[];
function getState() {
return state;
}
function dispatch(action) {
state=reducer(state,action);
listeners.forEach(l=>l());
}
function subscribe(listener) {
listeners.push(listener);
return () => {
listeners = listeners.filter(item => item!=listener);
console.log(listeners);
}
}
dispatch({});
return {
getState,
dispatch,
subscribe
}
}
let initState={
title: {color: 'red',text: '标题'},
content:{color:'green',text:'内容'}
}
let reducer=function (state=initState,action) {
switch (action.type) {
case 'UPDATE_TITLE_COLOR':
return {...state,title: {...state.title,color:action.color}};
case 'UPDATE_CONTENT_CONTENT':
return {...state,content: {...state.content,text:action.text}};
break;
default:
return state;
}
}
let store=createStore(reducer);
render();
let unsubscribe = store.subscribe(render);
setTimeout(function () {
store.dispatch({type:'UPDATE_TITLE_COLOR',color:'purple'});
unsubscribe();
store.dispatch({type:'UPDATE_CONTENT_CONTENT',text:'新标题'});
},2000);
纯函数很严格,也就是说你几乎除了计算数据以外什么都不能干,计算的时候还不能依赖除了函数参数以外的数据。