title: Dependency injection in JavaScript using apply, call or bind methods
body: <p>I'm fairly new to JavaScript and have a question about unit-testing an object
  and it's methods.  <strong>Is using the JavaScript bind, apply and call methods
  considered a form of dependency injection?</strong>  I ask because I have several
  functions I want to unit-test that use the 'this' keyword and I would like to
  set the context of those functions before running a test. Should I call the
  constructor with the appropriate parameters and then unit-test those methods or
  could I use one of the apply, call or bind methods?  Example provided below:</p>
  
  <pre><code>function assert(v1, v2) {
    console.log(v1 === v2);
  }
  
  function User(firstName, lastName, identifier) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.identifier = identifier;
  }
  
  User.prototype.getUserString = function() {
    return this.lastName + ", " + this.firstName + " &lt;" + this.identifier + "&gt;";
  }
  
  // First option:
  
  var user = new User('Andrew', 'Schools', '09AF22');
  assert(user.getUserString(), 'Schools, Andrew &lt;09AF22&gt;');
  
  // Second option:
  
  var getUserString = User.prototype.getUserString.bind({
    'lastName': 'Schools',
    'firstName': 'Andrew',
    'identifier': '09AF22'
  });
  
  assert(getUserString(), 'Schools, Andrew &lt;09AF22&gt;');
  </code></pre>
  
  <p>My concern with option one is that the own properties can be changed by other methods after they are invoked thus failing my tests.  I guess I could always reset the variables before every test but that's extra work, especially if the object has many own properties.  <strong>Is there any issues with using the apply, call or bind methods when unit-testing?</strong></p>