Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Object Orientated Javascript. Am I doing this right?

Featured Replies

I've recently started teaching myself Javascript. I'd thought I'd see how object orientated prorgamming is handled in Javascript and as a result have written up this simple player creation and am just wondering whether I'm approaching this correctly?

                       /**
			* Player Constructor
			*/
			function Player() {
				this.username;
				this.password;
			}

			/**
			* Player Username Setter
			*
			* @[member="param"] username
			*        The Username to be set
			*/
			Player.prototype.setPlayerUsername = function(username) {

				// Check undefinied
				if(typeof username != 'undefined') {

					// Set Player Username
					this.username = username;
				} else {
					console.log("Please enter a valid parameter...");
				}
			}

			/**
			* Player Password Setter
			*
			* @[member="param"] password
			*        The Password to be set
			*/
			Player.prototype.setPlayerPassword = function(password) {

				// Check undefined
				if(typeof password != 'undefined') {

					// Set Player Password
					this.password = password;

				} else {
					console.log("Please entier a valid parameter...")
				}
			}

			/**
			* Player Username Getter
			*
			* @return The Current Player's Username
			*/ 
			Player.prototype.getPlayerUsername = function() {
				return this.username;
			}

			/**
			* Player Password Getter
			*
			* @return The Current Player's Password
			*/
			Player.prototype.getPlayerPassword = function() {
				return this.password;
			}

			/**
			* Create new Player
			*
			* @[member="param"] username 
			*        The Player's Username
			* @[member="param"] password
			*        The Player's Password
			*/
			Player.prototype.create = function(username, password) {
				this.password = password;
				this.username = username;

				// new Player Object
				var player_to_create = new Player();
				player_to_create.setPlayerUsername = username;
				player_to_create.setPlayerPassword = password;

				// log output
				console.log("Player Created: " + this.username + " " + this.password + ".");

				// save
			}

			Player.prototype.create(prompt("Enter a desired Username..."), prompt("Enter a desired Password...")); 

Thank you for any feedback.

Edited by Responsive Designs

That looks right for prototyping.

 

Although that said There's a lot of movement now away from simulating the 'classical' structure in JS seen as JS does not have classes, so in a sense there's no read need for constructor functions like the Player() constructor you have at the top of your code.

 

Tutorials on this way of thinking are hard to find because JavaScript massively misunderstood which means most tutorials teach people to simulate classes in JavaScript which is totally unecessary.

 

Read the book series 'You Don't Know JS' by Kyle Simpson he explains it there in great detail.

 

 

Note ES6 is introducing classes but this is a bad thing, it's down to lazy, misinformed developers pushing for a classical method. People like Crockford, Kyle Simpson, other well known JS evangelists are advising to stay away from them.

Edited by rbrtsmith

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.