Wrote this article on Objects & Javascript for future reference. Codecademy.org is my favorite go-to-place for learning Javascript.
Javascript Objects could be defined in following three manners.
// Defining object as a variable
var School = {
name: "DAV Public School",
studentsCount: 600,
admissionOpen: false,
};
School.isAdmissionOpen = function() {
return this.admissionOpen;
};
School.setAdmissionStatus = function( openOrClosed ) {
this.admissionOpen = openOrClosed;
};
In the example below, College is an object type.
// Creating object using new Object()
var College = new Object();
College.name = "IIT Kharagpur";
College.strength = 3500;
College.admissionStatus = false;
College.isAdmissionOpen = function() {
return this.admissionStatus;
}
College.setAdmissionStatus = function( status ) {
this.admissionStatus = status;
}
In the example below, EducationalInstitution is created using the constructor which implies that different types of educational institutions such as schools, and colleges could be created using this type, EducationalInstitution. The object types are created using new operator such as that shown in the code below. This is unlike previous two cases which can be used to create specific objects.
Also, pay attention to the “prototype” keyword which ensures that the methods such as isAdmissionOpen and setAdmissionStatus can be called on all objects types created using “new EducationalInstitution”. If “prototype” is not used, method defined on one object type could not be called on other object types created using “new EducationalInstitution”.
“Prototype” is also used to implement inheritance.
// Creating Object using Constructor
function EducationalInstitution( name, strength ) {
this.name = name;
this.strength = strength;
var admissionStatus = false;
}
EducationalInstitution.prototype.isAdmissionOpen = function() {
return this.admissionStatus;
}
EducationalInstitution.prototype.setAdmissionStatus = function( status ) {
this.admissionStatus = status;
}
var davPublicSchool = new EducationalInstitution( "DAV Public School", 600 );
davPublicSchool.setAdmissionStatus( true );
var iitKharagpur = new EducationalInstitution( "IIT Kharagpur", 3500 );
iitKharagpur.setAdmissionStatus( false );
[adsenseyu1]
Last updated: 25th Jan, 2025 Have you ever wondered how to seamlessly integrate the vast…
Hey there! As I venture into building agentic MEAN apps with LangChain.js, I wanted to…
Software-as-a-Service (SaaS) providers have long relied on traditional chatbot solutions like AWS Lex and Google…
Retrieval-Augmented Generation (RAG) is an innovative generative AI method that combines retrieval-based search with large…
The combination of Retrieval-Augmented Generation (RAG) and powerful language models enables the development of sophisticated…
Have you ever wondered how to use OpenAI APIs to create custom chatbots? With advancements…