Zum Hauptinhalt springen
tsecurity.de LIVE
Echtzeit-Radar & Feeds
Alle RSS Feeds
👥 Community & Social
Web Security TippsIntroducing the new Confluence integration with Google Chat(22.09.2026 um 19:40 Uhr)
Web Security TippsQuick notes in Take notes for me(22.09.2026 um 21:31 Uhr)
Sichere ProgrammierungSecurity improvements for SSH(22.09.2026 um 16:11 Uhr)
Sichere ProgrammierungKI-Akzeptanz: Wie Rewe digital einfach nur den Chatbot umbenannte(22.09.2026 um 18:00 Uhr)
Sichere ProgrammierungClaude Opus 5.5: Keeping safety ahead of capabilities(22.09.2026 um 20:59 Uhr)
Sichere ProgrammierungYour Terraform Monolith Isn't Too Big. It's Tightly Coupled.(22.09.2026 um 21:00 Uhr)
Sichere ProgrammierungMy PR got merged into Mike — OSS Legal AI Platform 🎉(22.09.2026 um 21:34 Uhr)
Sichere ProgrammierungStop Writing JavaScript To Fix `100vh` On Mobile(22.09.2026 um 21:35 Uhr)
Sichere ProgrammierungNext.js proxy.ts Explained (with Cheat Sheet)(22.09.2026 um 21:36 Uhr)
Web Security TippsIntroducing the new Confluence integration with Google Chat(22.09.2026 um 19:40 Uhr)
Web Security TippsQuick notes in Take notes for me(22.09.2026 um 21:31 Uhr)
Sichere ProgrammierungSecurity improvements for SSH(22.09.2026 um 16:11 Uhr)
Sichere ProgrammierungKI-Akzeptanz: Wie Rewe digital einfach nur den Chatbot umbenannte(22.09.2026 um 18:00 Uhr)
Sichere ProgrammierungClaude Opus 5.5: Keeping safety ahead of capabilities(22.09.2026 um 20:59 Uhr)
Sichere ProgrammierungYour Terraform Monolith Isn't Too Big. It's Tightly Coupled.(22.09.2026 um 21:00 Uhr)
Sichere ProgrammierungMy PR got merged into Mike — OSS Legal AI Platform 🎉(22.09.2026 um 21:34 Uhr)
Sichere ProgrammierungStop Writing JavaScript To Fix `100vh` On Mobile(22.09.2026 um 21:35 Uhr)
Sichere ProgrammierungNext.js proxy.ts Explained (with Cheat Sheet)(22.09.2026 um 21:36 Uhr)
Intelligence View
⚡ tsecurity.de Intelligence

JavaScript Constructor Functions

What is a Constructor Function? A Constructor Function is a regular JavaScript function used with the new keyword to create and initialize multiple objects with the same structure. It acts like a blueprint for creating objects. By…

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




What is a Constructor Function?



A Constructor Function is a regular JavaScript function used with the new keyword to create and initialize multiple objects with the same structure.



It acts like a blueprint for creating objects.



By convention, constructor function names start with a capital letter.






Syntax






function ConstructorName(parameter1, parameter2) {
this.property1 = parameter1;
this.property2 = parameter2;
}












Example






function Employee(name, salary) {
this.name = name;
this.salary = salary;
}

const emp1 = new Employee("Saravanan", 50000);

console.log(emp1);









Output






Employee {
name: "Saravanan",
salary: 50000
}












How the new Keyword Works



When JavaScript executes:




const emp1 = new Employee("Saravanan", 50000);






it performs the following steps automatically:






Step 1: Creates an Empty Object






{}









Step 2: Sets this to the New Object






this = {}









Step 3: Executes the Constructor Function






this.name = "Saravanan";
this.salary = 50000;






Result:




{
name: "Saravanan",
salary: 50000
}









Step 4: Returns the Object Automatically






return this;












Understanding this



Inside a constructor function, this refers to the newly created object.




function Employee(name) {
this.name = name;
}

const emp1 = new Employee("Saravanan");






Internally:




emp1.name = "Saravanan";






The property name is on the left side and the value comes from the parameter on the right side.




this.name = name;












Creating Multiple Objects






function Employee(name, salary) {
this.name = name;
this.salary = salary;
}

const emp1 = new Employee("Ram", 50000);
const emp2 = new Employee("Kumar", 60000);
const emp3 = new Employee("Ajay", 70000);






All objects follow the same structure.









Adding Methods



Constructor functions can also contain methods.




function Employee(name, salary) {
this.name = name;
this.salary = salary;

this.displayInfo = function() {
console.log(this.name + " earns " + this.salary);
};
}

const emp1 = new Employee("Saravanan", 50000);

emp1.displayInfo();









Output






Saravanan earns 50000












CRUD Operations on Constructor Function Objects



Objects created using constructor functions behave like normal JavaScript objects.






Create






emp1.age = 26;









Read






console.log(emp1.age);









Update






emp1.age = 27;









Delete






delete emp1.age;












Adding Properties to One Object






emp1.department = "IT";






Only emp1 receives the property.









Adding Properties for All Objects Using Prototype






Employee.prototype.company = "TCS";






Now all Employee objects can access the property.




console.log(emp1.company);
console.log(emp2.company);









Output






TCS
TCS












Adding Shared Methods Using Prototype






Employee.prototype.greet = function() {
console.log("Hello " + this.name);
};






Now every Employee object can use the same method.




emp1.greet();
emp2.greet();






This approach is more memory-efficient because only one copy of the function exists.









Constructor Function vs Object Literal






Object Literal






const employee = {
name: "Ram",
salary: 50000
};






Best when creating a single object.






Constructor Function






function Employee(name, salary) {
this.name = name;
this.salary = salary;
}






Best when creating multiple objects with the same structure.






References:

https://www.w3schools.com/js/js_object_constructors.asp

https://www.geeksforgeeks.org/javascript/javascript-function-constructor/

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/Function

Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten JavaScript Constructor Functions

Thematisch verwandte Begriffe: JavaScript, Constructor, Functions · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

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-77259 | MCP Atlassian is a Model Context Protocol (MCP) server for Atlassian pro…
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