0

I cannot figure out how to create several objects from the main object

Here is what i am trying to do:

var player = {
    var totalScore = 0;
    var playing = true;
    function checkScore() {
       if (totalScore >= 100) {
            playing = false;
       }
    };
};

player playerAI = new player();
player playerOne = new player();
7
  • 3
    Objects aren't function. Commented Sep 28, 2014 at 16:47
  • I've edited the code to make it easier to read Commented Sep 28, 2014 at 16:50
  • player is already an object. its not a template for creating new objects. in javascript functions play the role of being a template through which you can create new objects. Commented Sep 28, 2014 at 16:50
  • eloquentjavascript.net/06_object.html Commented Sep 28, 2014 at 16:51
  • I'm thinking of it as a class from java(no reference), this is why objects confuse me so much. So if i want to have few objects with same variables but different values, i have to simply write two separate objects? Commented Sep 28, 2014 at 16:52

4 Answers 4

2

I've re-written your code as a Constructor in JavaScript

function Player() {
    this.totalScore = 0; // `this` will refer to the instance
    this.playing = true;
}
Player.prototype.checkScore = function () { // the prototype is for shared methods
    if (this.totalScore >= 100) {           // that every instance will inherit
        this.playing = false;
    }
};

var playerAI = new Player(),
    playerOne  = new Player();

Some of your code patterns don't look like JavaScript though, are you sure you're not using a different language? (such as Java)

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

2 Comments

Yeah i'm thinking of java when i write code but then i keep reminding myself its javascript, hard to find mistakes now since eclipse luna doesn't validate javascript :(
@Evald Well, go out and find yourself one of the dozens of competent JavaScript IDEs if you actually are serious about programming in JavaScript.
0

try this :

var player = function() {
    this.totalScore = 0;
    this.checkScore = function() {
        if (totalScore >= 100) {
        playing = false;
    }    
};
player playerAI = new player();
player playerOne = new player();

Comments

0

I'm not sure if this is what you want:

function player() {
    var totalScore = 0;
    var playing = true;
    return {
        checkScore: function checkScore() {
            if (totalScore >= 100) {
                playing = false;
            }
        }
    }
};

var playerAI = new player();
var playerOne = new player();

Comments

0
// Define a constructor function
var Player = function () {

    // This acts as public property
    this.playing = true;

    // This acts as public method
    this.isPlaying = function (){
        return this.playing;
    };
};

Usage:

var player1 = new Player();
console.log(player1.isPlaying());

Note: It's better to declare your methods as properties of the Player.prototype object. (For memory saving)

Player.protoype.isPlaying = function (){
    return this.playing;
};

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.