How to avoid using "this" in Javascript prototypes

user14742

Here's my javascript object, I would like to know how to avoid using "this" so many times in prototype. I know there is lot of theory and links for prototypal inhericance and this has probably been answered already, but as I haven't been able to make all ends meet, I thought this may be worth another question.

function shape(smth) {
    this.a = smth
    this.b = 2
    this.c = 3
}

shape.prototype.doCalculus = function () {
    return this.a * this.b + this.c - (2 * (this.b + this.c) + this.a);
}

module.exports = shape
jfriend00

If you want public members of an object, they MUST be referenced from the this pointer. That's how OO works in Javascript. No alternative.

If you have lots of references to the same variable within a function, you can temporarily put it in a local variable just to save some reference logic (same as with any multiple step reference), but you will still have to initially retrieve using this.varName.


There is a scheme that uses "private" member variables in a constructor closure and does not use the prototype that can be used in some situations and this allows you to refer to the variables directly without this use of this:

function shape(smth) {
    var a = smth,
        b = 2,
        c = 3;

    this.doCalculus = function() {
        return a * b + c - (2 * (b + c) + a);
    }
}

module.exports = shape

For object types where you create lots of instances, this may consume a bit more memory because methods are not stored on a shared prototype, but are created separately for each instance. There are those who argue the difference in memory consumption is immaterial in most uses.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to avoid using "this" in Javascript prototypes

From Dev

using a group of functions as a prototypes in javascript

From Dev

Understanding how JavaScript Prototypes work

From Dev

Is it possible to using Javascript prototypes without using the "new" keyword?

From Dev

Elegant callback binding when using promises and prototypes in javascript

From Dev

How to avoid executing javascript while using Kendo Templates in grids

From Dev

How to avoid for-if-else hell in n iterations using JavaScript

From Dev

How to avoid inline javascript?

From Dev

How to Avoid HTML in JavaScript

From Dev

How to avoid inline javascript?

From Dev

How to avoid asyncronism in javascript?

From Dev

JavaScript functions and prototypes

From Dev

Understanding of JavaScript Prototypes

From Dev

Javascript nested prototypes

From Dev

Constructor and prototypes in javascript

From Dev

Safely inheriting prototypes in JavaScript

From Dev

Prototypes and property inheritence in JavaScript

From Dev

Prototypes issue in javascript

From Dev

JavaScript prototypes: replace a function

From Dev

Javascript nested prototypes

From Dev

Safely inheriting prototypes in JavaScript

From Dev

Prototypes and usage Javascript

From Dev

How to avoid using null?

From Dev

How to avoid using break

From Dev

How to avoid using null?

From Dev

How Enumerate the Prototypes of Array

From Dev

How to avoid unused variables with JavaScript?

From Dev

How to avoid unused variables with JavaScript?

From Dev

How to avoid the `this` and `new` keywords in Javascript?