0

Similar to how you can call __init__ on anything in python

class Class():
    def __init__(self, a):
        self["a"] = a
dic = { "a": 0 } # create dict
Class.__init__(dic, 5); # "init" the dict
print(dic) # value "a" changes

How would you call constructor on anything in javscript (ie change this, before calling the constructor) The old way was apparently to use .call

<class>.call(<new this>, <arguments>)

However this no longer, works giving an error:

Class constructor <class> cannot be invoked without 'new'

I have thought of a way to do this by creating the class normally with new then copying the key, pair values to this, but that seems incredibly slow. I'm trying to get PhysiJS to run but it's an ancient codebase, and this is one of many offending lines of code which breaks it:

THREE.Scene.call( this );
Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
SollyBunny
  • 800
  • 1
  • 8
  • 15
  • ".call" and ".apply" only workes with "function" classes rather than actuall classes, so no. This is probably why PhysiJS uses them because due to it's age, all classes back then used to be function classes, but the libraries it relies on (ammo.js or cannon.js) don't. and you deleted the comment (please leave it, because it may help someone in the future ) – SollyBunny Dec 22 '22 at 13:25
  • I'm sorry, are you writing Javascript or Python? There is no explicit `self` param, there is no dunder-init, there are no dicts, and classes must be constructed with the `new` keyword. I can't really tell what you're trying to do here. It might be better to say "here's some Python code that does what I want and here's my attempt at translating it to JS" and ask for help with that. – Jared Smith Dec 22 '22 at 13:26
  • Javascript (clearly in the tags, and I ask my question in terms of javascript), `this` is equivilant to self, `constructor` is equivilant to `__init__`, `dicts`s are equivilant to `Object`s, and only classes using the new `class` syntax require new, which is the problem. Did you read the question? @JaredSmith – SollyBunny Dec 22 '22 at 13:28
  • 1
    "only classes using the new class syntax require new" that isn't true, old pre-ES6 constructor functions would give wrong results if called without `new`. "this is equivilant to self" also not true, because in Python methods require it to explicitly be part of the signature and in JS it's implicit. "constructor is equivilant to `__init__`" that actually *is* true, but you still have to *name* the method `constructor` so that it gets called when you `new` up an object, you can't name it whatever and expect it to work. – Jared Smith Dec 22 '22 at 13:31
  • 2
    "Did you read the question? @JaredSmith" I did. I'm asking *you* if *you* understand the differences in the semantics between JS and Python, because you seem to be confused about some of the differences between the two. – Jared Smith Dec 22 '22 at 13:32
  • If converting the code from python to javascript, the __init__ would turn into constructor, self would be this (although no longer in arguments), and by a "function class" i mean a class which doesn't use the newer class syntax and instead uses a function, such as PhysiJS (wrongly) assumes. Can you atleast answer the question instead of pointing out everything wrong with it. If you want you can edit the question, because it is a very valid question and the question which has been marked duplicate with simply does not work, because what the question says is what PhysiJS already does @JaredSmit – SollyBunny Dec 22 '22 at 13:36
  • @JaredSmith _“old pre-ES6 constructor functions would give wrong results if called without `new`”_ — Not if called with `.call` or `.apply`. The fact that this isn’t possible with a `class` is the point of this question. _“and in JS it's implicit”_ — Why does this matter? There’s a [proposal](//github.com/gilbert/es-explicit-this) to allow an explicit `this` parameter. Just because it’s _currently_ implicit doesn’t mean it’s not at least analogous. – Sebastian Simon Dec 22 '22 at 14:06
  • 1
    See [How to use es6 constructor instructions with a different context](/q/54622443/4642212). – Sebastian Simon Dec 22 '22 at 14:22
  • @SollyBunny I'm not pointing out flaws in the question for fun, I'm doing it because the issues make it hard to tell what you're actually asking or where the problem lies. I'm not the one who closed it, but that does mean that I can't answer it. As far as it goes though, you cannot call a class constructor with a different context (i.e. `this`). You can however extend the class and call `super`. Unfortunately I think you're stuck: you're going to have to either update the codebase or use older versions of the libraries if you can even still find them. – Jared Smith Dec 22 '22 at 15:03
  • @SebastianSimon Although that does work, it would mean I would have to completley redo every class definition by turning the functions inside out – SollyBunny Dec 22 '22 at 16:11
  • I guess it's the best solution to an impossible problem, thanks – SollyBunny Dec 22 '22 at 16:17
  • `THREE.Scene.call( this );` *looks* a lot like it should be a `super()` call in an inherited class. Please post the entire section of the PhysiJS code (or at least link that line in their source), otherwise we cannot answer how to solve this. – Bergi Dec 23 '22 at 13:10
  • All the code in Physi.js doesn't use classes – SollyBunny Dec 23 '22 at 13:36

0 Answers0