Code Monkey home page Code Monkey logo

prototype-basic's Introduction

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);

prototype-basic's People

Contributors

abulbashar38 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.