zaydek zaydek - 2 months ago 5
Javascript Question

How to write a method for a function?

In order to use

hello.world
in the function listed below,
hello()
must be called once. Is there a way to write functions / methods / blocks for functions that do not require the parent function to be called once, first?

According to me it is not a duplicate of JavaScript private methods

function hello() {

world = function world() {

return 'hello world'

}

return 'hello'

}

Answer

Answer is easier than I thought.

function hello() {

    return 'hello'

}

hello.world = function() {

    return 'hello world'

}

hello() and hello.world() work as expected and without either hello() needing to be called once in order for hello.world() to work.

Comments