0

In this example, the greet() function is invoked through sample.greet().

let sample = {
    greet() {
        console.log("hi")
    }
}

How to invoke the inner greet() function if defined like this?

function sample() {
    function greet() {
        console.log("hi")
    }
}
colourCoder
  • 1,394
  • 2
  • 11
  • 18
CHECoder08
  • 41
  • 3
  • 2
    Having functions *inside* function code, and having functions *on* function objects as properties, are different kettle of fish. – Bergi Sep 06 '20 at 20:37
  • 1
    Have a look at https://stackoverflow.com/questions/16596117/can-you-create-functions-with-custom-prototypes-in-javascript/, https://stackoverflow.com/questions/50612293/what-is-an-idiomatic-way-to-put-fields-attributes-on-a-function, https://stackoverflow.com/questions/8588563/adding-custom-properties-to-a-function and https://stackoverflow.com/questions/29003166/what-is-the-difference-between-giving-value-inside-function – Bergi Sep 06 '20 at 20:42

3 Answers3

2

Functions declared inside a function body such as the greet() function in your example here:

function sample() {
    function greet() {
        console.log("hi")
    }
}

are private to within the function body and cannot be called from outside of the sample() function scope. You can only call greet() from within the sample() function body unless you somehow assign or return greet after running sample() so you purposely assign greet to some outside variable (which is not present in your example).


Functions are objects so you can create properties on those objects and can then assign a function to a property and can then call it:

function sample() {
    console.log("running sample()");
}

sample.greet = function () {
    console.log("hi")
}

sample.greet();
jfriend00
  • 683,504
  • 96
  • 985
  • 979
0

You can call the nested function by calling it within the enclosing function. This is because the scope of the nested function is limited to the enclosing function and there is no other way to call it from outside.

function sample() {
    function greet() {
        console.log("hi");
    }
    greet();
}

sample()
Jai
  • 11
  • 2
0

You can assign properties to functions the same way you can for objects. You can assign another function as one of those properties, which makes it like a method.

function foo(){
    console.log("foo called");
}

foo.bar = function() {
    console.log("foo.bar called");
}

foo(); // "foo called"
foo.bar(); // "foo.bar called"

You can also assign any value to a property on a function.

foo.x = 1;
console.log(foo.x); // 1

So yes, functions are objects and it is possible to treat them like objects.

Chris Rollins
  • 550
  • 3
  • 9
  • "*It is interesting but not standard practice*" - I have to disagree here. Thousands of popular libraries are using this approach. Name any. – Bergi Sep 06 '20 at 22:00
  • It's understandable that a library would use it to provide a more flexible API. Anyway I will remove it from my answer because it's a matter of opinion I guess. – Chris Rollins Sep 06 '20 at 22:13