Categories: Javascript

What are Objects in Javascript ? – Code Samples

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.

  • Objects created as Variables
  • Objects created using new Object()
  • Objects created using Constructor

 

Objects created as Variables
// 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;
};

 

Objects created using new Object()

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;
}

 

Objects created using Constructor

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]

Ajitesh Kumar

I have been recently working in the area of Data analytics including Data Science and Machine Learning / Deep Learning. I am also passionate about different technologies including programming languages such as Java/JEE, Javascript, Python, R, Julia, etc, and technologies such as Blockchain, mobile computing, cloud-native technologies, application security, cloud computing platforms, big data, etc. I would love to connect with you on Linkedin. Check out my latest book titled as First Principles Thinking: Building winning products using first principles thinking.

Share
Published by
Ajitesh Kumar

Recent Posts

Coefficient of Variation in Regression Modelling: Example

When building a regression model or performing regression analysis to predict a target variable, understanding…

20 minutes ago

Chunking Strategies for RAG with Examples

If you've built a "Naive" RAG pipeline, you've probably hit a wall. You've indexed your…

1 week ago

RAG Pipeline: 6 Steps for Creating Naive RAG App

If you're starting with large language models, you must have heard of RAG (Retrieval-Augmented Generation).…

1 week ago

Python: List Comprehension Explained with Examples

If you've spent any time with Python, you've likely heard the term "Pythonic." It refers…

2 weeks ago

Large Language Models (LLMs): Four Critical Modeling Stages

Large language models (LLMs) have fundamentally transformed our digital landscape, powering everything from chatbots and…

3 months ago

Agentic Workflow Design Patterns Explained with Examples

As Large Language Models (LLMs) evolve into autonomous agents, understanding agentic workflow design patterns has…

3 months ago