Quantcast
Channel: 2D Latest Topics
Viewing all articles
Browse latest Browse all 623

confused on objects

$
0
0

Hi All

Im trying to learn to build Pong whilst learning JS at the same time.

I'm learning in pure js with no libraries for frameworks etc as i want to learn the JS first but im a little confused.

in other languages with OOP ive used classes and it may look like this:

class Paddle{
  constructor(x,y,w,h){
    this.x = x;
    this.y = y;
    this.width = w;
    this.height = h;
  }
}

player = new.Paddle(50,100,30,100);

This i understand

and i also understand the "i assume old" way however they created a function.

is this similar to a constructor? 

to me it looks like the function is creating an object called p of which its properties come from the create function.. so im wondering.. do the inital x y width and height need to be there?

 

let Paddle = {
  x:0,
  y:0,
  width:0,
  height:0,

  create: function(x, y, width, height) {
        var p = Object.create(this);
            p._x = x;
            p._y = y;
            p._width = width;
            p._height = height;
        return p;
    }
}

var playerPaddle = paddle.create(0, 0, 10, 100);

 

could it be like this?

let Paddle = {

  create: function(x, y, width, height) {
        var p = Object.create(this);
            p._x = x;
            p._y = y;
            p._width = width;
            p._height = height;
        return p;
    }
}

var playerPaddle = paddle.create(0, 0, 10, 100);

OR

is this line,  var p = Object.create(this);, giving the properties to the "p" ??


Viewing all articles
Browse latest Browse all 623

Trending Articles