There is no best way, it depends on your use case.

  • Use way 1 if you want to create several similar objects. In your example, Person (you should start the name with a capital letter) is called the constructor function. This is similar to classes in other OO languages.
  • Use way 2 if you only need one object of a kind (like a singleton). If you want this object to inherit from another one, then you have to use a constructor function though.
  • Use way 3 if you want to initialize properties of the object depending on other properties of it or if you have dynamic property names.

Update: As requested examples for the third way.

Dependent properties:

The following does not work as this does not refer to book. There is no way to initialize a property with values of other properties in a object literal:

var book = {
    price: somePrice * discount,
    pages: 500,
    pricePerPage: this.price / this.pages
};

instead, you could do:

var book = {
    price: somePrice * discount,
    pages: 500
};
book.pricePerPage = book.price / book.pages;
// or book['pricePerPage'] = book.price / book.pages;

Dynamic property names:

If the property name is stored in some variable or created through some expression, then you have to use bracket notation:

var name = 'propertyName';

// the property will be `name`, not `propertyName`
var obj = {
    name: 42
}; 

// same here
obj.name = 42;

// this works, it will set `propertyName`
obj[name] = 42;
Answer from Felix Kling on Stack Overflow
🌐
Mozilla
developer.mozilla.org › en-US › docs › Web › JavaScript › Guide › Working_with_objects
Working with objects - JavaScript | MDN
August 21, 2026 - For example, this example creates an object named myCar, with properties named make, model, and year, with their values set to "Ford", "Mustang", and 1969: ... Like JavaScript variables, property names are case sensitive. Property names can only be strings or Symbols — all keys are converted to strings unless they are Symbols. Array indices are, in fact, properties with string keys that contain integers.
🌐
W3Schools
w3schools.com › js › js_object_definition.asp
JavaScript Object Definitions
An object literal is a list of property key:values inside curly braces { }. {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"}; // Create an Object const person = { firstName: "John", lastName: "Doe", age: 50, eyeColor: "blue" }; Try it Yourself »
Discussions

Which way is best for creating an object in JavaScript? Is `var` necessary before an object property? - Stack Overflow
So far I saw three ways for creating an object in JavaScript. Which way is best for creating an object and why? I also saw that in all of these examples the keyword var is not used before a proper... More on stackoverflow.com
🌐 stackoverflow.com
ecmascript 5 - Is there any reason to use Object.create() or new in JavaScript? - Stack Overflow
I've been using the new keyword in JavaScript so far. I have been reading about Object.create and I wonder if I should use it instead. What I don't quite get is that I often need to run constructio... More on stackoverflow.com
🌐 stackoverflow.com
Understanding Object.create() in JavaScript
In this case, Student is a function when you updated NinthGrader's prototype (although all functions are also objects in JS), and does not have a gender (the gender prop exists on the student instance carl and not the function that created carl). More on reddit.com
🌐 r/learnprogramming
2
1
January 10, 2022
Which way is the best way to create objects in Javascript?
EDIT: This post has been published--refined and expanded--as a SitePoint article. https://www.sitepoint.com/javascript-object-creation-patterns-best-practises/ So, here's how things evolved. Simple objects. Obviously the simplest way to make an object in JavaScript is an object literal. var o = { x: 42, y: 3.14, f: function() {}, g: function() {} }; But there's a drawback. If you need to use the same type of object in other places, then you'll end up copy-pasting the object's structure and initialization. The fix... 2) Factory functions. Objects are created, initialized, and returned from functions. function thing() { return { x: 42, y: 3.14, f: function() {}, g: function() {} }; } var o = thing(); But there's a drawback. We're creating fresh copies of functions "f" and "g" with each object. It would be better if all "thing" objects could share just one copy of each function. (Note: JavaScript engines are heavily optimized, so this issue is less important today than it used to be.) The fix... 3) Delegating to prototypes. JavaScript makes it easy to delegate property accesses to other objects through what we call the prototype chain. var thingPrototype = { f: function() {}, g: function() {} }; function thing() { var o = Object.create(thingPrototype); o.x = 42; o.y = 3.14; return o; } var o = thing(); This is such a common pattern that the language has some built-in support. A prototype object is created automatically for each function. thing.prototype.f = function() {}; thing.prototype.g = function() {}; function thing() { var o = Object.create(thing.prototype); o.x = 42; o.y = 3.14; return o; } var o = thing(); But there's a drawback. This is going to result in some repetition. In the "thing" function, the first and last lines are going to be repeated almost verbatim in every such delegating-to-prototype-factory-function. The fix... 4) Consolidate the repetition. function create(func) { var o = Object.create(func.prototype); func.call(o); return o; } Thing.prototype.f = function() {}; Thing.prototype.g = function() {}; function Thing() { this.x = 42; this.y = 3.14; } var o = create(Thing); The repetitive lines from "thing" have been moved into "create" and made generic to operate on any such function. This too is such a common pattern that the language has some built-in support. The "create" function we defined is actually a rudimentary version of the "new" keyword. Thing.prototype.f = function() {}; Thing.prototype.g = function() {}; function Thing() { this.x = 42; this.y = 3.14; } var o = new Thing; We've now arrived at ES5 classes. They are object creation functions that delegate shared properties to a prototype object and rely on the new keyword to handle repetitive logic. But there's a drawback. It's verbose and ugly. And implementing the notion of inheritance is even more verbose and ugly. The fix... 5) ES6 classes. They offer a significantly cleaner syntax for doing the same thing. class Thing { constructor() { this.x = 42; this.y = 3.14; } f() {} g() {} } var o = new Thing; More on reddit.com
🌐 r/javascript
78
249
March 27, 2016
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Object › create
Object.create() - JavaScript | MDN
The Object.create() static method creates a new object, using an existing object as the prototype of the newly created object.
Top answer
1 of 8
186

There is no best way, it depends on your use case.

  • Use way 1 if you want to create several similar objects. In your example, Person (you should start the name with a capital letter) is called the constructor function. This is similar to classes in other OO languages.
  • Use way 2 if you only need one object of a kind (like a singleton). If you want this object to inherit from another one, then you have to use a constructor function though.
  • Use way 3 if you want to initialize properties of the object depending on other properties of it or if you have dynamic property names.

Update: As requested examples for the third way.

Dependent properties:

The following does not work as this does not refer to book. There is no way to initialize a property with values of other properties in a object literal:

var book = {
    price: somePrice * discount,
    pages: 500,
    pricePerPage: this.price / this.pages
};

instead, you could do:

var book = {
    price: somePrice * discount,
    pages: 500
};
book.pricePerPage = book.price / book.pages;
// or book['pricePerPage'] = book.price / book.pages;

Dynamic property names:

If the property name is stored in some variable or created through some expression, then you have to use bracket notation:

var name = 'propertyName';

// the property will be `name`, not `propertyName`
var obj = {
    name: 42
}; 

// same here
obj.name = 42;

// this works, it will set `propertyName`
obj[name] = 42;
2 of 8
118

There is various way to define a function. It is totally based upon your requirement. Below are the few styles :-

  1. Object Constructor
  2. Literal constructor
  3. Function Based
  4. Protoype Based
  5. Function and Prototype Based
  6. Singleton Based

Examples:

  1. Object constructor
var person = new Object();

person.name = "Anand",
person.getName = function(){
  return this.name ; 
};
  1. Literal constructor
var person = { 
  name : "Anand",
  getName : function (){
   return this.name
  } 
} 
  1. function Constructor
function Person(name){
  this.name = name
  this.getName = function(){
    return this.name
  } 
} 
  1. Prototype
function Person(){};

Person.prototype.name = "Anand";
  1. Function/Prototype combination
function Person(name){
  this.name = name;
} 
Person.prototype.getName = function(){
  return this.name
} 
  1. Singleton
var person = new function(){
  this.name = "Anand"
} 

You can try it on console, if you have any confusion.

🌐
W3Schools
w3schools.com › Jsref › jsref_object_create.asp
JavaScript Object.create() Method
Object.create() is an ECMAScript5 (ES5 2009) feature. JavaScript 2009 is supported in all browsers since July 2013:
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › creating-objects-in-javascript-4-different-ways
Ways to Create Objects in JavaScript - GeeksforGeeks
August 22, 2026 - An object in JavaScript is a collection of key-value pairs where keys are properties and values can be any data type. JavaScript provides several ways to create objects, including object literals, constructor functions, Object.create(), and ES6 classes.
🌐
W3Schools
w3schools.com › js › js_objects.asp
JavaScript Objects
Getters and setters allow you to define Object Accessors (Computed Properties). ... Coding fundamentals as a game. Bite-sized lessons and challenges. ... Ready to start your journey? Your streak is waiting. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com · If you want to report an error, or if you want to make a suggestion, send us an e-mail: help@w3schools.com · HTML Tutorial CSS Tutorial JavaScript ...
Top answer
1 of 4
18

So far, if you want to create an object, you can only use literals:

var obj = {};

or the Object constructor.

var obj = Object();

But none of these methods let you specify the prototype of the created object.

This is what you can do with Object.create now. It lets you create a new object and sets the first argument as prototype of the new object. In addition, it allows you to set properties of the new object provided as second argument.

It is similar to doing something like this (without the second argument):

function create(proto) {
    var Constr = function(){};
    Constr.prototype = proto;
    return new Constr();
}

So if you are using a construct similar to this, this when you wanted to use Object.create.

It is not a replacement for new. It is more an addition to make creating single objects which should inherit from another object simpler.

Example:

I have an object a:

var a = {
   someFunction: function() {}
};

and I want b to extend this object. Then you can use Object.create:

b = Object.create(a);
b.someOtherFunction = function(){};

Whenever you have a constructor function, but you only instantiate one object from it, you might be able to replace this with Object.create.

There is general rule that applies. It depends very much on what the constructor function is doing and how you inherit from other objects, etc.

2 of 4
6

As already mentioned, Object.create() is commonly used when you want an easy way to set the prototype of a new object. What the other answers fail to mention though, is that constructor functions (which require new) are not all that different from any other function.

In fact, any function can return an object, and it's common in JavaScript to see factory functions (like constructors, but they don't require new, or use this to refer to the new object). Factory functions often use Object.create() to set the prototype of the new object.

