0

Is there a better way to access the class level vars from the class itself from within a called function in the same class? While this works it feels like there should be a better way without instantiating a complete copy of the class. If there were a pointer/by ref concept in js, that would be better. I realize js is just a scripting language.

class Cruds {
    constructor() {
        name = 'cruds'
        this.Debug = false;
        this.useAsync = true;
    }
    ... 

    // In Cruds class
    updateListItem(webUrl, listName, itemId, itemProperties, success, failure) {
    let me = this; //My current solution 

    // Also in Cruds class 
    this.getListItemById(webUrl, listName, itemId, function (item) {
        if (me.Debug === true) {
            console.log("updateListItem: " + listName);
            console.dir(itemProperties);
        }
        // another in Cruds class 
        me.cleanProperties(itemProperties);
        ... 

My thanks in advance.

Joe Johnston
  • 2,794
  • 2
  • 31
  • 54
  • Just use an arrow function instead of a regular one. – VLAZ Sep 21 '22 at 13:14
  • `let me = this;` is already `by ref`. Also, labeling a programming language without pointers as scripting language is utterly wrong. – n-- Sep 21 '22 at 13:41
  • @n-- Thanks for taking the time to answer. The way I read the spec 'let' creates a new variable at it's scope and below and is initialized to it's value when the parser evaluates it. So if we did - let x = 1; if (x === 1) { let x = 2; console.log(x); //output: 2 } console.log(x); // output: 1 If it were by ref the last output would be 2. I was hoping that there was something like obj *ptr = &someOtherObj; Thinking about it now, I don't think there can be because js is a script vs compiled it would not have access to the memory address. At least I can't find it. – Joe Johnston Sep 21 '22 at 16:28
  • @VLAZ thanks for your time. How would the arrow function help me? According to the MDN Arrow functions don't have their own bindings to this, arguments or super, and should not be used as methods. I could certainly be overlooking something or just not know enough. :) – Joe Johnston Sep 21 '22 at 16:33
  • 1
    `function (item) {` -> `(item) => {` and the `this` inside the body of the function would be resolved lexically to the surrounding context. So, no need for `me = this` you can directly use it. [How to access the correct `this` inside a callback](https://stackoverflow.com/q/20279484) | [How does the "this" keyword work, and when should it be used?](https://stackoverflow.com/q/3127429) | [Are 'Arrow Functions' and 'Functions' equivalent / interchangeable?](https://stackoverflow.com/q/34361379) | [runnable example](https://jsbin.com/yubakof/1/edit?js,console) – VLAZ Sep 21 '22 at 16:47
  • @VLAZ the example made it very clear. And I am reading the links now. To be honest I haven't seen a reason to use the arrow functions before. I am going to dig into them and give myself another chance to improve. – Joe Johnston Sep 21 '22 at 16:56
  • @JoeJohnston If you have troubles to understand the `by value` vs `by reference` you may read about it in [this answer](https://stackoverflow.com/questions/518000/is-javascript-a-pass-by-reference-or-pass-by-value-language). To put it short, a variable initialized with an object value is just a reference to that object, furthermore, from the JavaScript engine perspective this is also partially true for primitive values. – n-- Sep 21 '22 at 20:55
  • @JoeJohnston Also, you are misusing the `script` and `compiled` words, not all compiled languages have direct memory access, think about Java and C#. This it is more about language level, JavaScript, as many other modern languages, being a high-level, interpreted language does not have direct memory access like mid-low level languages such as C/C++/Assembler. – n-- Sep 21 '22 at 21:02

0 Answers0