Creating Objects in JavaScript

 Objects can be created in two ways.

  1. Factory Function
  2. Constructor Function


// two ways to create object
// Factory function
function createCircle(radius) {
return {
radius,
draw: function() {
console.log('draw', radius);
}
};
}
const circle = createCircle(5);
circle.draw();

// Constructor function
function Circle(radius) {
this.radius = radius;
this.draw = function() {
console.log('draw', this.radius);
}
}

const obj = new Circle(3);
obj.draw();


Comments

Popular posts from this blog

Transform values with a stream

Collections Framework

Inspect a collection