Created
October 8, 2019 13:30
-
-
Save AliveDD/f95a4ee0dde8667c2fdaac807ea54c4c to your computer and use it in GitHub Desktop.
closure-example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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