JavaScript 中的对象和普通对象的区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/52453407/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The difference between object and plain object in JavaScript?
提问by Hemadri Dasari
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. I googled about this but couldn't understand.
我知道 Object 的样子,但不了解普通对象。我用谷歌搜索了这个,但无法理解。
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.
动作必须是普通对象。将自定义中间件用于异步操作。
回答by Mamun
I think you wanted to mean Plain Old JavaScript Objectas 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().
在原生 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");
回答by Sathish
Plain object(POJO - Plain Old Javascript Object)
普通对象(POJO - 普通旧 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
回答by Ishant Gaurav
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()
回答by Pratap Sharma
Any object created with object literals notation is called plain Objects in JavaScript
任何使用对象文字符号创建的对象在 JavaScript 中都称为普通对象
function Animal(){
//Some codes
}
Var obj = new Animal();`
回答by Ryan
回答by Rafael
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.
你在谈论对象文字,它是一个文字对象,{}. 像数组文字一样使用[]而不是new Array(). 这是一个原型为 Object 的对象。字符串也是一个对象,但它的原型链看起来像:String -> Object。数组是数组 -> 对象。这些都是对象。
An object literal's prototype is just, well, Object.
对象字面量的原型就是 Object。


