Zum Hauptinhalt springen
tsecurity.de LIVE
Echtzeit-Radar & Feeds
Alle RSS Feeds
👥 Community & Social
Sicherheitslücken (CVE)USN-8797-1: GStreamer Base Plugins vulnerability(21.09.2026 um 20:05 Uhr)
Sichere ProgrammierungYou can build HTML emails with Tailwind CSS(21.09.2026 um 22:15 Uhr)
Sichere ProgrammierungDEV-Part-1-Backend.md(21.09.2026 um 22:24 Uhr)
Sichere ProgrammierungWhat It Actually Costs to Serve a 1M-Token Model in Production(21.09.2026 um 22:33 Uhr)
Sichere ProgrammierungHow to Check an Agent's Diagnosis Before It Touches Production(21.09.2026 um 22:53 Uhr)
Linux Tipps & HardeningWhat if Spotify was self-hosted? I think I got pretty close.(21.09.2026 um 22:33 Uhr)
Linux Tipps & HardeningSandboxing on Linux(21.09.2026 um 22:45 Uhr)
Sicherheitslücken (CVE)USN-8797-1: GStreamer Base Plugins vulnerability(21.09.2026 um 20:05 Uhr)
Sichere ProgrammierungYou can build HTML emails with Tailwind CSS(21.09.2026 um 22:15 Uhr)
Sichere ProgrammierungDEV-Part-1-Backend.md(21.09.2026 um 22:24 Uhr)
Sichere ProgrammierungWhat It Actually Costs to Serve a 1M-Token Model in Production(21.09.2026 um 22:33 Uhr)
Sichere ProgrammierungHow to Check an Agent's Diagnosis Before It Touches Production(21.09.2026 um 22:53 Uhr)
Linux Tipps & HardeningWhat if Spotify was self-hosted? I think I got pretty close.(21.09.2026 um 22:33 Uhr)
Linux Tipps & HardeningSandboxing on Linux(21.09.2026 um 22:45 Uhr)
Intelligence View
⚡ tsecurity.de Intelligence

Constructor in JavaScript

A constructor function is a special function used to create many objects with the same structure. Curly braces represent an object. Create many Objects easily. Each Key-value pair is called Property Instead of writing many objects…

0
↗ Quelle (dev.to)
Reagiere als Erste:r — dein Feedback zählt!

A constructor function is a special function used to create many objects with the same structure.

Curly braces represent an object.

Create many Objects easily.

Each Key-value pair is called Property

Instead of writing many objects manually, we create a blueprint

name constructor functions with an upper-case first letter.



constructor()

Special method inside a class

Runs automatically when object is created




function Person(name, age) {






Explanation

function Person(name, age)



function → keyword to define a function

Person → constructor name

name, age → parameters



Understanding this

this.name = name;

this=>refers to the current object being created



this.name=>creates a property called name inside the object



= name(assigns the value received from the parameter)




Example :

{
name : "Priya"
}






this.age = age;

creates an age property

stores the value passed to the constructor



object becomes:



{

name : "Priya",

age : 21

}



Steps:




  1. Creates an empty object { }

  2. Sets this to point to that object

  3. Calls the constructor function

  4. Assigns values (name, age)

  5. Returns the object



Instantiating an object constructor

There are two ways to instantiate object constructor,




const object_name = new Object(); 
const object_name = { };









function Person(first, last, age, eye) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eye;
}

// Create a Person object
const myFather = new Person("John", "Doe", 50, "blue");
const myMother = new Person("Saraswathi","g",42,"black")
console.log(myFather.age)






Explanation

In constructor function,'this' has no value

this value = new object value



Property Default Value




function Person(first, last, age, eye) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eye;
this.nationality = "English";
}

// Create 2 Person objects
const myFather = new Person("John", "Doe", 50, "blue");
const myMother = new Person("Sally", "Rally", 48, "green");
console.log(myMother.nationality)






Adding Property to an Object




function Person(first, last, age, eye) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eye;
}

// Create 2 Person objects
const myFather = new Person("John", "Doe", 50, "blue");
const myMother = new Person("Sally", "Rally", 48, "green");

// Add nationality to first object
myFather.nationality = "English";
console.log(myFather.nationality)






Adding Property to Constructor




function Person(first, last, age, eye) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eye;
}

// Create 2 Person Objects
const myFather = new Person("John", "Doe", 50, "blue");
const myMother = new Person("Sally", "Rally", 48, "green");

// Will Not Work
Person.nationality = "English";
console.log(Person.nationality)






Add a new property add it to constructor function prototype




function Person(first, last, age, eye) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eye;
}

// Create 2 Person Objects
const myFather = new Person("John", "Doe", 50, "blue");
const myMother = new Person("Sally", "Rally", 48, "green");

// Add a new Property
Person.prototype.nationality = "English";
console.log(myFather.nationality)






Explanation

Person.nationality =>Property belongs to constructor only.

Person.prototype.nationality => Property shared by all objects.



