Day 7 – Learning Node JS In 30 Days [Mini series]

Day 7 – Learning Node JS In 30 Days [Mini series]

Today we’re going to learn about some more about module

We are using files as last posts. Now it can happens that in our song folder there are two different functions.

No alt text provided for this image

Now how to send this both module, as per we know from last posts we can’t send both functions using this module.exports

No alt text provided for this image

Adding module.exports.VARIABLE_NAME we can send it to other file

No alt text provided for this image

By using Song.VARIABLE_NAME what we’ve send from song.js file we can call the functions

No alt text provided for this image

This is not all, there are some others way you can do it to. What if you have 100 functions in the same file are you going to write one by one ?

Now we can just use module.exports as a object ! Example –

No alt text provided for this image

app.js will be the same and if we run it now then same result

No alt text provided for this image

now we can modify code a bit. When the name are same in es6 version we can write just once.

No alt text provided for this image

So, do you feel you know a bit about module now ?

要查看或添加评论,请登录

Muhammad Ali的更多文章

社区洞察

其他会员也浏览了