-
Notifications
You must be signed in to change notification settings - Fork 513
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Feature request] Instatiate JS classes with ()
, and depreciate .new()
#2513
Comments
Hi Denis, This is already how it works; if you use a JS "class" (defined with the JS keyword <script type="text/python" debug="2">
from browser import window
a = window.A(5)
print(a.x) # 5
b = window.A.new(6)
print(b.x) # 6
</script>
<script>
class A{
constructor(x){
this.x = x
}
}
window.A = A
</script> To detect a JS "class", the test is not the one you report, but another one in the same stackoverflow thread, testing if the result of Line 650 in 86b3381
|
Hum, something is strange. Last month, somebody tried to instantiate a JS I just tested in the editor : from browser import window
Blobby = window.Blob(['test'], {'type':'text/plain'} ) Gives :
It seems there is a bug.
With my method, it gives : Can you try out my method to see if everything work with it ? |
This function But if we do so, we would also translate Javascript's from browser import window
print(window.Date()) # string
print(window.Date.new()) # Javascript Date object My conclusion is that it's better to keep it simple : in Brython, use method |
I am always reminded how JS is a dirty language... Here 2 suggestions below :
from browser.javascript import new
new(klass, args) # would do klass.__new__(klass, args) + klass.__init__(klass, args) ? The advantage is this would work on any classes (JS or Python), which could be useful in some code where we only know the class we will build at runtime. |
Another example of Javascript inconsistencies : Regarding your suggestions:
|
I hate JS so much... xD
Then how about an hint if an exception is raised like :
It might be the case if you dev some kind of generic factory for some reasons.
True. |
Hi,
Currently Brython has difficulties to distinguish functions and classes, requiring us to write
Klass.new()
to instantiate it.However, it seems there is a way to detect whether a variable is a function or a class :
With this, it should be possible, to add a
__call__()
on JS classes enabling to instantiate them, i.e. being able to writeKlass()
.isClass()
may returnfalse
on some native classes. However, native classes can be instantiated without usingnew Klass()
.Implemented classes with functions is nowadays not that frequent. We could deprecate
.new()
, but keep it when an explicit usage ofnew
is required ?EDIT: I though I already talked about it here, but I couldn't find it anymore. I may have talked about it only on the google group.
The text was updated successfully, but these errors were encountered: