0

I have a clarification in using Immediately Invoked functions (IIFs) in JavaScript. I have downloaded a JavaScript file called called test.js as follows and I have got following questions after Googling IIFs:

define(function () { (function (window) { this.test = function() {}; Test.prototype.function1 = function(){ //Do something }, function Delete(){ //Code to Delete } window.Delete = Delete; })(window); }); 

I do have the following questions:

  1. Is the line, this.test = function() {}; a constructor? If so can I have 2 constructors in a single file like for example:

     this.test = function() {}; this.test2 = function() {}; 

    And also, why would I need a constructor when I know that this is an automatically invoked file where everything gets executed initially itself.

  2. Is this a private function?

     Test.prototype.function1 = function(){ //Do something }, 

    Does this not get automatically? Should I need to create an object of the test and then invoke it?

  3. Is this a public function?

     function Delete(){ //Code to Delete } window.Delete = Delete; 

    The last line of the above says that . If it is so then whats the difference between first and second function?

  4. What is keyword window here?

3
  • Please ask only one question per post. The answers would be 1) No 2) No 3) No 4) not a keyword Commented Dec 21, 2015 at 11:26
  • I don't see any IIFE in your code. And the code appears to be quite crappy. Is this the original code? And where did you download it? Commented Dec 21, 2015 at 11:27
  • You're not executing the funtion you need to add })(window); Commented Dec 21, 2015 at 11:27

1 Answer 1

4
+50

It's worth noting that this code will fail with an error, as Test is undefined, and you can't set the prototype property on undefined.

  1. In JavaScript, any function can be a constructor. It's up to how you use it. You can add functions and properties to the .prototype property of any function, and any objects created from it using new will get them from the prototype chain. In your case, this.test = function(){} doesn't look like a prototype.
  2. There's no such things a "public" or "private" functions in JavaScript, there's only what's exposed via return out of the function (or in your case, by using the global window object, which is considered bad practice) If the Test function is exposed somewhere, then Test.prototype.function1 is also exposed. All prototype methods are "public".
  3. Yes, sort of. Like I said, "public" or "private" isn't a thing in JavaScript. You created a function and placed it on the window object, which is global. Essentially, you've made the function global.
  4. window is the global browser object. Although when used like this (function(window) { ... })(window), the first window is the name of the parameter, (and any instances of window inside the function reference to that parameter, and the second one (passed to the function call), is the global window object.

Further reading:

Sign up to request clarification or add additional context in comments.

4 Comments

Hi, I have one more clarification: why would the code fail because there is already a test function defined this.test = function() {}; and in the next line Test.prototype has been used. . Is my understanding correct?
this.test and Test are two completely different identifiers/objects. They are not equivalent.
Ok one last clarification, assuming that the code compiles , is this a constuctor ?Test.prototype.function1 = function(){ //Do something } . If yes , then is it necessary to use prototype object to make it as a constuctor?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.