0

I have some object:

var book = {}
book.title = "Big bad book"
book.size = {}
book.size.width = "1 meter"
book.size.height = "2 meter"

I want to get something like this

nameof(book) // book
nameof(book.size) // book.size

or this

book.objectName // book
book.size.objectName // book.size

How can i do this?

Orange Fox
  • 147
  • 7

1 Answers1

0

The name of the object can be retrieved when using a constructor function to create the object:

function Book(){
  this.title = "Big bad book"
  this.size = {}
  this.size.width = "1 meter"
  this.size.height = "2 meter"
}

var book = new Book();
console.log(book.constructor.name);
Kevin Bowersox
  • 93,289
  • 19
  • 159
  • 189
  • Maybe it's fair to tell, that every instance of `Book` will return the same value, i.e. the name of the constructor function. – Teemu Feb 01 '14 at 16:07
  • `console.log(book.constructor.name)` **Book** , not a **book** `` `console.log(book.size.constructor.name)` **Object** , not a **book.size** – Orange Fox Feb 01 '14 at 16:16