Reputation: 34014
Couldn’t understand the difference between object and plain object in JavaScript.
I know how Object looks like but don’t understand plain object. I googled about this but couldn’t understand.
As per my understanding normal object looks like below
const object = {};
Or we do call functions as objects in JavaScript
function test() {
}
But what is plain object? how it differs with normal object. Thank you
Edit:
My confusion started about plain object after looking at below error. So my query is to understand the concept of plain object in JavaScript
Actions must be plain objects. Use custom middleware for async actions.
Upvotes: 19
Views: 41583
Reputation: 2753
Any object created with object literals notation is called plain Objects in JavaScript
function Animal(){
//Some codes
}
var obj = new Animal();
Upvotes: 2
Reputation: 1
plain objects (sets of key/value pairs wrapped in { } ) are great for storing simple data sets.
var lunch={
sandwich:'furkey',
drink:'soda',
chips:true
}
like in react redux actions are written in key/value pairs.
hope you understand it.
Upvotes: -3
Reputation: 68933
I think you wanted to mean Plain Old JavaScript Object as plain object.
In vanilla JavaScript a POJO (Plain Old JavaScript Object) is the simplest kind of object you could possibly have: a set of key-value pairs, created by the {}
object literal notation or constructed with new Object()
.
Plain Old JavaScript Object:
Using the bracket's syntactic sugar also known as object literal:
var obj = {};
Using the Object() constructor:
var obj = new Object();
Other Than Plain Object:
Using a function constructor:
var Obj = function(name) {
this.name = name;
}
var c = new Obj("hello");
Using ES6 class syntax:
class myObject {
constructor(name) {
this.name = name;
}
}
var e = new myObject("hello");
Upvotes: 27
Reputation: 200
Plain object(POJO - Plain Old Javascript Object)
var plainObj1 = {}; // typeof plainObj1 --> Object
var plainObj2 = {name : "myName"}; // typeof plainObj2 --> Object
var plainObj3 = new Object(); // typeof plainObj3 --> Object
Non Plain object
var Person = function(){}; //class
var nonPlainObj = new Person(); // typeof nonPlainObj --> function
Upvotes: 15
Reputation: 7746
You are talking about object literals, which is a literal object, {}
. Like array literals use []
instead of new Array()
. This is an object whose prototype is Object. A string is an Object too, but its prototype chain looks like: String -> Object. Arrays are Array -> Object. These are all objects.
An object literal's prototype is just, well, Object.
Upvotes: 2
Reputation: 7957
In your question, you cite that you think both an object literal and a function are both "objects". In JS, function is a type, and so is object. So your original question, those two items are not objects ...
Upvotes: 1
Reputation: 1203
An Object created by literal notation or new Object are know as plain object. example :
let a = {aaa : 1}
let b = new Object()
while Object created using function are not plain object
let C = function(){}
let d = new C()
Upvotes: 6