var barPrototype = {
  open: function open() { /* ... */ },
  close: function close() { /* ... */ },
};
function createBar() {
  return Object.create(barPrototype);
}

var bar = createBar();
🌐
SitePoint
sitepoint.com › blog › javascript › javascript object creation: patterns and best practices
JavaScript Object Creation: Patterns and Best Practices — SitePoint
November 7, 2024 - The next stop on our JavaScript object creation tour is the factory function. This is the absolute simplest way to create a family of objects that share the same structure, interface, and implementation. Rather than creating an object literal directly, instead we return an object literal from ...
🌐
freeCodeCamp
freecodecamp.org › news › javascript-create-object-how-to-define-objects-in-js
JavaScript Create Object – How to Define Objects in JS
July 20, 2020 - By Cristian Salcescu Objects are the main unit of encapsulation in Object-Oriented Programming. In this article, I will describe several ways to build objects in JavaScript. They are: Object literal Object.create() Classes Factory functions Object ...
🌐
Reddit
reddit.com › r/learnprogramming › understanding object.create() in javascript
r/learnprogramming on Reddit: Understanding Object.create() in JavaScript
January 10, 2022 -

Hi All,

The Odin Project has an example using an empty Student constructor, and an Eighth Grader constructor:

function Student() {
}

Student.prototype.sayName = function() {
  console.log(this.name)
}

