Created
October 18, 2018 07:53
-
-
Save shsunmoonlee/e92157449baa5a0cffedc0f7b9ea4405 to your computer and use it in GitHub Desktop.
call apply bind and their usages in react guide
This file contains hidden or 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
| class Toggle extends React.Component { | |
| constructor(props) { | |
| super(props); | |
| this.state = {isToggleOn: true}; | |
| // This binding is necessary to make `this` work in the callback | |
| this.handleClick = this.handleClick.bind(this); | |
| } | |
| handleClick() { | |
| this.setState(state => ({ | |
| isToggleOn: !state.isToggleOn | |
| })); | |
| } | |
| render() { | |
| return ( | |
| <button onClick={this.handleClick}> | |
| {this.state.isToggleOn ? 'ON' : 'OFF'} | |
| </button> | |
| ); | |
| } | |
| } | |
| ReactDOM.render( | |
| <Toggle />, | |
| document.getElementById('root') | |
| ); | |
| 1. Call | |
| function Product(name, price) { | |
| this.name = name; | |
| this.price = price; | |
| } | |
| function Food(name, price) { | |
| Product.call(this, name, price); | |
| this.category = 'food'; | |
| } | |
| function Toy(name, price) { | |
| Product.call(this, name, price); | |
| this.category = 'toy'; | |
| } | |
| var cheese = new Food('feta', 5); | |
| var fun = new Toy('robot', 40); | |
| 2. Apply | |
| var numbers = [5, 6, 2, 3, 7]; | |
| var max = Math.max.apply(null, numbers); | |
| console.log(max); | |
| // expected output: 7 | |
| var min = Math.min.apply(null, numbers); | |
| console.log(min); | |
| // expected output: 2 | |
| function MyConstructor() { | |
| for (var nProp = 0; nProp < arguments.length; nProp++) { | |
| this['property' + nProp] = arguments[nProp]; | |
| } | |
| } | |
| var myArray = [4, 'Hello world!', false]; | |
| var myInstance = MyConstructor.construct(myArray); | |
| console.log(myInstance.property1); // logs 'Hello world!' | |
| console.log(myInstance instanceof MyConstructor); // logs 'true' | |
| console.log(myInstance.constructor); // logs 'MyConstructor' | |
| 3. Bind | |
| this.x = 9; // this refers to global "window" object here in the browser | |
| var module = { | |
| x: 81, | |
| getX: function() { return this.x; } | |
| }; | |
| module.getX(); // 81 | |
| var retrieveX = module.getX; | |
| retrieveX(); | |
| // returns 9 - The function gets invoked at the global scope | |
| // Create a new function with 'this' bound to module | |
| // New programmers might confuse the | |
| // global var x with module's property x | |
| var boundGetX = retrieveX.bind(module); | |
| boundGetX(); // 81 | |
| function list() { | |
| return Array.prototype.slice.call(arguments); | |
| } | |
| var list1 = list(1, 2, 3); // [1, 2, 3] | |
| // Create a function with a preset leading argument | |
| var leadingThirtysevenList = list.bind(null, 37); | |
| var list2 = leadingThirtysevenList(); | |
| // [37] | |
| var list3 = leadingThirtysevenList(1, 2, 3); | |
| // [37, 1, 2, 3] | |
| function LateBloomer() { | |
| this.petalCount = Math.floor(Math.random() * 12) + 1; | |
| } | |
| // Declare bloom after a delay of 1 second | |
| LateBloomer.prototype.bloom = function() { | |
| window.setTimeout(this.declare.bind(this), 1000); | |
| }; | |
| LateBloomer.prototype.declare = function() { | |
| console.log('I am a beautiful flower with ' + | |
| this.petalCount + ' petals!'); | |
| }; | |
| var flower = new LateBloomer(); | |
| flower.bloom(); | |
| // after 1 second, triggers the 'declare' method | |
| References: | |
| https://reactjs.org/docs/handling-events.html | |
| https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind | |
| https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call | |
| https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply | |
| 1. bind | |
| var pokemon = { | |
| firstname: 'Pika', | |
| lastname: 'Chu ', | |
| getPokeName: function() { | |
| var fullname = this.firstname + ' ' + this.lastname; | |
| return fullname; | |
| } | |
| }; | |
| var pokemonName = function() { | |
| console.log(this.getPokeName() + 'I choose you!'); | |
| }; | |
| var logPokemon = pokemonName.bind(pokemon); // creates new object and binds pokemon. 'this' of pokemon === pokemon now | |
| logPokemon(); // 'Pika Chu I choose you!' | |
| var pokemon = { | |
| firstname: 'Pika', | |
| lastname: 'Chu ', | |
| getPokeName: function() { | |
| var fullname = this.firstname + ' ' + this.lastname; | |
| return fullname; | |
| } | |
| }; | |
| var pokemonName = function(snack, hobby) { | |
| console.log(this.getPokeName() + 'I choose you!'); | |
| console.log(this.getPokeName() + ' loves ' + snack + ' and ' + hobby); | |
| }; | |
| var logPokemon = pokemonName.bind(pokemon); // creates new object and binds pokemon. 'this' of pokemon === pokemon now | |
| logPokemon('sushi', 'algorithms'); // Pika Chu loves sushi and algorithms | |
| 2. Call, Apply | |
| ar pokemon = { | |
| firstname: 'Pika', | |
| lastname: 'Chu ', | |
| getPokeName: function() { | |
| var fullname = this.firstname + ' ' + this.lastname; | |
| return fullname; | |
| } | |
| }; | |
| var pokemonName = function(snack, hobby) { | |
| console.log(this.getPokeName() + ' loves ' + snack + ' and ' + hobby); | |
| }; | |
| pokemonName.call(pokemon,'sushi', 'algorithms'); // Pika Chu loves sushi and algorithms | |
| pokemonName.apply(pokemon,['sushi', 'algorithms']); // Pika Chu loves sushi and algorithms | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment