What is the type of 'child'?

242 views Asked by At

I know type of Object and Array. I know how to use and how to create Object and Array but I don't know what's the type of child in JavaScript and how to create type of child?

This is a chrome console screenshot.

Chrome console 'child' object

3

There are 3 answers

1
Alexander O'Mara On

It appears to be an instance of a custom class named child.

If you run the following code in Chrome's console:

function child() {}
new child();

You would see the same output, minus all the properties of child:

screenshot

For what it's work, normally the naming convention is to capitalize the names of constructor functions like Child, but it's not technically necessary.

0
Louis On

I concur with mu is too short that this is most likely an instance of a Backbone.Model, either directly or indirectly (through a subclass).

The reason you see child is due to Backbone's internal machinery. When you use Backbone.Model.extend, backbone sets up a constructor for the new class, and this constructor has the name child. This is readily visible in the code for the .extend method.

Getting some sort of useful identifier out of what you get at runtimme is difficult, if not downright impossible. I've taken the habit of using __classname__ as a field in which I plunk the name of the class. It is then visible on the __proto__ field of the object I'm inspecting. I use this only for debugging purposes. Here's an example at the Chrome console:

Chrome console example

0
Ferdinand Prantl On

As @mu is too short correctly guessed from looking at the object properties, it was an instance of Backbone.Model. You could create another instance of the same object (class) like this, providing you knew correct constructor arguments:

new child.constructor(...);

You could also use the model (class) in a new collection, although you had access just to a model instance:

new MyCollection = Backbone.Collection.extend({
  model: child.constructor,
  ...
});

As @Louis noted, it is not possible to get a reasonable (class) name out of an object instance, unless you put some name to the object prototype yourself. I often need some initialization code in my models and instead of putting it to the initialize method, I declare a named constructor explicitly and put the initialization code there. Like the function named MyModel here:

var MyModel = Backbone.Model.extend({
  constructor: function MyModel(attributes, options) {
    Backbone.Model.prototype.constructor.call(this, attributes, options);
    ...
  }
});

You can see the object (class) name in Chrome developer tools like this:

var Car = Backbone.Model.extend({
  constructor: function Car(attributes, options) {
    Backbone.Model.prototype.constructor.call(this, attributes, options);
  }
});

var Truck = Car.extend({
  constructor: function Truck(attributes, options) {
    Car.prototype.constructor.call(this, attributes, options);
  }
});

Truck object instance

String properties like __classname___ will be available unchanged in minified code too. Named constructors will be available only in non-minified code. If you still needed to see meaningful constructor names after minifying the code, you would have to exclude them from mangling.