skip to Main Content

This is what I tried:

var app = function() {
    var listen; 
    return listen = function()
        { 
            return "ggg";
        }
    }

console.log("dd: " + app().listen().length);

This says listen is not a function.
console.log("dd: " + app().listen().length);

Now w.r.t socket.io, we can write:

  const express = require('express');
  const app = express();
  app.listen(); 

I want to understand how to write like socket.io where from we can call one function from another?

2

Answers


  1. you are returning an expression which returns undefined as somevar = ‘somevalue’ will give you undefined. instead you should

        return {
    listen: function()
                { 
                    return "ggg";
                }
        }
    
    Login or Signup to reply.
  2. You seem to want to create a factory function. The problem is that you are calling it’s result as if it were an Object (with method listen). The way you created the function app returns a (named) function. See the snippet

    const app = function() {
      return listen = function() {
        return "ggg";
      };
    }
    
    const appFactory = function() {
      return {
        listen() {
          return "ggg";
        }
      };
    }
    
    // your function returns a function
    console.log("dd: " + app()() + 
      ` => the returned function name is '${app().name}'`);
    
    // the factory returns an object
    // with method 'listen'
    console.log("dd: " + appFactory().listen());
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search