Riyad Amin

Riyad Amin


How to create JavaScript objects using different methods

Today, we are going to learn about creating JavaScript objects using various methods given below.

  1. Object Literals.
  2. Constructor Functions.
  3. Object.create().
  4. ECMAScript 6 Classes.

Let us understand them in detail.

Object Literals

I’d say this is one of the simplest and the most common ways to create objects in JavaScript. We just simply define the properties and their values inside curly brackets like given below.

var student = {name: 'Ross', rollno: 1}  

Here, I’ve created a new object with two properties — name and rollno. We can confirm it by printing out the values of the created object.


Easy enough, isn’t it?

Constructor Functions

What if we want to create multiple instances with the same set of properties/structure? And, we also don’t want to endup writing multiple lines of object literals…

var ross = {name: 'Ross', rollno: 1}   
var david = {name: 'David', rollno: 2} var atul = {name: 'Atul', rollno: 3}  

To solve the problem, let’s create a function.

funtion Student(name,age)  
{ this.name = name; this.age = age; }   

The this keyword refers to an object. That object is whatever object is executing the current bit of code. By default, that is the global object. In a web browser, that is the Windows object.

Notice that this is just a simple function and nothing special. The new keyword is followed by the function name to initialize the object.

var ross = new Student('Ross',1);   
var david = new Student('David',2);  

When we use the new keyword with a function name, an empty object gets created and the this keyword used inside that function holds the reference to that object.

Now, we don’t need to pass the property name along with its value. To print the same output, we can write.


Let me give you another example with a twist.

function Student(registrationNo) {  
    this.registrationNo = registrationNo;  
var amit = Student(123);  

It will print undefined. Notice, I didn’t use the new keyword with the function name on purpose. So, all the properties get attached to the Windows object.


The function, like Student, is commonly called constructor function. But you can see that there’s really nothing special about it. It is just a function. This is a very common pattern for creating objects in JavaScript.


So far, we’ve looked at two different ways of creating objects using object literals and using constructor functions with the new keyword. These are just basically syntactic sugar for object.create.

We could create the same objects using the object.create() syntax as follows.

var ross = Object.create(Object.prototype, {  
    name: {  
        value: 'ross',  
        enumerable: true,  
        writable: true,  
        configurable: true  
    rollno: {  
        value: 1,  
        enumerable: true,  
        writable: true,  
        configurable: true  

For each property, we are setting the value and the enumerable, writable, and configurable properties to true. This automatically gets done for us when using either object literals or constructor functions.

You may be wondering what these attributes are. So, I will be writing another blog to explain all these properties and inheritance in JavaScript.

ECMAScript 6 Classes

For browsers that support it, the ECMAScript 6 specs now provide the functionality of creating objects using a class structure that is similar to statically typed languages like C#, .NET, and Java.

Again, this is just syntactic sugar on top of the existing object creation functionality. So, to create our object using a class, we would do something like this.

class Student {  
    constructor(name, rollno) {  
        this.name = name;  
        this.rollno = rollno;  
    getStudentDetails() {  
        return "Student: " + this.name + ", Roll No: " + this.rollno;  
var ross = new Student("Ross", 1);  

Thanks for reading !

#js #javascript #developer

What is GEEK

Buddha Community

How to create JavaScript objects using different methods
Arvel  Parker

Arvel Parker


How to Find Ulimit For user on Linux

How can I find the correct ulimit values for a user account or process on Linux systems?

For proper operation, we must ensure that the correct ulimit values set after installing various software. The Linux system provides means of restricting the number of resources that can be used. Limits set for each Linux user account. However, system limits are applied separately to each process that is running for that user too. For example, if certain thresholds are too low, the system might not be able to server web pages using Nginx/Apache or PHP/Python app. System resource limits viewed or set with the NA command. Let us see how to use the ulimit that provides control over the resources available to the shell and processes.

#[object object] #[object object] #[object object] #[object object] #[object object] #[object object] #[object object] #[object object] #[object object] #[object object]

MEAN Stack Tutorial MongoDB ExpressJS AngularJS NodeJS

We are going to build a full stack Todo App using the MEAN (MongoDB, ExpressJS, AngularJS and NodeJS). This is the last part of three-post series tutorial.

MEAN Stack tutorial series:

AngularJS tutorial for beginners (Part I)
Creating RESTful APIs with NodeJS and MongoDB Tutorial (Part II)
MEAN Stack Tutorial: MongoDB, ExpressJS, AngularJS and NodeJS (Part III) 👈 you are here
Before completing the app, let’s cover some background about the this stack. If you rather jump to the hands-on part click here to get started.

#[object object] #[object object] #[object object] #[object object] #[object object] #[object object] #[object object] #[object object]

Creating RESTful APIs with NodeJS and MongoDB Tutorial

Welcome to this tutorial about RESTful API using Node.js (Express.js) and MongoDB (mongoose)! We are going to learn how to install and use each component individually and then proceed to create a RESTful API.

MEAN Stack tutorial series:

AngularJS tutorial for beginners (Part I)
Creating RESTful APIs with NodeJS and MongoDB Tutorial (Part II) 👈 you are here
MEAN Stack Tutorial: MongoDB, ExpressJS, AngularJS and NodeJS (Part III)

#[object object] #[object object] #[object object] #[object object] #[object object] #[object object] #[object object]

systemctl List All Failed Units/Services on Linux

Is there is a command to print list all failed units or services when using systemd on Linux? Can you tell me the systemctl command to list all failed services on Linux?

This quick tutorial explains how to find/list all failed systemd services/units on Linux operating systems using the systemctl command.

#[object object] #[object object] #[object object] #[object object] #[object object] #[object object] #[object object]

Yoshiko  Jones

Yoshiko Jones


How to configure AWS SES with Postfix MTA

How do I configure Amazon SES With Postfix mail server to send email under a CentOS/RHEL/Fedora/Ubuntu/Debian Linux server?

Amazon Simple Email Service (SES) is a hosted email service for you to send and receive email using your email addresses and domains. Typically SES used for sending bulk email or routing emails without hosting MTA. We can use Perl/Python/PHP APIs to send an email via SES. Another option is to configure Linux or Unix box running Postfix to route all outgoing emails via SES.

  • » Remove sendmail
  • » Install postfix
  • » Configuring postfix for SES
  • » Test postfix

Procedure to configure AWS SES with Postfix

Before getting started with Amazon SES and Postfix, you need to sign up for AWS, including SES. You need to verify your email address and other settings. Make sure you create a user for SES access and download credentials too.

Step 1 – Uninstall Sendmail if installed

If sendmail installed remove it. Debian/Ubuntu Linux user type the following apt command/apt-get command:

$`` sudo apt --purge remove sendmail

CentOS/RHEL user type the following yum command or dnf command on Fedora/CentOS/RHEL 8.x:

$`` sudo yum remove sendmail

$`` sudo dnf remove sendmail

Sample outputs from CentOS 8 server:

Dependencies resolved.
 Package           Architecture  Version               Repository         Size
 sendmail          x86_64        8.15.2-32.el8         @AppStream        2.4 M
Removing unused dependencies:
 cyrus-sasl        x86_64        2.1.27-1.el8          @BaseOS           160 k
 procmail          x86_64        3.22-47.el8           @AppStream        369 k

Transaction Summary
Remove  3 Packages

Freed space: 2.9 M
Is this ok [y/N]: y

#[object object] #[object object] #[object object] #[object object] #[object object] #[object object] #[object object]