You have already learned that JavaScript variables are containers for data values(if not check out our previous article Introduction to JS Objects) . and that objects are the same as variables they can contain many values.
what’ll be doing ?
Let’s consider a simple program that displays information about a teacher in a school .
First Start with creating the teacher object as follows
var teacher=new Object()
teacher.firstName= "John",
teacher.lastName= "Doe"
teacher.age=27
or we can use another syntax we can simply write
var teacher= {
firstName: "John",
lastName: "Doe",
age :27
};
now if we want to display the information about Jane Doe we’ll be able to access it through the following syntax
exemple : teacher.firstName
console.log(teacher.firstName)
alert(“The teacher’s name is “ + teacher.firstName + ' ' +teacher.lastName + “and his age is “+ teacher.age );
So now go inside the console of your navigator and create your object
try a console.log(objectName) we can see all the properties of the object we just created
now run this and see what you’ll get !
now let’s get a teacher’s full name using the method fullName()
var teacher= {
firstName: "John",
lastName : "Doe",
age : 27,
fullName : function() {
return this.firstName + " " + this.lastName;
}
};
In your alert this time call the method we just created as follows
name = teacher.fullName();
you’ll get this in your alert
congratulations ! you’ve done it ! you created your first object keep up with us to learn more about javascript . next time we’ll talk about data types !
What’s Happening i’m new to this, I stumbled upon this I’ve found It absolutely useful and it has aided me out loads. I hope to contribute help other users like its helped me. Great job.