Skip to content

we can know what is prototype and why we need to prototype from this repo.

Notifications You must be signed in to change notification settings

AbulBashar38/prototype-basic

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 

Repository files navigation

prototype-basic

মনে করি আমরা একটা person এর অব্জেক্ট তৈরি করবো যার কিছু প্রপার্টি আর মেথড থাকবে

const person = {};
person.name = 'Abul Basar';
person.age = 20;
person.eat= function (){
    console.log('person is eating')
}
person.sleep = function(){
    console.log('person is sleeping')
}

এখন আমরা যদি এই person object কে বার বার use করতে চাই তাহলে এই পুরো object কে একটা function এর মধ্যে রেখে বার বার call করতে পারবো।

function Person(name, age) {
    const person = {};
    person.name = name;
    person.age = age;
    person.eat = function () {
        console.log('person is eating')
    }
    person.sleep = function () {
        console.log('person is sleeping')
    }
    return person;
}
const sakib = Person('sakib',34)

const tamim = Person('tamim',32)

এখন আমরা যত জন ব্যাক্তির জন্য Person function কে call করবো ততটা person নামের object তৈরি হবে তার প্রপার্টি হিসাবে ততটা name,age,eat,sleep তৈরি হবে। এখনে eat এবং sleep property একই object বার বার তৈরি হচ্ছে। আমরা eat এবং sleep প্রপার্টি function গুলাকে Person এর বাইরে একটা object এর ভিতরে eat() এবং sleep() method তৈরি করে ব্যবহার করতে পারি। এতে eat আর sleep বার বার object তৈরি করছে নাহ একবার তৈরি করা object কে বার বার use করতে পারছে।

const personShareMethod = {
    eat() {
        console.log('person is eating')
    },
    sleep() {
        console.log('person is sleeping')
    }
}
function Person(name, age) {
    const person = {};
    person.name = name;
    person.age = age;
    person.eat = personShareMethod.eat;
    person.sleep = personShareMethod.sleep;
    return person;
}
const sakib = Person('sakib',34)

const tamim = Person('tamim',32)

এখন আমরা যদি personShareMethod এ কোনো method add করি তাহলে Person এর ভিতরেও অই method apply হবে।

const personShareMethod = {
    eat() {
        console.log('person is eating')
    },
    sleep() {
        console.log('person is sleeping')
    }
    play(){
        console.log('person is playing')
    }
    chill(){
        console.log('person is Chilling')
    }
}
function Person(name, age) {
    const person = {};
    person.name = name;
    person.age = age;
    person.eat = personShareMethod.eat;
    person.sleep = personShareMethod.sleep;
    person.play = personShareMethod.play;
    person.chill = personShareMethod.chill;
    return person;
}
const sakib = Person('sakib',34)

const tamim = Person('tamim',32)

তাহলে এইটা ও একটু সমস্যা কারন আমাদের দুই জায়গায় ই কোড লিখতে হচ্ছে আমরা চাইলে এইটাকে ও একটু সহজ ভাবে লিখতে পারি person এর মধ্যে Object.create() apply করে। তাহলে personShareMethod parent object এর মধ্যে person নামের object create হচ্ছে। এই person object personShareMethod অব্জেক্ট এর সব প্রপার্টি access করতে পারবে।

const personShareMethod = {
    eat() {
        console.log('person is eating')
    },
    sleep() {
        console.log('person is sleeping')
    },
    play(){
        console.log('person is playing')
    },
    chill(){
        console.log('person is Chilling')
    }
}
function Person(name, age) {
    const person = Object.create(personShareMethod);
    person.name = name;
    person.age = age;
    return person;
}
const sakib = Person('sakib',34)
sakib.eat() // person is eating
const tamim = Person('tamim',32)

এখানে আমরা method গুলো personShareMethod এ না রেখে Person এর prototype এর মধ্যে রেখে দিতে পারি। তাহলে personShareMethod এর মতো বাইরে কোনো object create করতে হচ্ছে নাহ।

Prototype হলো javascript এর যেকোনো একটা function এর প্রপার্টি যেটা একটা object কে point করে।

function Person(name, age) {
    const person = Object.create(Person.prototype);
    person.name = name;
    person.age = age;
    return person;
}
Person.prototype={
    eat() {
        console.log('person is eating')
    },
    sleep() {
        console.log('person is sleeping')
    },
    play(){
        console.log('person is playing')
    },
    chill(){
        console.log('person is Chilling')
    }
}
const sakib = Person('sakib',34)
sakib.eat() // person is eating
const tamim = Person('tamim',32)

এখন আমরা যদি const sakib = Person('sakib',34) কে const sakib = new Person('sakib',34) করে লেখি তখন const person = Object.create(Person.prototype); এবং return person; লেখার দরকার হয় নাহ। javascript তা নিজে নিজে লিখে নেয়। যেহেতু আমরা কোনো person নামে অব্জেক্ট বানাচ্ছি নাহ javascript বানাচ্ছে তাই javascript this নামের object বানায়।

function Person(name, age) {
    //const this = Object.create(Person.prototype)
    this.name = name;
    this.age = age;
   // return this;
}
Person.prototype={
    eat() {
        console.log('person is eating')
    },
    sleep() {
        console.log('person is sleeping')
    }
}
const kuddos = new Person('kuddus', 20);
kuddos.sleep()
const kobir = new Person('kobir', 40);

এই concept থেকে আমরা class টা বুঝতে পারি। class এর মাধ্যমে আমরা এই পুরা কোডটা কে আমরা খুব সহজে লিখতে পারি।

class Person {
    constructor(name, age) {
        this.name = name;
        this.age = age;
    };
    eat() {
        console.log('person is eating')
    };
    sleep() {
        console.log('person is sleeping')
    }
}
const kuddos = new Person('kuddus', 20);
kuddos.sleep()
const kobir = new Person('kobir', 40);

About

we can know what is prototype and why we need to prototype from this repo.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published