Skip to content

Instantly share code, notes, and snippets.

@franTarkenton
Last active August 30, 2024 23:27
Show Gist options
  • Save franTarkenton/8acfa6f256d3c8a76d33253ea9d976a8 to your computer and use it in GitHub Desktop.
Save franTarkenton/8acfa6f256d3c8a76d33253ea9d976a8 to your computer and use it in GitHub Desktop.
Stoopid Javascript

Demo what I would expect

class Demo:
    var = 10

    def add(self, addNum):
        print(f'adding {addNum} and {self.var} ')
        return self.var + addNum

class Demo2:
    def __init__(self, objref):
        var = 2
        self.objref = objref

    def increment(self):
        self.objref.var += 1

    def define_Callback(self, callback):
        self.callback = callback

if __name__ == "__main__":
    # this is the same as js / etc / most langs
    obj = Demo()
    obj2 = Demo2(obj)
    obj2.increment()

    obj2.define_Callback(obj.add)
    print('obj.var is', obj.var)
    var = obj2.callback(5)
    print('var + 5 = ', var)

outputs:

--- output ---
obj.var is 11
adding 5 and 11 
var + 5 =  16

meanwhile in crazy town...

class Demo {
    var = 10;

    add(num2Add) {
        console.log(`adding ${num2Add} to ${this.var}`);
        this.var += num2Add;
        return this.var;
    }
}

class Demo2 {
    constructor(objref) {
        this.objref = objref;
        this.var = 5;
        this.callback = null;
    }

    increment() {
        this.objref.var++;
    }

    defineCallback(callback) {
        this.callback = callback;
    }
}

var obj = new Demo();
var obj2 = new Demo2(obj);
obj2.increment();

obj2.defineCallback(obj.add);
console.log('obj.var is ' + obj.var);
var newvar = obj2.callback(5);
console.log(`var (${obj.var}) + 5 isn't ${newvar}`); // 16

Crazy town output

--- output ---
obj.var is 11
adding 5 to 5
var (11) + 5 isn't 10

Realize it is what it is, but this is mostly what screws me up

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment