# Why can't functions expose their inner declarations and act like a class?

**URL:** <https://es.discourse.group/t/why-cant-functions-expose-their-inner-declarations-and-act-like-a-class/653>\
**Category:** I have questions\
**Created:** [February 20, 2021, 3:59pm UTC](https://es.discourse.group/t/why-cant-functions-expose-their-inner-declarations-and-act-like-a-class/653 "2021-02-20T15:59:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jithujoshyjy](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/jithujoshyjy/32/399_2.png) [@jithujoshyjy](https://es.discourse.group/u/jithujoshyjy)\
**Post date:** [February 20, 2021, 3:59pm UTC](https://es.discourse.group/t/why-cant-functions-expose-their-inner-declarations-and-act-like-a-class/653/1 "2021-02-20T15:59:05Z")

</div>

This is not a great technical question, but a little one that arised out of my curiosity.  
It doesn't particularly concern JS but I guess you guys will tolerate that😅  
(The follow isn't valid JS syntax! sorry!!)  
It looks like this:

```javascript
exposed function Greet(name) {
	let fullName;
	for(let i = 0; i <= 500; i++)
		console.log(i)
	// some other statements
	function sayHi() {
		alert( "Hi " + name )
	}
	function sayHello() {
		alert( "Hello " + name )
	}
	function sayGoodBye() {
		alert( "Good bye " + name )
	}
	let #nickname = "Adios"
	function #makeFullName() {
		fullName = #nickname + " " + name
	}
}

```

Or a function like:

```javascript
function Greet(name) {
	let fullName;
	for(let i = 0; i <= 500; i++)
		console.log(i)
	// some other statements
	function sayHi() {
		alert( "Hi " + name )
	}
	function sayHello() {
		alert( "Hello " + name )
	}
	function sayGoodBye() {
		alert( "Good bye " + name )
	}
	let nickname = "Adios"
	function makeFullName() {
		fullName = nickname + " " + name
	}
	return export { fullName, sayHi, sayHello, sayGoodBye }
}

```

```javascript

greet1 = new Greet("Myself")
greet1.sayHi()

greet2 = new Greet("Myself")
greet2.sayHello()

greet3 = new Greet("Myself")
greet3.sayGoodBye()

greet3.#nickname // Error

```

Why isn't this sort of functions used in programming languages?

I very well know that JS can do:

```javascript
function Circle(x, y, r) {
    this.x = x
    this.y = y
    this.r = r
    
    this.draw = () => //Draw it
}
let c = new Circle(100, 100, 10)

```

Thanks for any help👍

---

<div class="post-metadata">

**Author:** ![ljharb](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ljharb/32/8_2.png) [@ljharb](https://es.discourse.group/u/ljharb)\
**Post date:** [February 20, 2021, 4:11pm UTC](https://es.discourse.group/t/why-cant-functions-expose-their-inner-declarations-and-act-like-a-class/653/2 "2021-02-20T16:11:40Z")

</div>

If functions worked like that, how would you _prevent_ exposing function-internal helper functions? You'd end up needing syntax to keep things private, which means the default behavior would be for every function's implementation details to be part of its API - which means that renaming or removing one of these helper functions would be a breaking change to consumers.

---

<div class="post-metadata">

**Author:** ![theScottyJam](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/thescottyjam/32/616_2.png) [@theScottyJam](https://es.discourse.group/u/theScottyJam)\
**Post date:** [February 20, 2021, 4:45pm UTC](https://es.discourse.group/t/why-cant-functions-expose-their-inner-declarations-and-act-like-a-class/653/3 "2021-02-20T16:45:36Z")

</div>

Your second example is very similar to something that can already be done:

Instead of doing

```javascript
return export { fullName, sayHi, sayHello, sayGoodBye }

```

just do

```javascript
Object.assign(this, { fullName, sayHi, sayHello, sayGoodBye })

```

or this will actually work too

```javascript
return { fullName, sayHi, sayHello, sayGoodBye }

```

And now you've explicitly created your instance with some locals.

---

<div class="post-metadata">

**Author:** ![bergus](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/bergus/32/152_2.png) [@bergus](https://es.discourse.group/u/bergus)\
**Post date:** [February 20, 2021, 5:07pm UTC](https://es.discourse.group/t/why-cant-functions-expose-their-inner-declarations-and-act-like-a-class/653/4 "2021-02-20T17:07:12Z")

</div>

These are known as factory functions, and they're used all the time. If you omit the `export` keyword to make it valid JS, it works out of the box:

```javascript
function makeGreet(name) {
	function sayHi() {
		alert( "Hi " + name )
	}
	function sayHello() {
		alert( "Hello " + name )
	}
	function sayGoodBye() {
		alert( "Good bye " + name )
	}
	return { sayHi, sayHello, sayGoodBye }
}

const greeter = makeGreet("Myself")
greeter.sayHi()
greeter.sayHello()
greeter.sayGoodBye()

```
