Tumgik
Text
private methods - class + object
function Person(first,last,age) {   this.firstname = first;   this.lastname = last;   this.age = age;   var bankBalance = 7500;
  var returnBalance = function() {      return bankBalance;   };
  // create the new function here   this.askTeller = function(){       return returnBalance;   }; }
var john = new Person('John','Smith',30); console.log(john.returnBalance); var myBalanceMethod = john.askTeller(); var myBalance = myBalanceMethod(); console.log(myBalance);
0 notes
Text
Printing object property and values using for loop
var nyc = {    fullName: "New York City",    mayor: "Bill de Blasio",    population: 8000000,    boroughs: 5 };
for(var property in nyc){    console.log(property + " " + nyc[property]); }
0 notes
Text
hasOwnProperty
var suitcase = {    shirt: "Hawaiian" };
if(suitcase.hasOwnProperty('shorts') === true){    console.log(suitcase.shorts); } else{    suitcase.shorts = "blue and white";    console.log(suitcase.shorts); }
typeof propertyName
0 notes
Text
methods and calls
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();
0 notes
Text
Add a friend object into an array
var bob = {    firstName: "Bob",    lastName: "Jones",    phoneNumber: "(650) 777-7777",    email: "[email protected]" };
var mary = {    firstName: "Mary",    lastName: "Johnson",    phoneNumber: "(650) 888-8888",    email: "[email protected]" };
var contacts = [bob, mary];
function printPerson(person) {    console.log(person.firstName + " " + person.lastName); }
function list() { var contactsLength = contacts.length; for (var i = 0; i < contactsLength; i++) { printPerson(contacts[i]); } }
/*Create a search function then call it passing "Jones"*/
var add = function(firstName, lastName, email, phoneNumber){        var friend = {            firstName: firstName,            lastName : lastName,            email: email,            phoneNumber: phoneNumber        };
       contacts[contacts.length] = friend; };
add("Dhuha", "Alamiri", "[email protected]", "902-999-4257");
list();
0 notes
Text
Custom Constructors
we can define our own custom constructors. These are helpful for two reasons:
We can assign our objects properties through parameters we pass in when the object is created.
We can give our objects methods automatically.
These both work to save us time and lines of code when we make objects.
0 notes
Text
Methods
Methods are like functions that are associated with a particular object.
They are especially helpful when you want to either:
Update the object properties
Calculate something based on an object's properties.
0 notes
Text
Cool from Codecademy
// Our Person constructor function Person(name, age){    this.name = name;    this.age = age; }
// Now we can make an array of people var family = new Array(); family[0] = new Person("alice", 40); family[1] = new Person("bob", 42); family[2] = new Person("michelle", 8); family[3] = new Person("timmy", 6);
// loop through our new array for(var i=0; i<family.length; i++){    console.log(family[i].name); }
0 notes
Text
Derived from Codecademy
function Rabbit(adjective) {    this.adjective = adjective;    this.describeMyself = function() {        console.log("I am a " + this.adjective + " rabbit");    }; }
// now we can easily make all of our rabbits var rabbit1 = new Rabbit("fluffy"); var rabbit2 = new Rabbit("happy"); var rabbit3 = new Rabbit("sleepy"); rabbit1.describeMyself(); rabbit2.describeMyself(); rabbit3.describeMyself();
0 notes
Text
Ways of Creating Objects
 There are two ways to create an object in JS:
 var company = { name: “Google”, website: “www.google.ca” }; 
 var company = new Object(); name: “Google”; website: “www.google.ca”; 
Two ways to access properties of an object: 
var org = company.name; 
var org = company[”name”];
0 notes