JavaScript How to change parameter inside function?
Clash Royale CLAN TAG#URR8PPP
JavaScript How to change parameter inside function?
This is my code:
function makeCounter(x)
x = x
var counter = makeCounter();
console.log(counter.increment()); // output is 1
console.log(counter.value); // output is 0
Now, I want to know how to edit the code so that the value will be 1 after counter.increment().
6 Answers
6
Update value
using this.value
& assign value of x to it
value
this.value
function makeCounter(x) 0;
var obj =
value: x,
increment: function()
x = x + 1;
this.value = x
return x;
,
decrement: function()
x = x - 1;
this.value = x
return x;
return obj;
var counter = makeCounter();
console.log(counter.increment()); // output is 1
console.log(counter.value); // output is 1
You need to update value after the increase or decrease and use this.value instead of x variable.
function makeCounter(value = 0)
return
value,
increment ()
return ++this.value;
,
decrement ()
return --this.value;
;
let counter = makeCounter();
console.log(counter.increment()); // output is 1
console.log(counter.value); // output is 1
Good solution, but this needs explanation, too.
– connexo
Aug 12 at 5:24
Its nothing different then the other. He just shortened it. He is returning the object
– Deadweight
Aug 12 at 5:25
You need to update value
after the increase or decrease and use this.value
instead of x
variable.
value
this.value
x
You can see my code:
function makeCounter(x)
var counter = makeCounter();
console.log(counter.increment()); // output is 1
console.log(counter.value); // output is 1
console.log(counter.increment())//output is 2
console.log(counter.value); // output is 2
function makeCounter(x)
return 0,
increment ()
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators#Increment_()
return ++this.value;
,
decrement ()
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators#Decrement_(--)
return --this.value;
;
let counter = makeCounter();
console.log(counter.increment()); // output is 1
console.log(counter.value);
In case of nesting, you might fail to use this at some point, you can go with the object's name. as long as you are calling it inside a function
function makeCounter(x)
var counter = makeCounter();
console.log(counter.increment()); // output is 1
console.log(counter.value); // output is 1
You could use a getter on the object being returned. You get control to do any business logic you like when you do counter.value
.
counter.value
You could change your code like so
function makeCounter(x)
x = x
var counter = makeCounter();
console.log(counter.increment()); // output is 1
console.log(counter.value); // output is 1
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Awww poop i was too slow. dies lol
– Deadweight
Aug 12 at 5:22