//Literal Notation var bob = {firstName: "Bob", lastName: "Jones"}; //Constructor Notation var bob = new Object(); bob.firstName = "Bob"; bob.lastName = "Jones"; //Dot notation someObj.propName //Bracket Notation someObj["propName"]; /*Bracket Notation allows you to use a variable as an Object property. For example: obj.job is a regular object property. If you want the job in the property to refer to a variable, then you must write obj[job]. To write obj.job in bracket notation you would have to write obj["job"]. You can also use characters that are not valid in javascript names. For example, obj.42 is invalid because you cannot begin a name with a number. obj[42] is valid because it automatically converts to a string */ //Method function person(first, last){ this.firstName = first; this.lastName = last; } //Custom Constructor Notation function = new Person(firstName, lastName){ this.firstName = firstName; this.lastName = lastName; } var bob = new Person("Bob", "Jones"); //Methods Associated with Objects function someObject(){ this.someMethod = function(){ }; } //See example of above function Person(job, married) { this.job = job; this.married = married; // add a "speak" method to Person! this.speak = function(){ console.log("Hello!"); } } var user = new Person("Codecademy Student",false); user.speak(); //See Another Example var james = { job: "programmer", married: false, sayJob: function() { // complete this method console.log("Hi, I work as a "+ this.job); } }; // james' first job james.sayJob(); // change james' job to "super programmer" here james.job = "super programmer"; // james' second job james.sayJob(); // Determining Object Properties var suitcase = { shirt: "Hawaiian" }; if(suitcase.hasOwnProperty("shorts") == true){ console.log(suitcase.shorts); } else{ suitcase.shorts = "tan"; console.log(suitcase.shorts); } //Type of var anObj = { job: "I'm an object!" }; var aNumber = 42; var aString = "I'm a string!"; console.log( typeof anObj ); // should print "object" console.log( typeof aNumber ); // should print "number" console.log( typeof aString ); // should print "string" //Printing all properties in an Object Using the "For In" Loop var nyc = { fullName: "New York City", mayor: "Bill de Blasio", population: 8000000, boroughs: 5 }; for(var property in nyc){ console.log(property); */For In Loop explained An object has one or more properties seperated by a comma-, Each property consists of a property-key and it's associated VALUE var nyc = { fullName: "New York City", mayor: "Bill de Blasio", population: 8000000, boroughs: 5 }; nyc -object- S p e c i f i e d The nyc object has 4 properties seperated by a comma-, a fullName property with property-key fullName and it's associated string value of "New York City" a mayor property with property-key mayor and it's associated string value of "Bill de Blasio" a population property with property-key population and it's associated number value 8000000 a boroughs property with property-key boroughs and it's associated number value 5 for - in - loop - e x p l a i n e d With the for-in-loop you have a Method which will iterate over all properties of a given object. At each iteration it will assign the property-key as a string to a variable name of your choice. Thus for (var x in nyc) will lead to 4 iteration's iteration-1 var x = "fullName"; iteration-2 var x = "mayor"; iteration-3 var x = "population"; iteration-4 var x = "boroughs"; As they want you to display the property-key and NOT it's associated VALUE you would use console.log( x ); If they wanted you to display the associated VALUE of the propery-key you would use console.log( nyc[x] ); */ //Adding Methods or Properties to a Constructor Object using Prototype function Dog (breed) { this.breed = breed; }; // here we make buddy and teach him how to bark var buddy = new Dog("golden Retriever"); Dog.prototype.bark = function() { console.log("Woof"); }; buddy.bark(); // here we make snoopy var snoopy = new Dog("Beagle"); /// this time it works! snoopy.bark(); //Inheriting class properties // the original Animal class and sayName method function Animal(name, numLegs) { this.name = name; this.numLegs = numLegs; } Animal.prototype.sayName = function() { console.log("Hi my name is " + this.name); }; // define a Penguin class function Penguin(name){ this.name = name; this.numLegs = 2; } // set its prototype to be a new instance of Animal Penguin.prototype = new Animal(); /* Private variables inside Object Constructors. Use the var inside the Object Constructor. The add a method to make the variable visible again.*/ function Person(first,last,age) { this.firstname = first; this.lastname = last; this.age = age; var bankBalance = 7500; this.getBalance = function() { // your code should return the bankBalance return(bankBalance); }; } var john = new Person('John','Smith',30); console.log(john.bankBalance); // create a new variable myBalance that calls getBalance() var myBalance = john.getBalance(); console.log(myBalance);