I found this very interesting and helps to understand the Java Script engine and how it works when creating objects and functions because functions are objects in Java Script and Objects are name/value pairs that can be created on the fly. Lets take a few examples of how Objects are created in Java Script and how to create new Object within objects based on primary approach and then shorthand. Lets Go!
// How to create objects in Java script. This first example is a primary method of creating an object which is a name /value pair//
//Take a section of script and copy and paste into your browser console or cut and past into your editor and then run to see how this works//
var person = new Object();
//After creating an object you can then define name/value pairs to the object. An example of this is using "firstname" and "lastname" and defining those objects, Here we use the brackets//
person["firstname"] = "Imhotep";
person["lastname"]= "Amen Ra El";
console.log(person.firstname,person.lastname);
VM86762:10 Imhotep Amen Ra El
undefined
/
/Now after printing my name in the console I don't know why it is printing undefined. Can someone answer that?//
//Well Lets go to the new step in creating an object and name/value pairs//
// Shorthard for this is the Object literal and it has the same meaning as var person = new Object(); //
var person = {}; //This set a new object simply by using the squirrely brackets//
console.log(person); //console.log(person) will then show you an Object has been created with no name/value pairs.//
//we can also now add name/value pairs to the object person using the brackets and also create new objects within an object//
var persons = {
firstname: "Edward",
lastname: "Kitchen",
address:{
street: "108 S. Hamlin Blvd.",
city: "Chicago",
state: "Illinois"
}
}
console.log(persons.firstname, persons.lastname,persons.address.street,persons.address.city,persons.address.state);
function greet(persons) {
console.log('Hi' + persons.firstname);
}
VM1465:4 {}__proto__: Object
VM1465:16 Edward Kitchen 108 S. Hamlin Blvd. Chicago Illinois
undefined
//My question here is why is my greet function undefined when I call it belowwhen it is clear from the first statement that I have defined persons.firstname = "Edward" //
console.log(persons.firstname);
VM2650:1 Edward
undefined
console.log(greet(persons.firstname));
VM1465:18 Hiundefined
VM2808:1 undefined
//To be continued// OK this is the continuation of Objects and Object Creation. I try to explain what I learn to help me understand it better. Lets create a new Object using the shorthand and then add some name /value pairs and new Object within the Object personnel//
var personnel = {}; //new object creation
console.log(personnel) //will confirm new object creation
var personnel={firstname: “Imhotep”,lastname: “Amen Ra El”,address:{street: “108 S Hamlin Ave”,city: “Chicago”,state: “Illinois”}} //define name value pairs within object and create new Object “address” within Object “personnel”
function greet(personnnel){
console.log('Hi' + personnel.firstname);
} // define function greet and instructions
greet(personnel); //call the function greet
VM266:2 {}
VM266:7 HiImhotep
undefined
greet(person); //person not defined must use personnel
VM307:1 Uncaught ReferenceError: person is not defined
at :1:7
(anonymous) @ VM307:1
console.log(personnel.firstname,personnel.lastname,personnel.address.street,personnel.address.city,personnel.address.state);
VM814:1 Imhotep Amen Ra El 108 S Hamlin Ave Chicago Illinois
undefined //Why the undefined here anyone? What is undefined?
// This is interesting. I exchanged the object in the greet function without knowing how I did it .
//I can also create a name/value pair on the fly inside of a function//
greet(Tony);
greet({firstname: 'Imhotep',lastname:'Amen Ra El'})
console.log(greet(Tony));
VM913:3 Uncaught ReferenceError: Tony is not defined
at :3:11
(anonymous) @ VM913:3
var Tony = {}; //So now I define “Tony” as a new Object
undefined
greet({firstname: “Imhotep”,lastname:“Amen Ra El”});
//I define name/value pairs for greet function and it immediately prints below
VM266:7 HiImhotep
undefined
greet(Tony); //and now when I greet(Tony); I also get the same HiImhotep.
VM266:7 HiImhotep
undefined //The old undefined again.
// What I find interesting is that my original greet function was greet(personnel){“HI” + personnel.firstname}; And this produced Hi Imhotep when I invoke the function greet(personnel); Now what happens apparently is that I changed the bag within the greet function from “personnel” to “Tony” and I defined the Object Tony on the fly with name value pairs that just happen to be the same as the value pairs of “personnel”. Maybe need to change the name/value pairs of Tony and see if it prints the Object name/value for “Tony”. Let me try this.
Well no matter what I define as my new object, whenever I invoke the function greet, I will get the same result. So defining a new object and using the object as a parameter has no affect on the result. It will not use the name value pair of the new Object. The only way to change the result is to change the instructions within the function. Now again whenever I invoke the greet function you get the same result no matter what parameter you use or object you use to call the function. Can someone explain this better?
greet({firstname: ‘Mary’, lastname: ‘Doe’});
VM266:7 HiImhotep
undefined
greet(Tony);
VM266:7 HiImhotep
undefined
Tony
{}proto: constructor: ƒ Object()hasOwnProperty: ƒ hasOwnProperty()isPrototypeOf: ƒ isPrototypeOf()propertyIsEnumerable: ƒ propertyIsEnumerable()toLocaleString: ƒ toLocaleString()toString: ƒ toString()valueOf: ƒ valueOf()defineGetter: ƒ defineGetter()defineSetter: ƒ defineSetter()lookupGetter: ƒ lookupGetter()lookupSetter: ƒ lookupSetter()get proto: ƒ proto()set proto: ƒ proto()
greet(Imhotep);
VM1119:1 Uncaught ReferenceError: Imhotep is not defined
at :1:7
(anonymous) @ VM1119:1
console.log(greet(Tony));
VM266:7 HiImhotep
VM1195:1 undefined
undefined
console.log(greet(personnel));
VM266:7 HiImhotep
VM1289:1 undefined
undefined
function greet(Tony){console.log(“Hello” + " " + personnel.firstname + personnel.lastname)};
undefined
greet(Tony);
VM1620:1 Hello ImhotepAmen Ra El
undefined
function greet(Tony){console.log(“Hello” + " " + personnel.firstname + " " + personnel.lastname)};greet(Tony);
VM1718:1 Hello Imhotep Amen Ra El
undefined
greet(persons);
VM1718:1 Hello Imhotep Amen Ra El
undefined
greet(personnnel); //missed spelled.
VM1825:1 Uncaught ReferenceError: personnnel is not defined
at :1:7
(anonymous) @ VM1825:1
greet(personnel);
VM1718:1 Hello Imhotep Amen Ra El
undefined