Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it right to think of a Javascript Function Expression that uses the 'new' keyword as 'static'

Tags:

javascript

I'm just trying to understand Javascript a little deeper.

I created a 'class' gameData that I only want ONE of, doesn't need a constructor, or instantiated.

So I created it like so...

var gameData = new function () {      //May need this later      this.init = function () {      };      this.storageAvailable = function () {         if (typeof (Storage) !== "undefined") {             return true;         }         else {             return false;         }     }; } 

Realizing that the 'new' keyword doesn't allow it to be instantiated and makes it available LIKE a static class would be in C#.

Am I thinking of this correctly? As static?

like image 313
Todd Vance Avatar asked May 02 '12 01:05

Todd Vance


People also ask

Should I use function expressions in JavaScript?

In short, use function declarations when you want to create a function on the global scope and make it available throughout your code. Use function expressions to limit where the function is available, keep your global scope light, and maintain clean syntax.

Which keyword is used to define a function in JavaScript?

A JavaScript function is defined with the function keyword, followed by a name, followed by parentheses (). Function names can contain letters, digits, underscores, and dollar signs (same rules as variables).

How do you write a function expression in JavaScript?

Syntax for Function Expression (named) :let variableName = function functionName(x, y) { statements... return (z) };

What is the benefit of function expression in JavaScript?

There are some benefits of function expressions over statements, for example they are anonymous functions, they can be used as closures, as arguments to other functions and as IIFEs. Put simply a statement in the execution phase it does not return anything, an expression results to a value, an object is created.


1 Answers

No, it is not static because it still has a constructor property pointing to your "anonymous" function. In your example, you could use

var gameData2 = new (gameData.constructor)(); 

to reinstantiate a second object, so the "class" (instance actually) is not really "static". You are basically leaking the constructor, and possibly the data that is bound to it. Also, a useless prototype object (gameData.constructor.prototype) does get created and is inserted in the prototype chain of gameData, which is not what you want.

Instead, you might use

  • a single, simple object literal (as in Daff's answer). That means you don't have a constructor, no closure-scoped private variables (you have used none anyway) and no (custom) prototype.
  • the (revealing) module pattern (as in jAndy's answer). There you'd have an IIFE to create closure-scoped variables, and can return any kind of object.
  • an actual constructor ("class") that can be instantiated later (when needed), and yields the same singleton object always.

This is what the singleton pattern would look like:

function GameData() {     if (this.constructor.singleton)         return this.constructor.singleton;     else         this.constructor.singleton = this;      // init:     // * private vars     // * public properties     // ... } GameData.prototype.storageAvailable = function () {     if (typeof (Storage) !== "undefined") {         return true;     }     else {         return false;     } };  var gameData = new GameData(); var gameData2 = new GameData(); gameData === gameData2 === GameData.singleton; // true 

Yet, the prototype is quite useless because you have only one instance of GameData. It would only get interesting with inheritance.

like image 130
Bergi Avatar answered Sep 23 '22 18:09

Bergi