0

I am running the following in node.js:

function TextCell(text) {
    this.text = text.split("\n");
}

TextCell.prototype.minWidth = function() {
   return this.text.reduce(function(width, line) {
      return Math.max(width, line.length);
   }, 0);
};

TextCell.prototype.minHeight = function() {
    return this.text.length;
};

TextCell.prototype.draw = function(width, height) {
    var result = [];
    for (var i = 0; i < height; i++) {
      var line = this.text[i] || "";
      result.push(line + repeat(" ", width - line.length));
    }
    return result;
};

function RTextCell(text) {
    TextCell.call(this, text);
}

RTextCell.prototype = Object.create(TextCell.prototype);

RTextCell.prototype.draw = function(width, height) {
    var result = [];
    for (var i = 0; i < height; i++) {
      var line = this.text[i] || "";
      result.push(repeat(" ", width - line.length) + line);
    }
    return result;
};

When I console.log(RTextCell.prototype), I get a prototype with only the draw function. Also, when I log(Object.create(Textcell.prototype)) I just get "TextCell{}". Why does it seem like the clone of the prototype is empty?

EDIT: I noticed my mistake. I made objects of type RTextCell before defining it. That's why the prototype turned out empty. Sorry for not including that part here

2
  • Your code seems to work fine to me. Remember that you're creating a new object whose prototype is the argument you passed it. If you're viewing it in Chrome, inspect the __proto__ property to see the underlying prototype. Commented Sep 29, 2016 at 22:46
  • I am running it in node.js. I get errors when trying to use functions that depend on both classes having the same functions defined Commented Sep 29, 2016 at 23:29

1 Answer 1

1

Why does it seem like the clone of the prototype is empty?

Because it has no own properties. Inherited properties don't show up in the console directly, but when you expand the object you can follow the prototype chain.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.