0

I'm new to the prototype structure, and I'm having trouble figuring this one out. Here's my JavaScript code.

var Game = function ()
{
   //some variables
};
Game.prototype.block = 
{
    spawn: function () {
        var t1 = new this.inst;
    },
    inst : {
        x: 5,
        y: 0,
        type: ''
    }
};

When I try to create a new object "inst" I get the following error: TypeError: object is not a function. What am I doing incorrectly?

7
  • 1
    Your code is unusual. How are you creating a new inst object? The only inst I see is already an object on Game.prototype.block. The new this.inst will never work because inst is not a function. Commented Oct 20, 2012 at 2:36
  • Assume I'm a complete noob to most JavaScript and prototypes. How should I restructure my code so that it makes sense? Commented Oct 20, 2012 at 2:46
  • What do you want to ultimately accomplish? Commented Oct 20, 2012 at 2:47
  • This is a brief excerpt of a lot of code. I'm calling spawn in a "setInterval" which I want to create a new instance of the "inst object" and manipulate its properties. Commented Oct 20, 2012 at 2:50
  • 1
    If you want to create objects that inherit from the inst object, you can do that using Object.create, so you could do var t1 = Object.create(this.inst);, assuming you're doing something like var game = new Game(); game.block.spawn(); Commented Oct 20, 2012 at 2:57

3 Answers 3

1

If you want to create objects that inherit from the inst object, you can do that using Object.create, with var t1 = Object.create(this.inst);.

var Game = function () {
   //some variables
};
Game.prototype.block =  {
    spawn: function () {
        var t1 = Object.create(this.inst);
    },
    inst : {
        x: 5,
        y: 0,
        type: ''
    }
};

So then your code would look something like this;

var game = new Game();

game.block.spawn();

And the .spawn() method would have a variable that references an object that inherits from the Game.prototype.block.inst object.

Sign up to request clarification or add additional context in comments.

Comments

0

First of all, inst is not defined within the scope of Game. So, this which refers to Game doesn't have any properties called inst. Secondly, inst must be followed by () to indicate a call to the constructor, which you are missing here.

3 Comments

You don't need () to invoke a function when you use new, but I can't see how OP's this could refer to Game. At best it may refer to Game.prototype.block.
This is because Game.prototype.block is not an instance property, but rather a Singleton object already instantiated. I might be wrong though.
How would use nest functions via prototypes and still have them accessible in Game's scope?
0

I guest you need a static factory method to create new "inst". is the below code what you need? you call the Game.spawn method to generate a new inst, and you can put this method in setInterval.

function Game() {
    //some variables
}

Game.spawn = function() {
    function Inst() {
        this.x = 5;
        this.y = 0;
        this.type = '';
    }

    return new Inst;
}

var inst1 = Game.spawn();
inst1.x = 1; //test inst1
console.log(inst1.x);
var inst2 = Game.spawn();
inst2.x = 2; //test inst2
console.log(inst2.x);
var inst3 = Game.spawn();
inst3.x = 3; //test inst 3
console.log(inst3.x);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.