Class#create and throw new Error()

4 views
Skip to first unread message

Ilya Furman

unread,
Nov 6, 2009, 11:23:05 AM11/6/09
to Prototype: Core
Hello!

I'm wondering, why class instance is being created while
initialization method throws an error?

var C = Class.create({
initialization : function() {
throw new Error('Hey, you can\'t have any instances of this
class!')
},
foo : 'foofoo' ,
bar : function() {
alert(this.foo)
}
});

var c = new C();
c.bar();

Demo link http://jsbin.com/inoqi

Variable c should be 'undefined' I suppose, without any methods and
properties.
Seems like there is actually no way to stop instance from being
created.

T.J. Crowder

unread,
Nov 7, 2009, 9:44:23 AM11/7/09
to Prototype: Core
Hi,

The initializer is called "initialize", not "initialization" (see the
docs[1]). Your code throwing the exception isn't being called, hence
the exception not being thrown. If you use the correct name, the
exception is thrown and correctly propagates out to the code creating
the object.

[1] http://api.prototypejs.org/language/class.html

HTH,
--
T.J. Crowder
Independent Software Consultant
tj / crowder software / com
www.crowdersoftware.com

On Nov 6, 4:23 pm, Ilya Furman <smashl...@gmail.com> wrote:
> Hello!
>
> I'm wondering, why class instance is being created while
> initialization method throws an error?
>
> var C = Class.create({
>   initialization : function() {
>     throw new Error('Hey, you can\'t have any instances of this
> class!')
>   },
>   foo : 'foofoo' ,
>   bar : function() {
>     alert(this.foo)
>   }
>
> });
>
> var c = new C();
> c.bar();
>
> Demo linkhttp://jsbin.com/inoqi

Ilya Furman

unread,
Nov 7, 2009, 7:40:44 PM11/7/09
to prototy...@googlegroups.com
Oops, my bad.

Ilya Furman
Reply all
Reply to author
Forward
0 new messages