3

I have just started learning javascript and nodejs by following a tutorial. One of the code they used is:

var express=require('express');
var app=express();
app.listen(3000);

Now when I do:

console.log(typeof app);
console.log(typeof app.listen);

I get:

function
function

as the output. Now if the typeof app is function, then how can we use . operator with it to access some other function(listen)? I tried to read the express module but couldn't understand much.

Can anyone give a simple example where we use a function name with . operator to access some other function inside it?

I tried this:

var t=function(){
    t.bar=function(){
        console.log('bar');
        }
    };  

t.bar();

But doesn't work.

4
  • You haven't defined t.bar in your example until you've called t()... Commented Mar 14, 2017 at 10:11
  • 3
    Functions are first-class citizens: function a() {} a.some = function() { console.log('Some'); } Commented Mar 14, 2017 at 10:11
  • @Tushar so can we use them just as we use objects? Commented Mar 14, 2017 at 10:14
  • Function ARE objects. :) So, yes. Commented Mar 14, 2017 at 10:20

3 Answers 3

2

Functions are objects, and they can have properties just like any other object.

var t = function() {
    console.log('foo');
};

t.bar = function() {
    console.log('bar');
};

t.bar();

t.favColor = 'red';
t.shoeSize = 12;
Sign up to request clarification or add additional context in comments.

1 Comment

Use the snippet, it makes the answer more reliable.
1

In Javascript, functions are objects. This is how you can define an object:

var myObject = {/*Some members*/};

This is how you define a function

var myFunction = function(/*Some parameters*/) {/*Do something*/};

typeof myObject

will result in "object"

and

typeof myFunction

will result in "function"

The difference between them is that myFunction can be called, while myObject cannot be called. For example this

({})()

will result in an error. However, otherwise they are very similar, they can have members in the same way.

myFunction.myInnerFunction = function(/*Some other parameters*/) {/*Do something else*/};

assigns the myInnerFunction member of myFunction a value which happens to be a function. From there on you will be able to call myFunction.myInnerFunction. However, you will need to read more about functions, you may start your study with the this keyword.

Comments

0

Your question is about basics of language.

There are lots of solutions.

It depends what You want to achieve.

Here are few examples:

const t = () => {}; 
t.bar = () => { 
    console.log('bar'); 
}; 

t.bar();

or:

const t = {
  bar: () => { 
    console.log('bar'); 
  }
};  

t.bar();

or:

class t {
  static bar() {
    console.log('bar'); 
  }
}

t.bar();

or:

class T {
  static bar() {
    console.log('bar'); 
  }
}

const t = new T();
t.bar();

and fix on Your example (same as in examples above):

var t = function {};
t.bar = function() { 
    console.log('bar'); 
};

t.bar();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.