K
K
Kirill Gostiev2016-08-27 20:47:54
JavaScript
Kirill Gostiev, 2016-08-27 20:47:54

How to create a constructor method?

Hello! I am writing a small game based on MVC. Got a question. How to create an object of some element of the world inside the Model property. And how to call it correctly so that the resulting object is a Model object. I do that, but the object is not added.

var model = {
    CreateGameObject: function(elem) {
      var gameArea = this.area;
      this.elName = elem; 
      this.makeDiv = function() {
        var parentElem = document.getElementById("game-area");
        console.log(parentElem);
        var div = document.createElement("div");
        div.setAttribute("id", elem);
        div.className = "wall";
        parentElem.appendChild(div)
      }
    },
    mainMethod: function(){
    var obj1 = new this.CreateGameObject("obj1"); 
    }
};

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladislav, 2016-08-27
@MrBikus

function model() {
}
var myModel = new model();
console log(myModel);
Then myModel will be an instance of model.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question