jsx / JSX

JSX - a faster, safer, easier JavaScript
http://jsx.github.io/
MIT License
1.47k stars 102 forks source link

Feature to bind JavaScript classes loaded by CommonJS-like require() to JSX native classes. #152

Open gfx opened 11 years ago

gfx commented 11 years ago

JSX lacks a way to apply "new" operator to classes loaded by CommonJS-like require(). Thus, we have to use js.eval() in some cases; it may unexpectedly bind local variables like$__jsx_extend` and also could affect performance.

The syntax may be something like this (not perfect, though):


native("require('es5-shim')"); // es5-shim.js changes the global environment

// for nodejs modules
native("require('fs')") class fs { ... }

native("require('http')") class http {
    class Server { ... } // http.Server
}

// for 3rd party modules
native("require('source-map').SourceMapGenerator") SourceMapGenerator { ... }

This issue is a part of #4.

kazuho commented 11 years ago

native("require('fs')") class fs { ... }

The syntax seems intuitive to me and I am on to the idea.

OTOH I do not see any merit in introducing

native("require('es5-shim')");

Can't es5-shim.js be loaded separately (at JavaScript level)?

gfx commented 11 years ago

Ah, es5-shim.js should be loaded by <script>; however, native("..."); will be useful to inject so-called polyfills in the local scope.

For example taken from https://gist.github.com/7shi/5487552 :

native("var HTMLDocument = typeof(HTMLDocument) == 'undefined' : window.Document : window.HTMLDocument"); // for IE10

However, this is a completely separated idea from loading CommonJS-like modules and needs carefully considering.

kazuho commented 11 years ago

@gfx I agree that it would be great if we could fix the issue pointed out by the gist (though I am not sure how we should do), and that it should be handled as a separate issue. Please open a separate issue regarding the problem.

kazuho commented 11 years ago

Reopening to reconsider the native("...") notation. Please see the discussion at https://github.com/jsx/JSX/issues/154#issuecomment-17484849