简体   繁体   中英

How to call function in javascript

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.

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.ca}`);

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.

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
         }
    }
   }())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM