简体   繁体   English

JavaScript 中的对象和普通对象的区别?

[英]The difference between object and plain object in JavaScript?

Couldn't understand the difference between object and plain object in JavaScript.无法理解 JavaScript 中对象和普通对象之间的区别。

I know how Object looks like but don't understand plain object.我知道 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或者我们在 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所以我的查询是理解 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.我认为您想将普通旧 JavaScript 对象称为普通对象。

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() .在 vanilla JavaScript 中,POJO(Plain Old JavaScript Object)是您可能拥有的最简单的对象类型:一组键值对,由{}对象文字符号创建或使用new Object()构造。

Plain Old JavaScript Object:普通的旧 JavaScript 对象:

Using the bracket's syntactic sugar also known as object literal:使用括号的语法糖,也称为对象字面量:

var obj = {};

Using the Object() constructor:使用 Object() 构造函数:

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:使用 ES6 类语法:

class myObject  {
  constructor(name) {
    this.name = name;
  }
}
var e = new myObject("hello");

Plain object(POJO - Plain Old Javascript Object)普通对象(PO​​JO - 普通旧 Javascript 对象)

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() .像数组文字一样使用[]而不是new Array() This is an object whose prototype is Object.这是一个原型为 Object 的对象。 A string is an Object too, but its prototype chain looks like: String -> Object.字符串也是一个对象,但它的原型链看起来像:String -> Object。 Arrays are Array -> Object.数组是数组 -> 对象。 These are all objects.这些都是对象。

An object literal's prototype is just, well, Object.对象字面量的原型就是 Object。

Any object created with object literals notation is called plain Objects in JavaScript任何使用对象文字符号创建的对象在 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.在 JS 中,函数是一种类型,对象也是。 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.就像在 react redux 中,动作是以键/值对编写的。

hope you understand it.希望你明白。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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