Thoughts on Native namespaces and constructors #2204
Replies: 9 comments
|
warning! You can't really splat a constructor, I'm afraid. // var client = new Client(...args);
var client = function(func, args, ctor) {
ctor.prototype = func.prototype;
var child = new ctor, result = func.apply(child, args), t;
return (t = typeof result) == "object" || t == "function" ? result || child : child;
})(Client, args, function(){});But it's an incomplete shim and won't work with "special constructors" like Date's. |
|
Yeap, it's annoying, which is why our switch/case statement would have to cover the first x-number of args, and then resort to a faked subclass or similar. The above example code also does not take into account blocks, so if one is given to |
|
Alright, just wanted to make sure you didn't fall into the same trap I did once! |
|
Everything looks right. The only thing that comes to mind is whether Native has any constant already defined that can shadow other stuff, pretty minor though. |
|
@adambeynon I'm pretty sure |
|
@meh The |
|
@adambeynon ah yeah, forgot about those things. |
|
rest arguments allow this natively: |
|
My idea would be to leave Native as-is and deprecate it while we work on a new alternative version of Native. Below I paste my other ideas regarding a new Native module from the Opal Slack channel:
Native.for `HTMLElement` do |i|
Browser::DOM::Element.new(i)
end
Native(`document.getElementById("test")`)
|
Uh oh!
There was an error while loading. Please reload this page.
I'm working on a project using Opal, that needs Faye interaction. Faye clients need to be constructed using:
I think we need two enhancements to
Native. The first is calling#newon functions. Let's say we have already accessed and got a reference toFaye.Client, I want to be able to call#newon it:An initial thought of implementation would be:
The only concern is how to determine whether a proc/function is a constructor or not. Im not sure whether there is actually a difference in javascript. Also, the result would need to be wrapped as a
Native().The second thought I have, is accessing
Faye.Clientto begin with. I could use:.. but that is not very pretty. I also considered overriding
Native.const_missing, so I could do something like:With the eventual goal of:
@meh, @elia, thoughts?
All reactions