Why is flowerPot anonymous while tumbler is named?

时间:2016-02-03 04:13:49

标签: javascript

Title says it all. Here's the fiddle https://jsfiddle.net/rohandeshpande/m1whyq8r/

Code as well, tested in Chrome.

var me = new room.Person('rohan');

room['Pet'] = function (type) {
    this.type = type;
}

console.log(me);

var batman = new room.Pet('dog');

console.log(batman);

// Name will get passed as a string
var make = function (Name) {
    room[Name] = function (arg) {
   this.prop = arg;
  }

  return room[Name];
};

var makeGlass = function () {
 room['Glass'] = function (arg) {
    this.prop = arg;
 }
}

var vase = make('Vase');

var flowerPot = new room.Vase('clay');

// Will log an anonymous class
console.log(flowerPot);

makeGlass();

var tumbler = new room.Glass('glass');

// Will log a named class
console.log(tumbler);

The context for which I will use this is for dynamically creating named constructors that can be logged correctly for debugging rather than seeing generic class names in the log.

0 个答案:

没有答案