The difference between object and plain object in JavaScript?
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
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");