Skip to content

Files

Latest commit

 

History

History
29 lines (20 loc) · 721 Bytes

new-cap.md

File metadata and controls

29 lines (20 loc) · 721 Bytes

Pattern: Constructor function without capital letter

Issue: -

Description

Constructor functions create new instances of objects when called with the new operator. By convention, constructor functions begin with a capital letter to distinguish them from regular functions. This naming convention helps developers quickly identify which functions are meant to be used with new.

Examples

Example of incorrect code:

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

const user = new person('John');

const colleague = Person();  // calling constructor without new

Example of correct code:

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

const user = new Person('John');