0

In JavaScript I can call objects, for example; String, Number and Boolean in two different ways, one as a standard function call, the other as a constructor.

Example

// Call String as a standard function call.
var a = String("Hello World");
// a = "Hello World";

// Call String as a constructor function.
var b = new String("Hello World");
// b = String {0: "H", 1: "e", 2: "l", 3: "l", 4: "o", 5: " ", 6: "W", 7: "o", 8: "r", 9: "l", 10: "d"}

Say for example I'm now designing my own object; Foo

var Foo = (function () {
    function Foo(value) {
        // Construct a foo object
    }
    return Foo;
})();

// Test
var foo = new Foo();

Okay, so I can call Foo as a constructor function, but how do I implement Foo as a standard function call, like so

var foo = Foo(myValue);
Matthew Layton
  • 39,871
  • 52
  • 185
  • 313

1 Answers1

1

for String, Bolean and Number there is no difference since internally they resolve to a constructor call. they are defined in the language in wich the javascript interpreter is written in (most of the time c++) so they differ to usual javascript objects / functions / constructors even though they behave like them.

GottZ
  • 4,824
  • 1
  • 36
  • 46