Constructor Function Methods

can have methods




function Person(first, last, age, eye) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eye;
this.fullName = function() {
return this.firstName + " " + this.lastName
};
}

// Create a Person object
const myFather = new Person("John", "Doe", 50, "blue");
console.log(myFather.fullName())






Adding method to an object




function Person(first, last, age, eye) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eye;
}

// Create 2 Person objects
const myFather = new Person("John", "Doe", 50, "blue");
const myMother = new Person("Sally", "Rally", 48, "green");

// Add a Name Method
myMother.changeName = function (name) {
this.lastName = name;
}

// Change Name
myMother.changeName("Doe");
console.log(myMother.lastName)






Output

Doe



Calling the Method




p1.sayHello();






Output:

My name is Priya and I am 21 years old.



Adding a Method to a Constructor

Adding a new method to a constructor must be done to the constructor function prototype:




function Person(firstName,lastName,age,eyeColor) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
this.eyeColor = eyeColor;
}

// Create a Person Object
const myMother = new Person("Sally","Rally",48,"green");

Person.prototype.changeName = function (name) {
this.lastName = name;
}

// Change Name
myMother.changeName("Doe");
console.log(myMother.lastName)






Built-in JavaScript Constructors



JavaScript has built-in constructors for all objects:

new Object() // A new Object object

new Array() // A new Array object

new Map() // A new Map object

new Set() // A new Set object

new Date() // A new Date object

new RegExp() // A new RegExp object

new Function() // A new Function object



Example




 "";           // primitive string
0; // primitive number
false; // primitive boolean

{}; // object object
[]; // array object
/()/ // regexp object
function(){}; // function






Need to Rememeber




  • The Math() object is not in the list. Math is a global object. The new keyword cannot be used on Math.

  • Use object literals {} instead of new Object().

  • Use array literals [] instead of new Array().

  • Use pattern literals /()/ instead of new RegExp().

  • Use function expressions () {} instead of new Function().



** Simple Real-Life Example**



Constructor = factory machine

Person constructor

|

|---- Person1

|---- Person2

|---- Person3

Each object has same structure but different values.



Why Constructors are Useful

Without constructor:



object1

object2

object3



You must write many objects manually.



With constructor:

Person() → blueprint

|

|---- p1

|---- p2

|---- p3

You can easily create many objects.



Constructor Using Class (Modern Way)




class Student {
constructor(name, age) {
this.name = name;
this.age = age;
}
}

let student = new Student("Priya", 22);
console.log(Student);






Constructor Function (Old Way)

Before ES6, constructors were written like this:




function Student(name, age) { 
this.name = name;
this.age = age;
}

let student = new Student("Priya", 22);






This still works, but modern JS prefers class syntax.



Concept Meaning

Object Collection of properties

Constructor Blueprint to create objects

this Refers to current object

new Creates a new object

Method Function inside object

Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Constructor in JavaScript

Thematisch verwandte Begriffe: Constructor, JavaScript · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Zum Aktualisieren ziehen
ZERO-DAY CVE-2026-45381 | Tautulli is a Python based monitoring and tracking tool for Plex Media S…
Advisory →
TTS Reader • tsecurity.de Voice
tsecurity.de Icon
tsecurity.de App
Offline-Lesen, Eilmeldungen & 0ms Ladezeit

Installiere tsecurity.de direkt auf deinen Home-Bildschirm für das ultimative Vollbild-Magazinerlebnis ohne Browser-Leisten.

Nächster Beitrag
Themen-Radar & Intelligence Matrix
Echtzeit-Taxonomie nach Angriffsvektoren & Plattformen

tsecurity.de Live Threat Radar

🔴 LIVE RADAR
MONITORING
AKTIV
CVE-DATENBANK
LIVE
🔍
Community Radar & Live Chat
Sentinel Bot online • Live-Stream
Dein Cluster: Security Explorer
Match:
lädt…
Verbindung zum Community-Stream wird aufgebaut...
Bearbeitungsmodus — Senden überschreibt deine Nachricht
Community-Puls — was gerade passiert
lädt…
Aktivitäten deiner Analysten
lädt…
Neues Thema oder Eilmeldung einreichen

Reiche interessante Links, Zero-Days oder Debatten ein. Die Community entscheidet per Upvote über die Veröffentlichung.

Heiß diskutierte Einreichungen
🔖 Gespeicherte Artikel
📂 Keine gespeicherten Artikel vorhanden.
Zurück Ziehen Vor
Links: vorheriger Artikel Rechts: nächster Artikel unten: schließen
News NIS-2 Frühwarnung Tier-1 Intel ⏱️ 3 Min vor 10 Min
Artikeldaten werden geladen...

Zurück: vorheriger Vor: nächster
↗ Original-Quelle
Social Reaktionen Deine Reaktion zählt
Einstufung & Relevanz-Poll 0 Stimmen
In sozialen Netzwerken teilen 1-Klick