nibzAU Posted October 23, 2019 Share Posted October 23, 2019 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" ?? Quote Link to comment Share on other sites More sharing options...
grelf Posted October 23, 2019 Share Posted October 23, 2019 Yes, JavaScript handles objects in a very different way from most languages. It does not have explicit classes, instead you create "prototypes". However, it is a truly object oriented language and using objects in your programs is a good way to build a maintainable and extensible structure. I emphasise this in my free JavaScript course - start here: https://www.grelf.net/jscourse/ I introduce objects right at the beginning of the course but the full syntax is explained in Part 2. First you need to get the syntax for functions clear, because your example code is not right. I am keen to encourage others to explore the creative possibilities of pure HTML5/JavaScript before going on to consider frameworks and other libraries. Here is my biggest demonstrator of what can be done in the basic 2D graphics context: https://www.myforest.uk/ From there you can find a page about how the infinite terrain, contour maps and photographic scenes are programmed in plain JS. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.