0

i`m trying to creat a little game using javascript and everything went well until i have to fire something, i nedeed to instantiate a "bullet" and fire it, but when calling "new bullet()" i got an "Uncaught TypeError: undefined is not a function ". How do i instantiate an object inside another object method?

this is what i done

function bullet(){
    //here it would be state for the bullet, like x and y and thigs
    console.log("bullet created");
}

function gun(){
    //state for the gun, amount of bullets and sort
    console.log("gun created");

    this.fire = function(){
        //here we instantiate a bullet and fire it
        console.log("begin fire");
        var bullet = new bullet();
        console.log("created bullet to fire");
    }
}

var gun = new gun();

gun.fire();
1
  • 1
    rename var gun and var bullet to something else... Commented Nov 28, 2013 at 21:50

2 Answers 2

2

in javascript your variables get hoisted. To be clear what that means, the compiler treats the code you wrote as if you had written:

function gun(){
    //state for the gun, amount of bullets and sort
    console.log("gun created");
    this.fire = function(){
        var bullet;
        //here we instantiate a bullet and fire it
        console.log("begin fire");
        bullet = new bullet();
        console.log("created bullet to fire");
    }
}

So in JS all variable declarations get moved to the top of the current function scope. Note, that this isn't true in all programming languages. In some languages you could get away with the above and bullet the variable would succesfully replace bullet the function.

The best solutions is to give your variable a different name.

function gun(){
    //state for the gun, amount of bullets and sort
    console.log("gun created");
    this.fire = function(){
        //here we instantiate a bullet and fire it
        console.log("begin fire");
        var mybullet = new bullet();
        console.log("created bullet to fire");
    }
}

Also, as brought up by hungarian, I think you are misunderstanding the new operator in JS.

Edit: clarified meaning of hoisting.

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

11 Comments

error: bullet is not a constructor but if you change the var bullet to bullets, then it works jsfiddle.net/f962Y
yes, that is also a problem, but it's not the problem he was having. Besides calling the function with new would just set this equal to a new instance of an object with its constructor pointed to the function but it would still "work". go ahead and try it. JS doesn't crash and burn when you call a function with new, it just isn;t doing exactly what you might be expecting.
@Huangism: I think you misunderstood the point of this answer. The code is a demonstration/explanation of the problem, not a solution. The solution is in the final sentence: "give it a different name."
@Obtuse: I don't think that was Huangism's intention. I think he actually run the code and got the error bullet is not a constructor (that's the error Firefox shows), under the assumption that the code you posted is the solution.
I am just saying, if an answer is posted with code, then the code should be working. It's updated now so it's good to go
|
0

Here's what's happening.

When you call gun.fire();, the local variable bulletis declared and initialized to 'undefined' while the rest of the expression is evaluated. The evaluation of this expression looks for a variable called bullet, and finds it in the local scope, ignoring the bullet variable in the global scope.

This taught me something new about javascript, variable declarations aren't atomical.

To solve the problem, rename the bullet variable in your fire method, to something like firedBullet.

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.