javascript - understanding a simple return this in transit.js -


i relatively new js , came across following function in transit.js, small animation library. have @ function below:

function transform(str) {     if (typeof str === 'string') { this.parse(str); }     return this;   } 

now this.parse(str), calling fucntion attached prototype of transform, can see function on line 422.

now question here this, have read few articles online, show how return can used chaining, not sure in instance return returning.

when return this in specific scenario, mean return? exactly.

i kind of understand return working bit so:

  • you pass object, variable or function
  • the function executes steps
  • you return new object or variable using return this.

but still not clear, can make crystal clear me, taking example , explaining using examples context?

p.s. not asking explain return asking explain return in given context.

the this.parse(str) mutates object , returns mutated result. useful, said, chaining function calls so:

var obj = transform('some string').someotherfunction(); 

someotherfunc may defined somewhere in prototype chain, or using kind of inheritance strategy, may inherited or extended function of "parent" object. point when someotherfunc called, called on object altered transform function.


Comments

Popular posts from this blog

javascript - Google App Script ContentService downloadAsFile not working -

javascript - Function overwritting -

php - Find a regex to take part of Email -