Javascript Basics for Non-Developer Types pt. 5/5

Part 1 of 5, part 2 of 5, part 3 of 5, part 4 of 5.

Part 4 covered objects and methods.

Useful Operators

To identify what type of variable (number, string, object, etc.), typeof:

var apple = {color: red};

console.log( typeof apple ); will return object. 🍎

To find out if an object has a certain property, hasOwnProperty:

var cheese = { type: "cheddar" }

console.log(cheese.hasOwnProperty('type')); will return true. 🧀

For Loops, Again

Using for/in, all elements of an object can be printed out.

var cheeseBurger = { condiments: "mustard", bread: "sesame seed bun", patties: 3 };

for (var property in cheeseBurger) { console.log(property); };

Condiments, bread, and patties should print out. 🍔

Using for loops can also print properties of an object. For the cheeseBurger object above:

for (var x in cheeseBurger) { console.log(cheeseburger[x]); }

Will print out mustard, sesame seed bun, and 3.

OOP (Object Oriented Programming) Stuffs

Add prototype so all members of a class can use a method:

cheeseBurger.prototype.order = function() { console.log("Medium rare"); };

This way order method will apply to all new cheeseburgers. 🍔🍔🍔

To make a variable private, define with var:

var cheeseBurger = { condiments: "mustard", bread: "sesame seed bun", patties: 3 var secretSauce = "sriracha mayo" };