function EighthGrader(name) {
  this.name = name
  this.grade = 8
}

EighthGrader.prototype = Object.create(Student.prototype)

const carl = new EighthGrader("carl")
carl.sayName() // console.logs "carl"
carl.grade // 8

This makes sense to me--the EighthGrader prototype is set to an object that is equal to the Student prototype, giving EighthGrader objects access to the Student.prototype methods. However, there's nothing in the student constructor, so I tried to mess around a bit, add a parameter to the student constructor, and make a new NinthGrader object that could access those properties. It's not working as I expected:

function Student(gender) {
  this.gender = gender;
}

Student.prototype.sayName = function() {
  console.log(this.name)
}

function EighthGrader(name) {
  this.name = name
  this.grade = 8
}

function NinthGrader() {
  
}

EighthGrader.prototype = Object.create(Student.prototype)
NinthGrader.prototype = Object.create(Student);

const carl = new EighthGrader("carl")
carl.sayName() // console.logs "carl"

const john = new NinthGrader("male");
console.log(john.gender); // logs undefined, shouldn't it log male, since NinthGrader's prototype is Student?

As I said in the comment there, I expected Student to be the prototype of of NinthGrader, which I thought would give NinthGrader objects access to the student properties (in this case, gender). Clearly I'm mistaken. Can anyone help me understand? Thanks!

