calling a function on a function is it possible?












9














in the following code



const express = require('express');
const app = express()
app.use(express.static('public'));


express is a function so how it call "express.static('public')" method on it? is it possible in JavaScript to call a function which is inside a function










share|improve this question



























    9














    in the following code



    const express = require('express');
    const app = express()
    app.use(express.static('public'));


    express is a function so how it call "express.static('public')" method on it? is it possible in JavaScript to call a function which is inside a function










    share|improve this question

























      9












      9








      9


      1





      in the following code



      const express = require('express');
      const app = express()
      app.use(express.static('public'));


      express is a function so how it call "express.static('public')" method on it? is it possible in JavaScript to call a function which is inside a function










      share|improve this question













      in the following code



      const express = require('express');
      const app = express()
      app.use(express.static('public'));


      express is a function so how it call "express.static('public')" method on it? is it possible in JavaScript to call a function which is inside a function







      javascript node.js express






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Dec 22 at 9:37









      Ehsan sarshar

      573




      573
























          3 Answers
          3






          active

          oldest

          votes


















          19














          A functions is not only a first class function, but also a first class object and can contain properties.






          function foo() {}

          foo.bar = function () { console.log('i am bar'); };

          foo.bar();








          share|improve this answer

















          • 5




            Yes. From a static, OO point of view, this could be explained as “function application is just a special method you can call on a function object, with syntactic sugar that makes it look like maths function application”, i.e. express() would be syntactic sugar for express.<operator"()">(). From a dynamic, FP view, it would be explained as “OO methods are just special functions that take the name of the method as the first argument”, i.e. express.static('public') would be syntactic sugar for express(<methodname "static">, 'public').
            – leftaroundabout
            Dec 22 at 13:40








          • 4




            @leftaroundabout: Neither of those matches the formalism of how it's actually defined in Javascript, though.
            – Henning Makholm
            Dec 22 at 14:42










          • @HenningMakholm The first one actually does. The "<operator"()">" method is named [[call]] in the spec, although it is an internal method not a normal object property.
            – Bergi
            Dec 22 at 19:38












          • @Bergi: That's what I mean: [[call]] is special and not just sugar. And it's equally present in named method calls: foo.bar() would then be sugar for foo["bar"].<operator()>() plus some additional semantics to set this appropriately.
            – Henning Makholm
            Dec 22 at 21:54





















          2














          You can attach a function as member data to another function (which is what is done in your example).



          const express = () => {};
          express.static = argument => console.log('argument');
          express.static('public'); # console >>> 'public'


          However, you cannot readily access a variable that is defined in a function body.



          const express = () => {
          const static = argument => console.log('argument');
          };
          express.static('public'); # console >>> Error: undefined in not a function


          There is a signifiant difference between member data attached to a function (the first example) and the closure that wraps the body of the function (the second example).



          So, to answer your question "is it possible in JavaScript to call a function which is inside a function?" No, this is not readily possible with the important note that this is not what is being done in your example.






          share|improve this answer





























            0














            Actually, it is possible to call a function inside another function in javaScript.
            So it depends on the condition, Callback is widely used in js




            (A callback is a function that is to be executed after another
            function has finished executing
            )




            function doHomework(subject, callback) {
            alert(`Starting my ${subject} homework.`);
            callback();
            }
            function alertFinished(){
            alert('Finished my homework');
            }
            doHomework('math', alertFinished);


            And the second example can be Closures




            (A closure is a feature in JavaScript where an inner function has
            access to the outer (enclosing) function's variables)




            function outer() {
            var b = 10;
            function inner() {

            var a = 20;
            console.log(a+b);
            }
            return inner;
            }





            share|improve this answer

















            • 3




              While the facts given here are technically correct (and are important ones to know in JS), I don't think this answer the specific question.
              – Pac0
              Dec 22 at 14:06












            • @Pac0 I believe this answer fully satisfies (is it possible in JavaScript to call a function which is inside a function) this part of the question. So there is no reason to vote as a not useful answer!
              – Nuriddin Kudratov
              Dec 22 at 21:27












            • @NurridanKudratov I didn't downvote or vote anything, but I strongly believe that when OP is talking about being "inside a function", he was tallkking about functions that could be used as an object while having a member function at the same time (express can be executed () and also has static as member). This case is not among your examples.
              – Pac0
              2 days ago













            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',
            autoActivateHeartbeat: false,
            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%2f53894551%2fcalling-a-function-on-a-function-is-it-possible%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            19














            A functions is not only a first class function, but also a first class object and can contain properties.






            function foo() {}

            foo.bar = function () { console.log('i am bar'); };

            foo.bar();








            share|improve this answer

















            • 5




              Yes. From a static, OO point of view, this could be explained as “function application is just a special method you can call on a function object, with syntactic sugar that makes it look like maths function application”, i.e. express() would be syntactic sugar for express.<operator"()">(). From a dynamic, FP view, it would be explained as “OO methods are just special functions that take the name of the method as the first argument”, i.e. express.static('public') would be syntactic sugar for express(<methodname "static">, 'public').
              – leftaroundabout
              Dec 22 at 13:40








            • 4




              @leftaroundabout: Neither of those matches the formalism of how it's actually defined in Javascript, though.
              – Henning Makholm
              Dec 22 at 14:42










            • @HenningMakholm The first one actually does. The "<operator"()">" method is named [[call]] in the spec, although it is an internal method not a normal object property.
              – Bergi
              Dec 22 at 19:38












            • @Bergi: That's what I mean: [[call]] is special and not just sugar. And it's equally present in named method calls: foo.bar() would then be sugar for foo["bar"].<operator()>() plus some additional semantics to set this appropriately.
              – Henning Makholm
              Dec 22 at 21:54


















            19














            A functions is not only a first class function, but also a first class object and can contain properties.






            function foo() {}

            foo.bar = function () { console.log('i am bar'); };

            foo.bar();








            share|improve this answer

















            • 5




              Yes. From a static, OO point of view, this could be explained as “function application is just a special method you can call on a function object, with syntactic sugar that makes it look like maths function application”, i.e. express() would be syntactic sugar for express.<operator"()">(). From a dynamic, FP view, it would be explained as “OO methods are just special functions that take the name of the method as the first argument”, i.e. express.static('public') would be syntactic sugar for express(<methodname "static">, 'public').
              – leftaroundabout
              Dec 22 at 13:40








            • 4




              @leftaroundabout: Neither of those matches the formalism of how it's actually defined in Javascript, though.
              – Henning Makholm
              Dec 22 at 14:42










            • @HenningMakholm The first one actually does. The "<operator"()">" method is named [[call]] in the spec, although it is an internal method not a normal object property.
              – Bergi
              Dec 22 at 19:38












            • @Bergi: That's what I mean: [[call]] is special and not just sugar. And it's equally present in named method calls: foo.bar() would then be sugar for foo["bar"].<operator()>() plus some additional semantics to set this appropriately.
              – Henning Makholm
              Dec 22 at 21:54
















            19












            19








            19






            A functions is not only a first class function, but also a first class object and can contain properties.






            function foo() {}

            foo.bar = function () { console.log('i am bar'); };

            foo.bar();








            share|improve this answer












            A functions is not only a first class function, but also a first class object and can contain properties.






            function foo() {}

            foo.bar = function () { console.log('i am bar'); };

            foo.bar();








            function foo() {}

            foo.bar = function () { console.log('i am bar'); };

            foo.bar();





            function foo() {}

            foo.bar = function () { console.log('i am bar'); };

            foo.bar();






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Dec 22 at 9:40









            Nina Scholz

            175k1388152




            175k1388152








            • 5




              Yes. From a static, OO point of view, this could be explained as “function application is just a special method you can call on a function object, with syntactic sugar that makes it look like maths function application”, i.e. express() would be syntactic sugar for express.<operator"()">(). From a dynamic, FP view, it would be explained as “OO methods are just special functions that take the name of the method as the first argument”, i.e. express.static('public') would be syntactic sugar for express(<methodname "static">, 'public').
              – leftaroundabout
              Dec 22 at 13:40








            • 4




              @leftaroundabout: Neither of those matches the formalism of how it's actually defined in Javascript, though.
              – Henning Makholm
              Dec 22 at 14:42










            • @HenningMakholm The first one actually does. The "<operator"()">" method is named [[call]] in the spec, although it is an internal method not a normal object property.
              – Bergi
              Dec 22 at 19:38












            • @Bergi: That's what I mean: [[call]] is special and not just sugar. And it's equally present in named method calls: foo.bar() would then be sugar for foo["bar"].<operator()>() plus some additional semantics to set this appropriately.
              – Henning Makholm
              Dec 22 at 21:54
















            • 5




              Yes. From a static, OO point of view, this could be explained as “function application is just a special method you can call on a function object, with syntactic sugar that makes it look like maths function application”, i.e. express() would be syntactic sugar for express.<operator"()">(). From a dynamic, FP view, it would be explained as “OO methods are just special functions that take the name of the method as the first argument”, i.e. express.static('public') would be syntactic sugar for express(<methodname "static">, 'public').
              – leftaroundabout
              Dec 22 at 13:40








            • 4




              @leftaroundabout: Neither of those matches the formalism of how it's actually defined in Javascript, though.
              – Henning Makholm
              Dec 22 at 14:42










            • @HenningMakholm The first one actually does. The "<operator"()">" method is named [[call]] in the spec, although it is an internal method not a normal object property.
              – Bergi
              Dec 22 at 19:38












            • @Bergi: That's what I mean: [[call]] is special and not just sugar. And it's equally present in named method calls: foo.bar() would then be sugar for foo["bar"].<operator()>() plus some additional semantics to set this appropriately.
              – Henning Makholm
              Dec 22 at 21:54










            5




            5




            Yes. From a static, OO point of view, this could be explained as “function application is just a special method you can call on a function object, with syntactic sugar that makes it look like maths function application”, i.e. express() would be syntactic sugar for express.<operator"()">(). From a dynamic, FP view, it would be explained as “OO methods are just special functions that take the name of the method as the first argument”, i.e. express.static('public') would be syntactic sugar for express(<methodname "static">, 'public').
            – leftaroundabout
            Dec 22 at 13:40






            Yes. From a static, OO point of view, this could be explained as “function application is just a special method you can call on a function object, with syntactic sugar that makes it look like maths function application”, i.e. express() would be syntactic sugar for express.<operator"()">(). From a dynamic, FP view, it would be explained as “OO methods are just special functions that take the name of the method as the first argument”, i.e. express.static('public') would be syntactic sugar for express(<methodname "static">, 'public').
            – leftaroundabout
            Dec 22 at 13:40






            4




            4




            @leftaroundabout: Neither of those matches the formalism of how it's actually defined in Javascript, though.
            – Henning Makholm
            Dec 22 at 14:42




            @leftaroundabout: Neither of those matches the formalism of how it's actually defined in Javascript, though.
            – Henning Makholm
            Dec 22 at 14:42












            @HenningMakholm The first one actually does. The "<operator"()">" method is named [[call]] in the spec, although it is an internal method not a normal object property.
            – Bergi
            Dec 22 at 19:38






            @HenningMakholm The first one actually does. The "<operator"()">" method is named [[call]] in the spec, although it is an internal method not a normal object property.
            – Bergi
            Dec 22 at 19:38














            @Bergi: That's what I mean: [[call]] is special and not just sugar. And it's equally present in named method calls: foo.bar() would then be sugar for foo["bar"].<operator()>() plus some additional semantics to set this appropriately.
            – Henning Makholm
            Dec 22 at 21:54






            @Bergi: That's what I mean: [[call]] is special and not just sugar. And it's equally present in named method calls: foo.bar() would then be sugar for foo["bar"].<operator()>() plus some additional semantics to set this appropriately.
            – Henning Makholm
            Dec 22 at 21:54















            2














            You can attach a function as member data to another function (which is what is done in your example).



            const express = () => {};
            express.static = argument => console.log('argument');
            express.static('public'); # console >>> 'public'


            However, you cannot readily access a variable that is defined in a function body.



            const express = () => {
            const static = argument => console.log('argument');
            };
            express.static('public'); # console >>> Error: undefined in not a function


            There is a signifiant difference between member data attached to a function (the first example) and the closure that wraps the body of the function (the second example).



            So, to answer your question "is it possible in JavaScript to call a function which is inside a function?" No, this is not readily possible with the important note that this is not what is being done in your example.






            share|improve this answer


























              2














              You can attach a function as member data to another function (which is what is done in your example).



              const express = () => {};
              express.static = argument => console.log('argument');
              express.static('public'); # console >>> 'public'


              However, you cannot readily access a variable that is defined in a function body.



              const express = () => {
              const static = argument => console.log('argument');
              };
              express.static('public'); # console >>> Error: undefined in not a function


              There is a signifiant difference between member data attached to a function (the first example) and the closure that wraps the body of the function (the second example).



              So, to answer your question "is it possible in JavaScript to call a function which is inside a function?" No, this is not readily possible with the important note that this is not what is being done in your example.






              share|improve this answer
























                2












                2








                2






                You can attach a function as member data to another function (which is what is done in your example).



                const express = () => {};
                express.static = argument => console.log('argument');
                express.static('public'); # console >>> 'public'


                However, you cannot readily access a variable that is defined in a function body.



                const express = () => {
                const static = argument => console.log('argument');
                };
                express.static('public'); # console >>> Error: undefined in not a function


                There is a signifiant difference between member data attached to a function (the first example) and the closure that wraps the body of the function (the second example).



                So, to answer your question "is it possible in JavaScript to call a function which is inside a function?" No, this is not readily possible with the important note that this is not what is being done in your example.






                share|improve this answer












                You can attach a function as member data to another function (which is what is done in your example).



                const express = () => {};
                express.static = argument => console.log('argument');
                express.static('public'); # console >>> 'public'


                However, you cannot readily access a variable that is defined in a function body.



                const express = () => {
                const static = argument => console.log('argument');
                };
                express.static('public'); # console >>> Error: undefined in not a function


                There is a signifiant difference between member data attached to a function (the first example) and the closure that wraps the body of the function (the second example).



                So, to answer your question "is it possible in JavaScript to call a function which is inside a function?" No, this is not readily possible with the important note that this is not what is being done in your example.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Dec 22 at 17:28









                Jared Goguen

                7,09411031




                7,09411031























                    0














                    Actually, it is possible to call a function inside another function in javaScript.
                    So it depends on the condition, Callback is widely used in js




                    (A callback is a function that is to be executed after another
                    function has finished executing
                    )




                    function doHomework(subject, callback) {
                    alert(`Starting my ${subject} homework.`);
                    callback();
                    }
                    function alertFinished(){
                    alert('Finished my homework');
                    }
                    doHomework('math', alertFinished);


                    And the second example can be Closures




                    (A closure is a feature in JavaScript where an inner function has
                    access to the outer (enclosing) function's variables)




                    function outer() {
                    var b = 10;
                    function inner() {

                    var a = 20;
                    console.log(a+b);
                    }
                    return inner;
                    }





                    share|improve this answer

















                    • 3




                      While the facts given here are technically correct (and are important ones to know in JS), I don't think this answer the specific question.
                      – Pac0
                      Dec 22 at 14:06












                    • @Pac0 I believe this answer fully satisfies (is it possible in JavaScript to call a function which is inside a function) this part of the question. So there is no reason to vote as a not useful answer!
                      – Nuriddin Kudratov
                      Dec 22 at 21:27












                    • @NurridanKudratov I didn't downvote or vote anything, but I strongly believe that when OP is talking about being "inside a function", he was tallkking about functions that could be used as an object while having a member function at the same time (express can be executed () and also has static as member). This case is not among your examples.
                      – Pac0
                      2 days ago


















                    0














                    Actually, it is possible to call a function inside another function in javaScript.
                    So it depends on the condition, Callback is widely used in js




                    (A callback is a function that is to be executed after another
                    function has finished executing
                    )




                    function doHomework(subject, callback) {
                    alert(`Starting my ${subject} homework.`);
                    callback();
                    }
                    function alertFinished(){
                    alert('Finished my homework');
                    }
                    doHomework('math', alertFinished);


                    And the second example can be Closures




                    (A closure is a feature in JavaScript where an inner function has
                    access to the outer (enclosing) function's variables)




                    function outer() {
                    var b = 10;
                    function inner() {

                    var a = 20;
                    console.log(a+b);
                    }
                    return inner;
                    }





                    share|improve this answer

















                    • 3




                      While the facts given here are technically correct (and are important ones to know in JS), I don't think this answer the specific question.
                      – Pac0
                      Dec 22 at 14:06












                    • @Pac0 I believe this answer fully satisfies (is it possible in JavaScript to call a function which is inside a function) this part of the question. So there is no reason to vote as a not useful answer!
                      – Nuriddin Kudratov
                      Dec 22 at 21:27












                    • @NurridanKudratov I didn't downvote or vote anything, but I strongly believe that when OP is talking about being "inside a function", he was tallkking about functions that could be used as an object while having a member function at the same time (express can be executed () and also has static as member). This case is not among your examples.
                      – Pac0
                      2 days ago
















                    0












                    0








                    0






                    Actually, it is possible to call a function inside another function in javaScript.
                    So it depends on the condition, Callback is widely used in js




                    (A callback is a function that is to be executed after another
                    function has finished executing
                    )




                    function doHomework(subject, callback) {
                    alert(`Starting my ${subject} homework.`);
                    callback();
                    }
                    function alertFinished(){
                    alert('Finished my homework');
                    }
                    doHomework('math', alertFinished);


                    And the second example can be Closures




                    (A closure is a feature in JavaScript where an inner function has
                    access to the outer (enclosing) function's variables)




                    function outer() {
                    var b = 10;
                    function inner() {

                    var a = 20;
                    console.log(a+b);
                    }
                    return inner;
                    }





                    share|improve this answer












                    Actually, it is possible to call a function inside another function in javaScript.
                    So it depends on the condition, Callback is widely used in js




                    (A callback is a function that is to be executed after another
                    function has finished executing
                    )




                    function doHomework(subject, callback) {
                    alert(`Starting my ${subject} homework.`);
                    callback();
                    }
                    function alertFinished(){
                    alert('Finished my homework');
                    }
                    doHomework('math', alertFinished);


                    And the second example can be Closures




                    (A closure is a feature in JavaScript where an inner function has
                    access to the outer (enclosing) function's variables)




                    function outer() {
                    var b = 10;
                    function inner() {

                    var a = 20;
                    console.log(a+b);
                    }
                    return inner;
                    }






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Dec 22 at 11:44









                    Nuriddin Kudratov

                    737




                    737








                    • 3




                      While the facts given here are technically correct (and are important ones to know in JS), I don't think this answer the specific question.
                      – Pac0
                      Dec 22 at 14:06












                    • @Pac0 I believe this answer fully satisfies (is it possible in JavaScript to call a function which is inside a function) this part of the question. So there is no reason to vote as a not useful answer!
                      – Nuriddin Kudratov
                      Dec 22 at 21:27












                    • @NurridanKudratov I didn't downvote or vote anything, but I strongly believe that when OP is talking about being "inside a function", he was tallkking about functions that could be used as an object while having a member function at the same time (express can be executed () and also has static as member). This case is not among your examples.
                      – Pac0
                      2 days ago
















                    • 3




                      While the facts given here are technically correct (and are important ones to know in JS), I don't think this answer the specific question.
                      – Pac0
                      Dec 22 at 14:06












                    • @Pac0 I believe this answer fully satisfies (is it possible in JavaScript to call a function which is inside a function) this part of the question. So there is no reason to vote as a not useful answer!
                      – Nuriddin Kudratov
                      Dec 22 at 21:27












                    • @NurridanKudratov I didn't downvote or vote anything, but I strongly believe that when OP is talking about being "inside a function", he was tallkking about functions that could be used as an object while having a member function at the same time (express can be executed () and also has static as member). This case is not among your examples.
                      – Pac0
                      2 days ago










                    3




                    3




                    While the facts given here are technically correct (and are important ones to know in JS), I don't think this answer the specific question.
                    – Pac0
                    Dec 22 at 14:06






                    While the facts given here are technically correct (and are important ones to know in JS), I don't think this answer the specific question.
                    – Pac0
                    Dec 22 at 14:06














                    @Pac0 I believe this answer fully satisfies (is it possible in JavaScript to call a function which is inside a function) this part of the question. So there is no reason to vote as a not useful answer!
                    – Nuriddin Kudratov
                    Dec 22 at 21:27






                    @Pac0 I believe this answer fully satisfies (is it possible in JavaScript to call a function which is inside a function) this part of the question. So there is no reason to vote as a not useful answer!
                    – Nuriddin Kudratov
                    Dec 22 at 21:27














                    @NurridanKudratov I didn't downvote or vote anything, but I strongly believe that when OP is talking about being "inside a function", he was tallkking about functions that could be used as an object while having a member function at the same time (express can be executed () and also has static as member). This case is not among your examples.
                    – Pac0
                    2 days ago






                    @NurridanKudratov I didn't downvote or vote anything, but I strongly believe that when OP is talking about being "inside a function", he was tallkking about functions that could be used as an object while having a member function at the same time (express can be executed () and also has static as member). This case is not among your examples.
                    – Pac0
                    2 days ago




















                    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%2f53894551%2fcalling-a-function-on-a-function-is-it-possible%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

                    "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

                    Alcedinidae

                    Origin of the phrase “under your belt”?