OOP studies and a sold painting
Posted by markhwebster on February 8th, 2018 • 0 Comments
I spent today working through my first Object Oriented Programming exercise in JavaScript. Actually, I spent 2 hours of the day framing a painting for a climbing friend. We met at Jtree, and she came to the house and fell in love with this painting. She picked it up today, and I went back to programming. The code below will only work if it’s in a script tag embedded in the <body> element. Then you go to the developer tool console of the browser. Finally, you type person1.bio() and press enter.
It will trigger an alert box that says: “Bob Wayno is 35 years old. He likes music, knitting, skiing, climbing, painting, inventing, and sewing. Shoe size is: 15 and their favorite color is orange.” OOP programming is a stepping stone towards some of the higher end languages I plan to pursue, such as React, AngularJS and Node.js.
If this sounds like a bunch of geek speak move on to the next blog entry. I posted it here because I want to be able to search for it later, and read the code with syntax highlighting. I’m building up some code libraries of my own to refer back to.
//constructor class function names are *Capitalized* /* When an object instance is created from a class, the classes constructor function is run to create the instance. This is called instantiation. The object instance is instantiated from the class. In console type: person1.bio() */ //"function Person" is an class (object template)! function Person(first, last, age, gender, interests, shoeSize, favColor) { this.name = { 'first' : first, 'last' : last }; /*the word age on The right side = what ever string comes in from the object instance via the console.person1.bio() command */ this.age = age; this.gender = gender;// gender = Person[3] this.interests = interests; /*the shoeSize in the parameters listed above Person[5] gets replaced with whatever comes in from when the object instance gets used (instantiated)*/ this.shoeSize = shoeSize; this.favColor = favColor; this.bio = function() { var pronoun; var hobbies = ''; if (this.gender === 'male'){ pronoun = 'He'; } else if (this.gender === 'female'){ pronoun = 'She'; } else { pronoun = 'This person'; } for (let i = 0; i < interests.length; i++) { if(i === interests.length-1){ hobbies += 'and ' + interests[i]; } else { hobbies += interests[i] + ', '; } } var myString = this.name.first + ' ' + this.name.last + ' is ' + this.age + ' years old. ' + pronoun + ' likes ' + hobbies + '. Shoe size is: ' + shoeSize + ' and their favorite color is ' + favColor + '.'; alert(myString); };//end bio function this.greeting = function(){ alert('Hi! I\'m ' + this.name.first + '.'); }; } // person1 is an object instance created from the Person class var person1 = new Person( 'Bob', 'Wayno', 35, 'male', [ 'music', 'knitting', 'skiing', 'climbing', 'painting', 'inventing', 'sewing' ], 15, 'orange' );//end new Person if (person1[3] === 'male'){ person1[3] = 'He'; } else if (person1[3] === 'female'){ person1[3] = 'She'; } else { person1[3] = 'This person'; }//end if gender === ?