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
    23 hours ago








  • 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
    23 hours ago












  • 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
    21 hours ago










  • What's the use case for this, kindly?
    – Sello Mkantjwa
    20 hours ago










  • all values passed to the next function must be a function so I have to convert any non-function value to a function that returns the original value @estus
    – xx yy
    20 hours ago















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
    23 hours ago








  • 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
    23 hours ago












  • 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
    21 hours ago










  • What's the use case for this, kindly?
    – Sello Mkantjwa
    20 hours ago










  • all values passed to the next function must be a function so I have to convert any non-function value to a function that returns the original value @estus
    – xx yy
    20 hours ago













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 21 hours ago









Sridhar

4,90732636




4,90732636










asked 23 hours ago









xx yy

205




205












  • 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
    23 hours ago








  • 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
    23 hours ago












  • 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
    21 hours ago










  • What's the use case for this, kindly?
    – Sello Mkantjwa
    20 hours ago










  • all values passed to the next function must be a function so I have to convert any non-function value to a function that returns the original value @estus
    – xx yy
    20 hours ago


















  • 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
    23 hours ago








  • 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
    23 hours ago












  • 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
    21 hours ago










  • What's the use case for this, kindly?
    – Sello Mkantjwa
    20 hours ago










  • all values passed to the next function must be a function so I have to convert any non-function value to a function that returns the original value @estus
    – xx yy
    20 hours ago
















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
23 hours ago






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
23 hours ago






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
23 hours ago






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
23 hours ago














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
21 hours ago




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
21 hours ago












What's the use case for this, kindly?
– Sello Mkantjwa
20 hours ago




What's the use case for this, kindly?
– Sello Mkantjwa
20 hours ago












all values passed to the next function must be a function so I have to convert any non-function value to a function that returns the original value @estus
– xx yy
20 hours ago




all values passed to the next function must be a function so I have to convert any non-function value to a function that returns the original value @estus
– xx yy
20 hours ago












1 Answer
1






active

oldest

votes

















up vote
1
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





















    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
































    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    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

























      up vote
      1
      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























        up vote
        1
        down vote










        up vote
        1
        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 20 hours ago









        Bergi

        355k55527845




        355k55527845






























             

            draft saved


            draft discarded



















































             


            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




















































































            Popular posts from this blog

            Florida Star v. B. J. F.

            Danny Elfman

            Lugert, Oklahoma