JavaScript Essential Training

LinkedIn Learning : Morten Rand-Hendriksen

Code Samples

Object Cosnstructors

This is the code for creating an object using the object constructor function.

	/**
	 * Create an object constructor function for the Backpack object type.
	 * @link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/new
	 */

	function Backpack(
	  name,
	  volume,
	  color,
	  pocketNum,
	  strapLengthL,
	  strapLengthR,
	  lidOpen
	) {
	  this.name = name;
	  this.volume = volume;
	  this.color = color;
	  this.pocketNum = pocketNum;
	  this.strapLength = {
		left: strapLengthL,
		right: strapLengthR,
	  };
	  this.lidOpen = lidOpen;
	  this.toggleLid = function (lidStatus) {
		this.lidOpen = lidStatus;
	  };
	  this.newStrapLength = function (lengthLeft, lengthRight) {
		this.strapLength.left = lengthLeft;
		this.strapLength.right = lengthRight;
	  };
	}

	const everydayPack = new Backpack(
	  "Everyday Backpack",
	  30,
	  "grey",
	  15,
	  26,
	  26,
	  false
	);