Topic 12: OOPs: creating object templates: way 1 - with function constructor

//Regular Lecture 18: 17 Oct 2022

//Topic 12: //* oops- object oriented programming*// creating object templates // way 1



// Object literal: writing object directly in curly braces is called object literal 

// example:

let info11 = {
    firstName: "rahul",
    lastName: "dravid",
    age: 40
}

console.log(info11)


// creating an object template and using it to create more different objects

// we can create object template in 3 ways
//1) with function constructor
//2) with Es6 class
//3) with object.create()


//1) with function constructor: 

// program example 1

function Person (fn,ln,ag){
    this.firstName = fn
    this.lastName = ln 
    this.age = ag
    }


// we have created a template above
// now create different object using template

let amol = new Person("amol","mane",29)
console.log(amol)

let tukaram = new Person("tukaram","koli",30)
console.log(tukaram)


// program example 2

function Form (fn, ln, ag, ci){
    this.firstName = fn,
    this.lastName = ln,
    this.age = ag,
    this.city = ci
}

// we have created a template above
// now create different object using template

let ram = new Form ("ram", "yadav", 18, "dharashiv")
console.log(ram)

let sham = new Form ("sham", "dixit", 17, "latur")
console.log(sham)

let krishna = new Form ("krishna", "jadhav", 19, "tuljapur")
console.log(krishna)


// program example 3

function Form2 (ful, ci, st, pi){
    this.fullName = ful
    this.city = ci
    this.state = st
    this.pincode = pi
}

// we have created a template above
// now create different object using template

let ramesh = new Form2 ('ramesh joshi', 'pune', 'mh', 400123)
console.log(ramesh)

let umesh = new Form2 ('umesh kamble', 'bhopal', 'mp', 500213)
console.log(umesh)
//------------------------------------------------

टिप्पण्या