You are reading the article Object Oriented Javascript(Oojs) Tutorial With Example updated in October 2023 on the website Restaurant12h.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested November 2023 Object Oriented Javascript(Oojs) Tutorial With Example
What is OOPS Concept in JavaScript?Many times, variables or arrays are not sufficient to simulate real-life situations. JavaScript allows you to create objects that act like real life objects. A student or a home can be an object that have many unique characteristics of their own. You can create properties and methods to your objects to make programming easier. If your object is a student, it will have properties like first name, last name, id etc and methods like calculateRank, changeAddress etc. If your object is a home, it will have properties like a number of rooms, paint color, location etc and methods like calculateArea, changeOwner etc.
How to Create an ObjectYou can create an object like this:
var objName = new Object(); objName.property1 = value1; objName.property2 = value2; objName.method1 = function() { line of code }OR
var objName= {property1:value1, property2:value2, method1: function() { lines of code} };Access Object Properties and Methods
You can access properties of an object like this:
objectname.propertyname;You can access methods of an object like this:
objectname.methodname();Try this Example yourself:
var student = new Object(); student.fName = “John”; student.lName = “Smith”; chúng tôi = 5; student.markE = 76; student.markM = 99; student.markS = 87; student.calculateAverage = function() { return (student.markE + student.markM + student.markS)/3; }; student.displayDetails = function() { var avg = student.calculateAverage(); document.write(“Average Marks: ” + avg); }; student.displayDetails();
OOPS ConstructorBut creating objects of this kind is not that useful because here also, you will have to create different objects for different students. Here comes object constructor into picture. Object constructor helps you create an object type which can be reused to meet the need of individual instance.
Try this Example yourself:
function Student(first, last, id, english, maths, science) { this.fName = first; this.lName = last; chúng tôi = id; this.markE = english; this.markM = maths; this.markS = science; this.calculateAverage = function() { return (this.markE + this.markM + this.markS)/3; } this.displayDetails = function() { var avg = this.calculateAverage(); } } var st1 = new Student(“John”, “Smith”, 15, 85, 79, 90); var st2 = new Student(“Hannah”, “Turner”, 23, 75, 80, 82); var st3 = new Student(“Kevin”, “White”, 4, 93, 89, 90); var st4 = new Student(“Rose”, “Taylor”, 11, 55, 63, 45); st1.displayDetails(); st2.displayDetails(); st3.displayDetails(); st4.displayDetails();
Loop Through the Properties of an ObjectSyntax:
for (variablename in objectname) { lines of code to be executed }The for/in a loop is usually used to loop through the properties of an object. You can give any name for the variable, but the name of the object should be the same as that of an already existing object which you need to loop through.
Try this Example yourself:
var employee={first:”John”, last:”Doe”, department:”Accounts”}; var details = “”; for (var x in employee) { details = x + “: ” + employee[x]; }
You're reading Object Oriented Javascript(Oojs) Tutorial With Example
Update the detailed information about Object Oriented Javascript(Oojs) Tutorial With Example on the Restaurant12h.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!