Skip to content

Instantly share code, notes, and snippets.

@AliveDD
Created October 8, 2019 13:30
Show Gist options
  • Save AliveDD/f95a4ee0dde8667c2fdaac807ea54c4c to your computer and use it in GitHub Desktop.
Save AliveDD/f95a4ee0dde8667c2fdaac807ea54c4c to your computer and use it in GitHub Desktop.
closure-example
import React from "react";
import ReactDOM from "react-dom";
class App extends React.Component {
componentDidMount() {
const Closure = (() => {
let _value = 0;
return {
print: () => _value,
plus: () => {
_value += 1;
},
minus: () => {
_value -= 1;
}
};
})();
console.log(Closure.print()); // 0
Closure.plus();
console.log(Closure.print()); // 1
}
render() {
return <div className="App" />;
}
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment