1

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.

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?

1
  • The latter is a class instance. You can check this with myObject instanceof Resource. Commented Sep 14, 2017 at 16:32

3 Answers 3

1

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.

console.log(new Resource())

Your first example is simply a plain object with no constructor.

Sign up to request clarification or add additional context in comments.

Comments

1

They have a constructor property, that does not point to Object, but to Ressource in that case.

Comments

1

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. The console is hinting at the constructor.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.