Javascript - Object Constructor


Object - Constructor in Javascript of an object

Invoking a function with the new operator treats it as a constructor.

Unlike function calls and method calls, a constructor call passes a brand-new object as the value of this, and implicitly returns the new object as its result. The constructor function’s primary role is to initialize the object.

The constructor can be see as property of a function (object).

Not to confound with a function constructor.


var f = new foo(a);

is similar to the object creation to prototype.

var f = Object.create(foo.prototype);, a);

See Javascript - Call method

Array as Argument

When calling a constructor with new, it's not possible to directly use an array and apply (apply does a Call and not a Construct). However, an array can be easily used with new thanks to spread syntax:

var dateFields = [1970, 0, 1];  // 1 Jan 1970
var d = new Date(...dateFields);

See en-US/docs/Web/JavaScript/Reference/Operators/Spread_operator


function myObject(arg){
   this.prop1 = arg;
   this.prop2 = arg + arg;
   this.prop3 = arg.toUpperCase;

var obj1 = new myObject("foo");
console.log("An object obj1 was created with the following structure:");


See the code of the constructor of an object

function myObject(arg){
   this.prop1 = arg;
   this.prop2 = arg + arg;

var obj1 = new myObject("foo");
console.log("The code of the constructor function of this object is:");

Get the objects of a constructor

Call queryObjects(Constructor) from the Console to return an array of objects that were created with the specified constructor.

For example:

  • queryObjects(Promise). Returns all Promises.
  • queryObjects(HTMLElement). Returns all HTML elements.
  • queryObjects(foo), where foo is a function name. Returns all objects that were instantiated via new foo().

Discover More
How to check the equality of 2 values in Javascript? (If they are the same)

This page discusses Equality between 2 Javascript values. Because every value in javascript is an object, it applies also to the object. verifies the value. in case of the primitive,...
JavaScript - new

The new word indicate a constructor of: an object via: a or a class. See a function. See
Javascript - (Prototype Model|Prototypical object)

A prototype-based language has the notion of a prototypical object. Any object can: specify its own properties, either when you create it or at run time. be associated as the prototype for another...
Javascript Lexical Environment Scope Chaine
Javascript - (Variable) Scope (Namespace)

Variable scope in Javascript. variable scope is delimited by the function definition, not a the block level A block has the scope of its inner function. As a function is also an object, by generalization,...
Javascript - Method

Methods in JavaScript are nothing more than object properties that happen to be functions. Basic: same as with
Javascript - Object

object in javascript An object is a data type in Javascript. An object follows a json data structure with key that holds property value. The value may be of a variable or a function In JavaScript, a...
Javascript - instanceof

instanceof instanceofObjectprototype An object created from a class will always be as an instanceof its class. The constructor of a function createdWITHOUT a prototype will beNOT recognized...

Share this page:
Follow us:
Task Runner