简体   繁体   中英

The difference between object and plain object in JavaScript?

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.

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");

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

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()

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.

Any object created with object literals notation is called plain Objects in JavaScript

function Animal(){
//Some codes
}
Var obj = new Animal();`

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 ...

在此处输入图片说明

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM