Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

William Hurst
William Hurst
25,000 Points

Purpose of Object()()

I have been looking through various JS code.

Specifically looking at WooCommerce Booking Availability JS file (just as an example for using ()()) - I have noticed they use Object()():

Here is a code snippet:

var e = Object.create(null);

var a = function () {
    return Object(e.createElement)(n.Icon, {
      icon: Object(e.createElement)("svg", {
        xmlns: "http://www.w3.org/2000/svg",
        width: "24",
        height: "24",
        viewBox: "0 0 24 24"
      }, Object(e.createElement)("path", {
        d: "M19 5v14H5V5h14m1.1-2H3.9c-.5 0-.9.4-.9.9v16.2c0 .4.4.9.9.9h16.2c.4 0 .9-.5.9-.9V3.9c0-.5-.5-.9-.9-.9zM11 7h6v2h-6V7zm0 4h6v2h-6v-2zm0 4h6v2h-6zM7 7h2v2H7zm0 4h2v2H7zm0 4h2v2H7z"
      }), Object(e.createElement)("path", {
        fill: "none",
        d: "M0 0h24v24H0z"
      }))
    })
  }

the line to look at is return Object(...)(...).

I know that using ()() calls function that returns a function and that the second () is to call the returned function. Such as:

var doubleP = function(x){
    return function(y, z){
        console.log(x, y, z);
    }
}
doubleP('x')("y", "z");

Is this all that the above code is doing?

Or is there another technique available for the Object Constructor?

1 Answer

Justin Horner
STAFF
Justin Horner
Treehouse Guest Teacher

Hello, William

I've never worked with WooCommerce, so I'm not familiar with the codebase, but I believe you're right. The code is simply referencing a function first using Object() and then calling it passing arguments needed.

When you call Object and pass a function, you can then immediately invoke that function by using the second set of parentheses, like this.

function add(num1, num2) {
     return num1 + num2;
}

var result = Object(add)(5, 7);
console.log(result); // 12

I hope this helps. Happy coding 🙂