Wednesday, July 25, 2018

Factory & Constructor Function in javascript.

//Camel Notation :: oneTwoThreeFour
//Pascal Notation :: OneTwoThreeFour

//Factory Function
//use camel notation for construction function it's best practice
function createCircle(radius){
    return {
       /* radius:radius
       draw: function(){
           console.log("draw logic");
       }*/
       radius,
       draw(){
           console.log("draw");
       }
    }
}
var circle1= createCircle(10);
console.log(circle1);

//Constructor Function
//use pascal notation for construction function it's best practice
function CreateCircle(radius){
    this.radius=radius;
    this.draw=function(){
        console.log("draw logic go here !!");
    }
}

const circle1=new CreateCircle(2);
console.log(circle1);

No comments:

Post a Comment

Find the value from array when age is more than 30

 const data = [   { id: 1, name: 'Alice', age: 25 },   { id: 2, name: 'Bob', age: 30 },   { id: 3, name: 'Charlie', ...