Jim Girard
Jim Girard

Reputation: 11

Javascript Sub function in a Prototype function

I need to extend a string writer function which houses a heading and body. The body can have multiple appended lines to it before the entire "string" is rendered. Then a new object is referenced. My problem here is that when I call the sub function of the prototype, the body is not accessible to overarching function. I have to be missing something simple....thoughts? Thanks

https://jsfiddle.net/hitman1733/n2od4s6s/

 var StringBuilder = function() {
    this.Heading;
  this.Body;
}

StringBuilder.prototype.Heading = function(text){
    this.Heading = text + '\n';
}

StringBuilder.prototype.Item = function()
{
    this.add = function(field, text) {
            this.Body = '     ' + field +': ' + text + '\n';
    }
}

StringBuilder.prototype.Append = function() {
    return this.Heading + '\n' + this.Body;
}

var Primary = new StringBuilder();
Primary.Heading = 'My Heading';

var privalue = new Primary.Item();
privalue.add('bob', 'smith');
privalue.add('sally', 'smith');

console.log(Primary.Append());

Upvotes: 1

Views: 151

Answers (2)

Matt D. Webb
Matt D. Webb

Reputation: 3314

The use of new Primary.Item() isn't quite right, I have amended this to work a little more consistently:

const StringBuilder = function() {
    this.items = [];
    this.heading = "";
    this.body = "";
}

StringBuilder.prototype.heading = function(text) {
  this.heading = text;
}

StringBuilder.prototype.body = function(text) {
    this.body = text;
}

StringBuilder.prototype.addItem = function(field, text) {
    this.items.push(`${field} : ${text}`);
}

StringBuilder.prototype.append = function() {
  // note: using breaks only in the append method.

  this.body += `\n${this.items.join('\n')}`; // join items to existing body.

  return `${this.heading}\n${this.body}`;
}

const primary = new StringBuilder();

primary.heading = "My Heading";
primary.body = "My Body";
primary.addItem('bob', 'smith');
primary.addItem('sally', 'smith');

console.log(primary.append());

Here is a JsFiddle

Upvotes: 1

guest271314
guest271314

Reputation: 1

Not certain what the purpose of .Item function is? Would suggest adjusting names to lowercase where property is not a function, return this from Item call. Alternatively, simply call Primary.add() and omit .Item function

var StringBuilder = function() {
  this.heading = "";
  this.body = "";
}

StringBuilder.prototype.Heading = function(text) {
  this.heading = text + '\n';
}

StringBuilder.prototype.Item = function() {
  return this
}

StringBuilder.prototype.add = function(field, text) {
    this.body += '     ' + field + ': ' + text + '\n';
  }

StringBuilder.prototype.Append = function() {
  return this.heading + '\n' + this.body;
}

var Primary = new StringBuilder();
Primary.heading = 'My Heading';

// var privalue = Primary.Item(); // necessary?
// Primary.add('bob', 'smith');
// Primary.add('sally', 'smith');

var privalue = Primary.Item();
privalue.add('bob', 'smith');
privalue.add('sally', 'smith');

console.log(Primary.Append());

Upvotes: 0

Related Questions