espace Posted August 29, 2017 Share Posted August 29, 2017 hi, i really don't understand this ! why when i put an argument on the function in the CASE 1 and call this function with a the variable count_player it doesn't works ? in the CASE 2 it works with simply the variable as argument. Normally the CASE 1 must works, why is not the case ? //CASE 1 var count_player = 0 launch_player=(count) =>{ count++ } launch_player(count_player) //return 0 launch_player(count_player) // return always 0 //////////////////////////// //CASE 2 var count_player = 0 launch_player=(count_player) =>{ count_player++ } launch_player(count_player) //return 0 launch_player(count_player) // return 1 ok it works Quote Link to comment Share on other sites More sharing options...
BobF Posted August 29, 2017 Share Posted August 29, 2017 Your use of an implicit return in an arrow function is invalid in both case 1 and 2. An implicit return in launch_player should look more like this: launch_player = (count) => count + 1; You can read more about using implicit returns with arrow functions here. Quote Link to comment Share on other sites More sharing options...
mattstyles Posted August 30, 2017 Share Posted August 30, 2017 Neither of your cases returns anything as you've used curly braces to define a function body and not returned anything from that function body. I'm not sure why you're saying it's returning anything currently, because it does not. In any case JS will pass by value for primitive types so if you're trying to increment the global variable it won't work by passing it as an argument to a function without also declaring an assignment to that variable later. Quote Link to comment Share on other sites More sharing options...
espace Posted August 30, 2017 Author Share Posted August 30, 2017 hi, thanks how do you do so to have a pure function ? i don't really understand what you describe to soluce that... Quote Link to comment Share on other sites More sharing options...
espace Posted August 30, 2017 Author Share Posted August 30, 2017 in firefox console function add(a){a=a+1; return a} add(3) //return 4 it's the same however.... Quote Link to comment Share on other sites More sharing options...
espace Posted August 31, 2017 Author Share Posted August 31, 2017 finded var count_example=0 function countor(count) { return function () { return ++count; }; } var count_increment = countor(count_example); alert(count_increment()); // 1 alert(count_increment()); // 2 alert(count_increment()); // 3 Quote Link to comment Share on other sites More sharing options...
mattstyles Posted August 31, 2017 Share Posted August 31, 2017 So that is sometimes referred to as a thunk and its a great pattern using closures to encapsulate data. Effectively you're setting an initial state for your function to use and it then exposes that 'internal' state when invoked. `function add (i) {return ++i}` is a pure function. If you want it to increment and store a value from elsewhere then you'd have to do an assignment somewhere i.e. function add (num) { return ++num } var value = 0 value = add(value) value = add(value) console.log(value) // 2 // Without assignment value never changes add(value) add(value) console.log(value) // 2 If you want to 'trap' that initial value in there and then only access it when you want to increment it then your solution is perfect by using a closure. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.