Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript Am I doing it right? [Prototype]

  • This topic is empty.
Viewing 1 post (of 1 total)
  • Author
    Posts
  • #166345
    javascriptnewbie
    Participant

    Alright, so. I’ve 2 bunch of code below.

    First: It doesn’t use prototype but simple adds method to the main class.

    Second: Uses prototype.

    Here is my question, the reason to use prototype is so that each instance doesn’t take extra space? Just like in the second one.

    And am I right saying that if I don’t use prototype each instances will take a whole new space in memory? Just like in the first one

    First:
    function flower(){
    this.color = “blue”;
    this.type = “mud”;
    this.weight = function weight(height, length){
    return height * length;
    }
    }

    lotus = new flower;
    rose = new flower;
    
    lotus.weight(10, 28);
    rose.weight(11, 908);
    

    Second:
    function flower(){
    this.color = “blue”;
    this.type = “mud”;
    }

    flower.prototype.weight = function weight(height, length){
            return height * length;
        }
    
    lotus = new flower;
    rose = new flower;
    
    rose.weight(10, 28);
    lotus.weight(20, 60)
    

    I’m not even sure if I was even to explain what I exactly want.. :facepalm:

    thanks

Viewing 1 post (of 1 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.