🌐
Reddit
reddit.com › r/javascript › which way is the best way to create objects in javascript?
r/javascript on Reddit: Which way is the best way to create objects in Javascript?
March 27, 2016 -

Dear all,

I am trying to figure out Javascript syntax for creating objects, and it is confusing me a little bit, because there seem to be multiple ways of doing it.

I've seen:

(1)

function book(title, author){ 
     this.title = title;
     this.author = author;
}

var somebook = new book("Harry Potter", "J.K. Rowling");

I've also seen the new ECMAscript 6 "class" identifier.

I've also seen:

(2)

var book = {
     title: "Harry Potter", author: "J.K. Rowling"
}

Is it recommended to write out a constructor function, like in (1)?

If we simply create a particular object directly, like (2), would subsequent instantiations of book objects work if we give it a different set of properties?

What is the best way to think about objects in Javascript? Thanks.

Top answer
1 of 5
659
EDIT: This post has been published--refined and expanded--as a SitePoint article. https://www.sitepoint.com/javascript-object-creation-patterns-best-practises/ So, here's how things evolved. Simple objects. Obviously the simplest way to make an object in JavaScript is an object literal. var o = { x: 42, y: 3.14, f: function() {}, g: function() {} }; But there's a drawback. If you need to use the same type of object in other places, then you'll end up copy-pasting the object's structure and initialization. The fix... 2) Factory functions. Objects are created, initialized, and returned from functions. function thing() { return { x: 42, y: 3.14, f: function() {}, g: function() {} }; } var o = thing(); But there's a drawback. We're creating fresh copies of functions "f" and "g" with each object. It would be better if all "thing" objects could share just one copy of each function. (Note: JavaScript engines are heavily optimized, so this issue is less important today than it used to be.) The fix... 3) Delegating to prototypes. JavaScript makes it easy to delegate property accesses to other objects through what we call the prototype chain. var thingPrototype = { f: function() {}, g: function() {} }; function thing() { var o = Object.create(thingPrototype); o.x = 42; o.y = 3.14; return o; } var o = thing(); This is such a common pattern that the language has some built-in support. A prototype object is created automatically for each function. thing.prototype.f = function() {}; thing.prototype.g = function() {}; function thing() { var o = Object.create(thing.prototype); o.x = 42; o.y = 3.14; return o; } var o = thing(); But there's a drawback. This is going to result in some repetition. In the "thing" function, the first and last lines are going to be repeated almost verbatim in every such delegating-to-prototype-factory-function. The fix... 4) Consolidate the repetition. function create(func) { var o = Object.create(func.prototype); func.call(o); return o; } Thing.prototype.f = function() {}; Thing.prototype.g = function() {}; function Thing() { this.x = 42; this.y = 3.14; } var o = create(Thing); The repetitive lines from "thing" have been moved into "create" and made generic to operate on any such function. This too is such a common pattern that the language has some built-in support. The "create" function we defined is actually a rudimentary version of the "new" keyword. Thing.prototype.f = function() {}; Thing.prototype.g = function() {}; function Thing() { this.x = 42; this.y = 3.14; } var o = new Thing; We've now arrived at ES5 classes. They are object creation functions that delegate shared properties to a prototype object and rely on the new keyword to handle repetitive logic. But there's a drawback. It's verbose and ugly. And implementing the notion of inheritance is even more verbose and ugly. The fix... 5) ES6 classes. They offer a significantly cleaner syntax for doing the same thing. class Thing { constructor() { this.x = 42; this.y = 3.14; } f() {} g() {} } var o = new Thing;
2 of 5
11
I prefer ES2015 syntax. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes class Polygon { constructor(height, width) { this.height = height; this.width = width; } } Of course, this means you'll need to convert it to current syntax, so you'll have to use something like Babel to make it work on modern browsers. For the record, Babel converts that to this: "use strict"; function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } var Polygon = function Polygon(height, width) { _classCallCheck(this, Polygon); this.height = height; this.width = width; };
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-object-create-method
JavaScript Object create() Method - GeeksforGeeks
July 12, 2024 - JavaScript object.create() method is used to create a new object with the specified prototype object and properties. Object.create() method returns a new object with the specified prototype object and properties.
🌐
Medium
medium.com › @AlexanderObregon › object-creation-in-javascript-and-what-happens-behind-the-scenes-2b2829b70891
Object Creation in JavaScript and What Happens Behind the Scenes
April 19, 2025 - Here we will look at the three most common patterns, object literals, constructor functions with new, and Object.create—and explain what the JavaScript engine actually does with each one. Object literals are the most direct and readable option. You see them used almost everywhere. ... It may look like you’re just creating a container for some named values. But when this code runs, the engine carries out more work than it appears. First, it creates a new object in memory.
🌐
Oitihjya Sen
otee.dev › 2021 › 07 › 19 › creating-objects-in-javascript.html
Creating Objects in JavaScript | Oitihjya Sen
July 19, 2021 - Thus, when this.propertyName is written inside a constructor, it will create a property called propertyName every time a new object is created by calling that constructor along with the new keyword. Essentially, the new keyword tells the JavaScript interpreter to create an empty object, and add the properties provided inside the constructor using the this keyword.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Operators › new
new - JavaScript | MDN
... A list of values that the constructor will be called with. new Foo is equivalent to new Foo(), i.e., if no argument list is specified, Foo is called without arguments. When a function is called with the new keyword, the function will be used as a constructor. new will do the following things: ...
🌐
OpenAI Developers
developers.openai.com › api › docs › guides › batch
Batch API | OpenAI API
1 2 3 4openai batches create \ --input-file-id file-abc123 \ --endpoint /v1/chat/completions \ --completion-window 24h · This request will return a Batch object with metadata about your batch:
🌐
Esri Developer
developers.arcgis.com › javascript › latest
ArcGIS Maps SDK for JavaScript
June 25, 2026 - Add natural-language map interactions with the ArcGIS Assistant component. Show highly realistic 3D environments with Gaussian Splat layers. Create and update features quickly with the Editor component.
🌐
NestJS
docs.nestjs.com › first-steps
Documentation | NestJS - A progressive Node.js framework
When you pass a type to the NestFactory.create() method, as in the example below, the app object will have methods available exclusively for that specific platform.