Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get rid of the `this` keyword in local functions?

I'm writing a small JavaScript game framework and often use objects' properties, like

this.depth = this.y;

But these this'es are quite annoying @_@. Is there a way to write just…

depth = y;

…not affecting global object?

My instances are created via two factory functions, and they make a limited list of predefined variables, so all of them have depth, y, etc. Functions are applied to instances by .apply() method, though it all may be changed.

The reason I need to omit this keyword is that the framework is designed not for me only, but for other people too. I don't need to remove this in the framework itself, but the this keyword harvests much time while coding applications based on this library. The only solution I know so far is making 'private' variables, but it makes some inconvenience for people who haven't worked with JavaScript before, and manipulating obj1 from obj2 causes making lots of anonymous functions with .apply – even more hell. So, as I can see, there is no panacea in JavaScript.

Constructors:

/*...*/
'Copy' : function (type) {
  var obj = {
    'x':0,
    'y':0,
    'xprev':0,
    'yprev':0,
    /*...*/
  };
  return obj;
},
'make' : function (type,x,y) {
  obj = ct.types.Copy(type);
  obj.x = obj.xprev = obj.xstart = x;
  obj.y = obj.yprev = obj.ystart = y;

  /*...*/

  ct.stack.push(obj);
}
/*...*/
like image 943
CosmoMyzrailGorynych Avatar asked Feb 15 '15 11:02

CosmoMyzrailGorynych


People also ask

How do I avoid this in JavaScript?

It's possible to avoid using this in the constructor, by using Object. create to create the prototype chain, then declaring your properties directly on the resulting object. This removes the need for this in your Shape function, meaning that we no longer need to call it with new either.

What is the problem with this keyword in JavaScript?

It loses context inside nested functions, it loses context in callbacks. There are many solutions for these issues : the bind() method, the that/self pattern, the arrow function. For more on how to fix this related issue issues, take a look at What to do when “this” loses context.

How does the this keyword work?

In JavaScript, the this keyword refers to an object. Which object depends on how this is being invoked (used or called). The this keyword refers to different objects depending on how it is used: In an object method, this refers to the object.

Is this keyword necessary in JavaScript?

But it is critically important for writing more advanced JavaScript code. In JavaScript, the this keyword allows us to: Reuse functions in different execution contexts. It means, a function once defined can be invoked for different objects using the this keyword.


1 Answers

Your question is hard to answer without seeing any code, but in general, "modern" JavaScript OOP based on factories and closures is less verbose and more idiomatic than the old "wannabe Java" style with new's and this'es.

Old style:

function Something() {
   this.depth = 0;
}
Something.prototype.incDepth = function() {
   this.depth++;
}
foo = new Something()

New style:

function Something() {
   var depth = 0;
   return {
      incDepth: function() {
         depth++;
      }
   }
}
foo = Something()
like image 52
georg Avatar answered Oct 20 '22 23:10

georg