Trouble when extending any JS extern

109 views
Skip to first unread message

oleg karasev

unread,
Sep 15, 2013, 7:41:16 AM9/15/13
to haxe...@googlegroups.com
I got "Uncaught TypeError: Cannot read property 'prototype' of undefined " when extending any JS-written class.
For testing purposes i try to extend jquery class in new project:

class Main extends JQuery{
static function main()  {}
}

it compiles OK, but after run it causes error.


this was only example. The main goal is to extend custom js library in my haxe project.

Juraj Kirchheim

unread,
Sep 15, 2013, 8:06:59 AM9/15/13
to haxe...@googlegroups.com
That because at that point in time js.JQuery is undefined. That's
because it gets defined here:
https://github.com/HaxeFoundation/haxe/blob/development/std/js/JQuery.hx#L396

And __init__ functions are run *after* all class definitions are booted.

You should not extend JQuery. Use plain old composition or `abstract`
or `using` and you'll be fine.

Regards,
Juraj

oleg karasev

unread,
Sep 15, 2013, 8:48:49 AM9/15/13
to haxe...@googlegroups.com
I am new in haxe. Can you explain what is the "plain old composition"? 
The JQuery was only for example. I need to extend my custom JS class in my project.
For example: 
extern class my_custom_js_class{
 method1.....
 method2.....
 method3.....
}

how to extend it in my haxe project?

воскресенье, 15 сентября 2013 г., 16:06:59 UTC+4 пользователь back2dos написал:

oleg karasev

unread,
Sep 15, 2013, 10:14:10 AM9/15/13
to haxe...@googlegroups.com
i had tested this solutions and all of them are unsuitable
1) "using" and "abstract" classes cannot be extended, but there are some of "childs" in my project:

class child1 extends my_extern_js_class
class child2 extends child1
class child 3 extends child2

2) composition.... hmmm.. wrappers? Bad solution.

Is there any "fine" way to init JS *before* extending?


oleg karasev

unread,
Sep 15, 2013, 11:03:00 AM9/15/13
to haxe...@googlegroups.com
I found it!!! Thanks very very!!!!
in "__init__" of my external was compiler directive to include my_extern_js_class.JS 
I removed that include. Then i included my_extern_js_class.JS  manually from .html *before* project .js file.
Now it works!

Your answer about __init__ was very helpfull, thanks a lot. And excuse for my bad english :)

Juraj Kirchheim

unread,
Sep 15, 2013, 5:34:07 PM9/15/13
to haxe...@googlegroups.com
On Sun, Sep 15, 2013 at 4:14 PM, oleg karasev <okar...@gmail.com> wrote:
> i had tested this solutions and all of them are unsuitable
> 1) "using" and "abstract" classes cannot be extended, but there are some of
> "childs" in my project:
>
> class child1 extends my_extern_js_class
> class child2 extends child1
> class child 3 extends child2

What I mean is something like this:

extern class Foo { ... }

class FooMagic {
static public function superFoo(foo:Foo, x, y, z) { ... }
}

using FooMagic;

var f:Foo = ...;
f.superFoo(1, 2, 3);

> 2) composition.... hmmm.. wrappers? Bad solution.

More often than not, it's the best solution. But let's not get started on that.

As long as things are working for you, all is well :)

Regards,
Juraj
Reply all
Reply to author
Forward
0 new messages