开发者

Javascript debugger tip. Show a name for a value other than "Object"

I want to use the following idea for a class system in Javascript.

var base = Object.create(
  Object.prototype,
  {
   开发者_如何学JAVA extend: {
      value: function extend (properties) {
        var child = Object.create(this);
        for (var p in properties) {
          child[p] = properties[p];
        }
        return child;
      }
    },
    make: {
      value: function make () {
        var child = Object.create(this);
        if (child.init) child.init();
        return child;
      }
    }
});

var animal = base.extend();

var cat = animal.extend(
  {
    init: function init () {
      this.lives = 9;
    }
  }
);

var ares = cat.make();

But debuggers and consoles in firebug and chromium call every instance an Object. It's annoying. How can I fix this?


You're creating and extending instance -- not a class but You can create your own class as well.

function Cat() {
};

console.log(Cat); //output: function Cat() {...}
console.log(new Cat()); // output: Cat

console.log(Object); // output: function Object() {...}
console.log(new Object()); // output: Object
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