Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is this javascript idiom? [duplicate]

I was going through broccoli plugins and I see this line a lot. What is it used for?

function MyCompiler (arg1, arg2, ...) {
  if (!(this instanceof MyCompiler)) return new MyCompiler(arg1, arg2, ...);
  ...
};
like image 553
akula1001 Avatar asked Feb 11 '23 09:02

akula1001


1 Answers

That is so that you can use it with or without the new keyword.

E.g.:

var comp = new MyCompiler();

or:

var comp = MyCompiler();

If you call it as a function, it will call itself with the new keyword and return the instance.

like image 83
Guffa Avatar answered Feb 13 '23 23:02

Guffa