JSFiddle - React, Tailwind, and code Playground

by jfriend00

JavaScript

function Person() {
  this.full_name = "Bruce Wayne";
  this.last_name = null;
  this.orig_last_name = null;
}

Person.prototype.updateLastName = function() {
  // "this" is the Person object.
  console.log("in updateLastName()", this);
  this.last_name = this.full_name.replace(/\s+\S+$/g, this._replace_last_name.bind(this));
}

Person.prototype._replace_last_name = function(s) {
  // "this" is now the Window object.
  console.log("in _replace_last_name()", this);
  this.orig_last_name = s;
  this.last_name = " Banner";
  return this.last_name;
}

var p1 = new Person();
p1.updateLastName();
console.log(p1.full_name, p1.last_name, p1.orig_last_name);