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)










share|improve this question























  • 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




    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














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)










share|improve this question























  • 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




    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












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)










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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




    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







  • 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












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.






share|improve this answer




















  • 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










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















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

























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.






share|improve this answer




















  • 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














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.






share|improve this answer




















  • 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












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.






share|improve this answer












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.







share|improve this answer












share|improve this answer



share|improve this answer










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
















  • 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

















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Kleinkühnau

Makov (Slowakei)

Deutsches Schauspielhaus