2

Javascriptのクラスシステムに次のアイデアを使用したいと思います。

var base = Object.create(
  Object.prototype,
  {
    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();

しかし、firebug と chromium のデバッガーとコンソールは、すべてのインスタンスをオブジェクトと呼びます。気に障る。どうすればこれを修正できますか?

4

1 に答える 1

1

インスタンスを作成して拡張しています-クラスではありません、独自のクラスを作成することもできます。

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
于 2011-04-08T10:27:51.203 に答える