How to call function in javascript

80 views Asked by At

I am trying to decode a code and was stuck at the function calling. The function is defined in the below way.

    Function(){} ({
    
    g: 0,
    
    h :{},
    
    c:{
      a:1,
      b:2,
      d:4
    }
    
    });

Please help me how to call the above function. How to display g and to access c defined variables.

3

There are 3 answers

0
Ricardo Rocha On

I don't know really what you want but I will take the following assumptions:

  1. Your function declaration are wrong. Check in here how to declare functions on javascript;
  2. I suppose that you want to create a function that returns a given object an then working with that;

Based on the guesses above, follows a workable code:

// The correct function declaration. This function returns an object.
function getObject() {
  return  {
      g: 0,
      h :{},
      c:{
        a:1,
        b:2,
        d:4
      } 
  };
}
//The object variable will contain the result of the `getObject()` function
let object = getObject();

//Prints the object to the console
console.log(object);
//Prints the g value to the console
console.log(`g Value: ${object.g}`);
//Prints the c value to the console
console.log(object.c);
//Prints to the console the a value that is inside of the c object
console.log(`a Value inside c: ${object.c.a}`);

2
Kendall Adkins On

Two possible solutions to my interpretation of your question...

function test () {
    return {
        g: 0,
        h: {},
        c: {
            a:1,
            b:2,
            d:4
        }
    }
}

test().g 

0

test().c.a 

1

looks like you forgot to name your function and syntax is a bit off...

The only other thing I can think of is you are trying to get a function that is attached to the Window object. If so, you access using window.Function but I don't know why you'd do that.

if this is not what you are looking for let me know. But that's what I got from reading your question. Hope it helps.

0
Josh On

Maybe the function was supposed to be IIFE (Immediately Invoked Function Execution) holding an object which should have been written in the way:

(function() {

    {
       g: 0,
       h :{},
       c:{
          a:1,
          b:2,
          d:4
         }
    }
   }())