Jump to content

argument and variable in function problem ?


espace
 Share

Recommended Posts

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 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...