0

first, this is my code:

var rettangolo = {
        rectX_top: 0,
        recX_bottom: 0,
        rectY_top: 0,
        rectY_top: 0,
        colpito: false,
        colore: 'hsl(' + 360 * Math.random() + ', 50%, 50%)',

        rettangolo: function() {
        this.setRectX_top(0);
        this.setRectX_bottom(0);
        this.setRectY_top(0);
        this.setRectY_top(0);
        this.setColpito(false);
        this.setColore('red');
        },

        setColpito: function(colpito) {
            this.colpito = colpito;
        },

        setColore: function(colore) {
            this.colore = colore;
        },

        setRectY_top: function(top) {
            rectY_top = top;
        },

        setRectX_top: function(top) {
            rectX_top = top;
        },

        setRectX_bottom: function(bottom) {
            rectX_bottom = bottom;
        },

        setRectY_bottom: function(bottom) {
            rectY_bottom = bottom;
        },

        <!--        GET METHODS     -->

        getColore: function() {
            return this.colore;
        },

        getRectY_top: function() {
            return this.rectY_top;
        },

        getColpito: function() {
            return this.colpito;
        },

        getRectX_top: function() {
            return this.rectX_top;
        },

        getRectX_bottom: function() {
            return this.rectX_bottom;
        },

        getRectY_bottom: function() {
            return this.rectY_bottom;
        },



    }

This is my class, and now I need to create multiple instances of this class. Is it possible? And if it is, how do you achieve it? (Plus, is it correct? notice that this is the first class I create as they never explained it at school). Thank you!

Baffo rasta
  • 320
  • 1
  • 4
  • 17

1 Answers1

1

You need to make a constructor or a factory function.

Read this https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects

Constructor function vs Factory functions

There are many ways to achieve this this is one of them. Obviously you can extend it to fit your case.

var object = function(options) {

    var object = {
        id: options.id,
        property: 'value'
    };

    return object;
};

var a = object({id:1});
var b = object({id:2});

console.log(a); 
console.log(b); 

This illustrates that you can create two objects from the same constructor obvious by that both objects have the same value in the property attribute and a different id dependant on the id option passed.

Additionally the line comment:

<!--        GET METHODS     -->

Will throw an error, in Javascript use either // or /* */ for comments.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar

Community
  • 1
  • 1
Daniel Tate
  • 2,075
  • 4
  • 24
  • 50
  • 1
    You should not use the `new` keyword with a factory like this. `var a = object({id:1})` is all you need to create an instance using the function you defined. Only use `new` when the function is a constructor. See [here](http://stackoverflow.com/questions/8698726/constructor-function-vs-factory-functions) – jshanley Dec 02 '15 at 22:38
  • I managed to do exactly what I wanted thanks to you, you made my day. Thank you – Baffo rasta Dec 02 '15 at 22:49