javascript: how to convert a variable to a function with the same name that returns the original value
up vote
1
down vote
favorite
let's say I have a numeric variable (or any other type),
I want to convert this variable to a function which has the same variable name and returns the original value (without using additional variables)
var x=1
x=()=>x
this will copy x by reference, not by value, so the new x will always returns itself (i.e a function) not the original value
I reached the result using an additional variable (temp)
var x=1
var temp=x
var x=()=>temp
what I want is to copy this variable by value, not by reference
I know how to copy an object to another object, but it is not the same case
note: using the same name is important for the flow of the next code (i.e: I have to use a function, if another type provided, I have to convert it to a function of the same name)
javascript node.js reference clone
|
show 2 more comments
up vote
1
down vote
favorite
let's say I have a numeric variable (or any other type),
I want to convert this variable to a function which has the same variable name and returns the original value (without using additional variables)
var x=1
x=()=>x
this will copy x by reference, not by value, so the new x will always returns itself (i.e a function) not the original value
I reached the result using an additional variable (temp)
var x=1
var temp=x
var x=()=>temp
what I want is to copy this variable by value, not by reference
I know how to copy an object to another object, but it is not the same case
note: using the same name is important for the flow of the next code (i.e: I have to use a function, if another type provided, I have to convert it to a function of the same name)
javascript node.js reference clone
Sounds very much like an X/Y problem, but doesargumentsorthiscount as a variable in your situation? :P (I'm assuming that defining a parameter would count as a variable) Or, can you explain what the problem is with using another variable?
– CertainPerformance
Nov 10 at 6:55
4
JavaScript only has call/pass by value, never call/pass by reference. Either way, I don't see how these concepts are related to what you want to do. You cannot have the same variable refer to two different values at the same. You have to introduce another variable one way or the other. If you want to have it more "self-contained", use an IIFE:x = (x => () => x)(x);(I guess technically this is an "IIAF" (immediately invoked arrow function)).
– Felix Kling
Nov 10 at 7:11
You're trying to do some thing that isn't natural for JS. It's unclear what the purpose is but it's very likely that this is XY problem, as it was already mentioned.
– estus
Nov 10 at 9:11
What's the use case for this, kindly?
– Sello Mkantjwa
Nov 10 at 9:25
1
Consider providing the code that shows your problem so possible answers could address the problem in more constructive way. From what you said it's not clear why a function should have the same name as a variable. It's also not clear why it's a problem that variable reference is passed.
– estus
Nov 10 at 9:56
|
show 2 more comments
up vote
1
down vote
favorite
up vote
1
down vote
favorite
let's say I have a numeric variable (or any other type),
I want to convert this variable to a function which has the same variable name and returns the original value (without using additional variables)
var x=1
x=()=>x
this will copy x by reference, not by value, so the new x will always returns itself (i.e a function) not the original value
I reached the result using an additional variable (temp)
var x=1
var temp=x
var x=()=>temp
what I want is to copy this variable by value, not by reference
I know how to copy an object to another object, but it is not the same case
note: using the same name is important for the flow of the next code (i.e: I have to use a function, if another type provided, I have to convert it to a function of the same name)
javascript node.js reference clone
let's say I have a numeric variable (or any other type),
I want to convert this variable to a function which has the same variable name and returns the original value (without using additional variables)
var x=1
x=()=>x
this will copy x by reference, not by value, so the new x will always returns itself (i.e a function) not the original value
I reached the result using an additional variable (temp)
var x=1
var temp=x
var x=()=>temp
what I want is to copy this variable by value, not by reference
I know how to copy an object to another object, but it is not the same case
note: using the same name is important for the flow of the next code (i.e: I have to use a function, if another type provided, I have to convert it to a function of the same name)
javascript node.js reference clone
javascript node.js reference clone
edited Nov 10 at 9:06
Sridhar
5,05742736
5,05742736
asked Nov 10 at 6:52
xx yy
286
286
Sounds very much like an X/Y problem, but doesargumentsorthiscount as a variable in your situation? :P (I'm assuming that defining a parameter would count as a variable) Or, can you explain what the problem is with using another variable?
– CertainPerformance
Nov 10 at 6:55
4
JavaScript only has call/pass by value, never call/pass by reference. Either way, I don't see how these concepts are related to what you want to do. You cannot have the same variable refer to two different values at the same. You have to introduce another variable one way or the other. If you want to have it more "self-contained", use an IIFE:x = (x => () => x)(x);(I guess technically this is an "IIAF" (immediately invoked arrow function)).
– Felix Kling
Nov 10 at 7:11
You're trying to do some thing that isn't natural for JS. It's unclear what the purpose is but it's very likely that this is XY problem, as it was already mentioned.
– estus
Nov 10 at 9:11
What's the use case for this, kindly?
– Sello Mkantjwa
Nov 10 at 9:25
1
Consider providing the code that shows your problem so possible answers could address the problem in more constructive way. From what you said it's not clear why a function should have the same name as a variable. It's also not clear why it's a problem that variable reference is passed.
– estus
Nov 10 at 9:56
|
show 2 more comments
Sounds very much like an X/Y problem, but doesargumentsorthiscount as a variable in your situation? :P (I'm assuming that defining a parameter would count as a variable) Or, can you explain what the problem is with using another variable?
– CertainPerformance
Nov 10 at 6:55
4
JavaScript only has call/pass by value, never call/pass by reference. Either way, I don't see how these concepts are related to what you want to do. You cannot have the same variable refer to two different values at the same. You have to introduce another variable one way or the other. If you want to have it more "self-contained", use an IIFE:x = (x => () => x)(x);(I guess technically this is an "IIAF" (immediately invoked arrow function)).
– Felix Kling
Nov 10 at 7:11
You're trying to do some thing that isn't natural for JS. It's unclear what the purpose is but it's very likely that this is XY problem, as it was already mentioned.
– estus
Nov 10 at 9:11
What's the use case for this, kindly?
– Sello Mkantjwa
Nov 10 at 9:25
1
Consider providing the code that shows your problem so possible answers could address the problem in more constructive way. From what you said it's not clear why a function should have the same name as a variable. It's also not clear why it's a problem that variable reference is passed.
– estus
Nov 10 at 9:56
Sounds very much like an X/Y problem, but does
arguments or this count as a variable in your situation? :P (I'm assuming that defining a parameter would count as a variable) Or, can you explain what the problem is with using another variable?– CertainPerformance
Nov 10 at 6:55
Sounds very much like an X/Y problem, but does
arguments or this count as a variable in your situation? :P (I'm assuming that defining a parameter would count as a variable) Or, can you explain what the problem is with using another variable?– CertainPerformance
Nov 10 at 6:55
4
4
JavaScript only has call/pass by value, never call/pass by reference. Either way, I don't see how these concepts are related to what you want to do. You cannot have the same variable refer to two different values at the same. You have to introduce another variable one way or the other. If you want to have it more "self-contained", use an IIFE:
x = (x => () => x)(x); (I guess technically this is an "IIAF" (immediately invoked arrow function)).– Felix Kling
Nov 10 at 7:11
JavaScript only has call/pass by value, never call/pass by reference. Either way, I don't see how these concepts are related to what you want to do. You cannot have the same variable refer to two different values at the same. You have to introduce another variable one way or the other. If you want to have it more "self-contained", use an IIFE:
x = (x => () => x)(x); (I guess technically this is an "IIAF" (immediately invoked arrow function)).– Felix Kling
Nov 10 at 7:11
You're trying to do some thing that isn't natural for JS. It's unclear what the purpose is but it's very likely that this is XY problem, as it was already mentioned.
– estus
Nov 10 at 9:11
You're trying to do some thing that isn't natural for JS. It's unclear what the purpose is but it's very likely that this is XY problem, as it was already mentioned.
– estus
Nov 10 at 9:11
What's the use case for this, kindly?
– Sello Mkantjwa
Nov 10 at 9:25
What's the use case for this, kindly?
– Sello Mkantjwa
Nov 10 at 9:25
1
1
Consider providing the code that shows your problem so possible answers could address the problem in more constructive way. From what you said it's not clear why a function should have the same name as a variable. It's also not clear why it's a problem that variable reference is passed.
– estus
Nov 10 at 9:56
Consider providing the code that shows your problem so possible answers could address the problem in more constructive way. From what you said it's not clear why a function should have the same name as a variable. It's also not clear why it's a problem that variable reference is passed.
– estus
Nov 10 at 9:56
|
show 2 more comments
1 Answer
1
active
oldest
votes
up vote
2
down vote
Just write a function:
function constFunction(temp)
return () => temp;
so that you can call it as x = constFunction(x). Like every function call or assignment, this copies the value of the variable, not a reference. And no, you cannot really avoid a second variable (temp in this case), as you can't have x refer to both the function and the original value.
I don't use an extra variable, so you want me to use an extra function 😂, anyway this is a good idea but out of my question's scope
– xx yy
Nov 12 at 7:20
@xxyy Well, as pointed out in the comments you cannot do with a single variable only.
– Bergi
Nov 12 at 7:27
I finally used a temporary variable
– xx yy
Nov 12 at 7:32
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
Just write a function:
function constFunction(temp)
return () => temp;
so that you can call it as x = constFunction(x). Like every function call or assignment, this copies the value of the variable, not a reference. And no, you cannot really avoid a second variable (temp in this case), as you can't have x refer to both the function and the original value.
I don't use an extra variable, so you want me to use an extra function 😂, anyway this is a good idea but out of my question's scope
– xx yy
Nov 12 at 7:20
@xxyy Well, as pointed out in the comments you cannot do with a single variable only.
– Bergi
Nov 12 at 7:27
I finally used a temporary variable
– xx yy
Nov 12 at 7:32
add a comment |
up vote
2
down vote
Just write a function:
function constFunction(temp)
return () => temp;
so that you can call it as x = constFunction(x). Like every function call or assignment, this copies the value of the variable, not a reference. And no, you cannot really avoid a second variable (temp in this case), as you can't have x refer to both the function and the original value.
I don't use an extra variable, so you want me to use an extra function 😂, anyway this is a good idea but out of my question's scope
– xx yy
Nov 12 at 7:20
@xxyy Well, as pointed out in the comments you cannot do with a single variable only.
– Bergi
Nov 12 at 7:27
I finally used a temporary variable
– xx yy
Nov 12 at 7:32
add a comment |
up vote
2
down vote
up vote
2
down vote
Just write a function:
function constFunction(temp)
return () => temp;
so that you can call it as x = constFunction(x). Like every function call or assignment, this copies the value of the variable, not a reference. And no, you cannot really avoid a second variable (temp in this case), as you can't have x refer to both the function and the original value.
Just write a function:
function constFunction(temp)
return () => temp;
so that you can call it as x = constFunction(x). Like every function call or assignment, this copies the value of the variable, not a reference. And no, you cannot really avoid a second variable (temp in this case), as you can't have x refer to both the function and the original value.
answered Nov 10 at 10:05
Bergi
359k55533850
359k55533850
I don't use an extra variable, so you want me to use an extra function 😂, anyway this is a good idea but out of my question's scope
– xx yy
Nov 12 at 7:20
@xxyy Well, as pointed out in the comments you cannot do with a single variable only.
– Bergi
Nov 12 at 7:27
I finally used a temporary variable
– xx yy
Nov 12 at 7:32
add a comment |
I don't use an extra variable, so you want me to use an extra function 😂, anyway this is a good idea but out of my question's scope
– xx yy
Nov 12 at 7:20
@xxyy Well, as pointed out in the comments you cannot do with a single variable only.
– Bergi
Nov 12 at 7:27
I finally used a temporary variable
– xx yy
Nov 12 at 7:32
I don't use an extra variable, so you want me to use an extra function 😂, anyway this is a good idea but out of my question's scope
– xx yy
Nov 12 at 7:20
I don't use an extra variable, so you want me to use an extra function 😂, anyway this is a good idea but out of my question's scope
– xx yy
Nov 12 at 7:20
@xxyy Well, as pointed out in the comments you cannot do with a single variable only.
– Bergi
Nov 12 at 7:27
@xxyy Well, as pointed out in the comments you cannot do with a single variable only.
– Bergi
Nov 12 at 7:27
I finally used a temporary variable
– xx yy
Nov 12 at 7:32
I finally used a temporary variable
– xx yy
Nov 12 at 7:32
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53236690%2fjavascript-how-to-convert-a-variable-to-a-function-with-the-same-name-that-retu%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Sounds very much like an X/Y problem, but does
argumentsorthiscount as a variable in your situation? :P (I'm assuming that defining a parameter would count as a variable) Or, can you explain what the problem is with using another variable?– CertainPerformance
Nov 10 at 6:55
4
JavaScript only has call/pass by value, never call/pass by reference. Either way, I don't see how these concepts are related to what you want to do. You cannot have the same variable refer to two different values at the same. You have to introduce another variable one way or the other. If you want to have it more "self-contained", use an IIFE:
x = (x => () => x)(x);(I guess technically this is an "IIAF" (immediately invoked arrow function)).– Felix Kling
Nov 10 at 7:11
You're trying to do some thing that isn't natural for JS. It's unclear what the purpose is but it's very likely that this is XY problem, as it was already mentioned.
– estus
Nov 10 at 9:11
What's the use case for this, kindly?
– Sello Mkantjwa
Nov 10 at 9:25
1
Consider providing the code that shows your problem so possible answers could address the problem in more constructive way. From what you said it's not clear why a function should have the same name as a variable. It's also not clear why it's a problem that variable reference is passed.
– estus
Nov 10 at 9:56