ga-wdi-boston / js-function-context-this

Other
0 stars 126 forks source link

Add notes on how to read method calls #5

Open jrhorn424 opened 9 years ago

jrhorn424 commented 9 years ago
  1. "Method is called upon object"
  2. "Message sent to object"
  3. "Object calls method"
  4. "Object receives method"

Makes calling context clear.

RealWeeks commented 8 years ago

Added and will use this terminology. "Message sent to object" is there a better way to state this?

jrhorn424 commented 8 years ago

Nothing occurs to me. That's the terminology I've used and other use as well. @gaand any suggestions?

I think 3 is technically wrong from a pure OOP standpoint, but I've heard it used.

RealWeeks commented 8 years ago

This was added in a previous commit. Please don't close, this commit is useful in preparation.

MicFin commented 7 years ago

@jrhorn424 @gaand asks "Method is called with object as context"?

MicFin commented 7 years ago

I believe we are referencing the Here we would say sections of the The Four Patterns of Invocation (snippets shown below). I also like the phrasing by @raq929 suggested here that uses the names of the functions and used in example 3 below. The Here we should say phrasing should be consistent though and currently it is not.

1. Function Invocation Pattern

const goBoom = function() {
    console.log('this is ', this);
}
goBoom();

Here we would say "A method is called on an object". In this case the object is the window.

2. Method Invocation Pattern

let deathstar = {
    goBoom: function() {
      console.log('this is ', this);
  }
};

deathstar.goBoom();

Here we would say "Method is called with object as context"

3. Call/Apply Invocation Pattern

const goBoom = function () {
  console.log("this refers to ", this);
};
let deathstar = {
  weapon: 'Planet destroying laser'
};
goBoom.call(deathstar);

Here you would say "Call the function goBoom with deathstar as the context (this)"

4. Constructor Invocation Pattern

const Deathstar = function (weapon) {
  console.log("this is ", this);
  this.emporer = "Darth Sidius";
  this.weapon = weapon;
  this.whatIsThis = function(){
    console.log("Inside whatIsThis, this is ", this);
  };
  console.log("this is ", this);
};

let thatsNoMoon = new Deathstar('Mega giant huge laser');
let endor = new Deathstar('Happy little Ewoks');

Here we would say "the object receives the method".

raq929 commented 7 years ago

This was partially but not fully addressed for 016.