简体   繁体   English

JavaScript的对象文字与控制台记录时似乎命名的对象有什么区别?

[英]What is the difference between JavaScript's object literals and object that appears to be named when console logged?

I have noticed there are times when I console.log an object, I get the object literal and sometimes its seems what is console.logged is prefixed with a name. 我注意到有时候我在console.log中有一个对象,我得到了对象的文字,有时它似乎是什么是console.logged以前缀为名称。

For example, If I console.log an object before sending it to the server I get: 例如,如果我在将对象发送到服务器之前调试对象,我得到:

{id: 18, date: "2017-09-13T21:59:59.999Z"...etc}

but when I console the same log returned as a promise from a server call, I get: 但是当我通过服务器调用控制返回的相同日志作为承诺时,我得到:

Resource {id: 18, date: "2017-09-13T21:59:59.999Z"...etc}

What causes this differences? 是什么导致这种差异? and What are the differences between what seems to be two different representations of an object? 什么似乎是一个对象的两个不同表示之间的差异?

它们有一个constructor属性,它不指向Object ,但在这种情况下指向Ressource

The console is giving you hints, it doesn't always log stuff as is. 控制台给你提示,它并不总是按原样记录。

For example: 例如:

var Cat = function (name) {
    this.name = name;
}

var paws = new Cat('paws');
console.log(paws);

will act similar to Resource in your example. 在您的示例中将与Resource类似。 The console is hinting at the constructor . 控制台暗示了constructor

The latter is an instance of a named class: 后者是命名类的实例:

class Resource {
   constructor() {
      this.id = ...;
      this.date = ...;
   }
}

Or a named constructor: 或者命名的构造函数:

function Resource() {
  this.id = ...;
  this.date = ...;
}

In both cases, the class or constructor in instantiated with the new keyword, but class is the newer ES6 syntax. 在这两种情况下,使用new关键字实例化的类或构造函数,但class是较新的ES6语法。

console.log(new Resource())

Your first example is simply a plain object with no constructor. 您的第一个示例只是一个没有构造函数的普通对象。

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

相关问题 JavaScript 中的对象、Object 文字和模板文字有什么区别 - What is the difference between Objects, Object Literals and Template Literals in JavaScript 这些对象字面量有什么区别? - What is the difference between these object literals? JavaScript中的“节点”和“对象”有什么区别? - What's the difference between “node” and “object” in JavaScript? JavaScript 对象和 OO/UML/Java 对象有什么区别? - What's the difference between a JavaScript object and an OO/UML/Java object? Javascript对象和JSON对象之间有什么区别 - What's the difference between Javascript Object and JSON object JSON和JavaScript对象文字中的有效名称之间的区别 - Difference between valid names in JSON and JavaScript object literals 在对象的属性和数组Javascript之间进行迭代有什么区别 - What is the difference between iterating between an object's properties and an array Javascript javascript中函数和对象的本质区别是什么? - What's substantive difference between function and Object in javascript? 对象的范围与javascript中的上下文有什么区别? - What is the difference between an object's scope and its context in javascript? 除了.length属性外,JavaScript Array和Object有什么区别? - What's the difference between JavaScript Array and Object except .length property?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM