Re: [closure-compiler-discuss] JSC_INEXISTENT_PROPERTY when new-ing up a Function that returns an object

40 views
Skip to first unread message

Ilia Mirkin

unread,
Aug 29, 2012, 2:46:59 PM8/29/12
to closure-comp...@googlegroups.com
I get the same error when just running the javascript directly... I'm
pretty sure that scheme just doesn't work at all in JS.

var x = new y(arg1, arg2); is roughly equivalent to:

var x = {};
for (property in y.prototype):
x[property] = y.prototype[property];
y.call(x, arg1, arg2);

At no point is the return value of y considered. [And yeah, I'm sure
there's various subtleties the above approximation of 'new' misses...
it's just there for illustration purposes.]

Perhaps you meant to say

var v = Vehicle('Bob')?

That should work fine.

On Wed, Aug 29, 2012 at 2:28 PM, Mick Hakobyan <mick.h...@gmail.com> wrote:
> I like using following syntax when creating newable function in JavaScript,
> and closure complier doesn't seem to understand this... and I get a lot of
> JSC_INEXISTENT_PROPERTY.
>
> Is there a quick fix for this?
>
> /**
> * @constructor
> */
> function Vehicle(name){
> // public API
> return {
> say: _say,
> sayVrooom: _sayVrooom
> };
>
> // privat implementation
> function _sayVrooom(){
> _say('Vrooom');
> };
>
> function _say(what){
> console.log(what + ' by ' + name);
> };
> };
>
> var v = new Vehicle('Bob');
> v.sayVroom();
> v.sayVroom('yey');
>
> var v2 = new Vehicle('Bazooka');
> v2.sayVroom();
> v2.say('hi');
>
> ----
>
> JSC_INEXISTENT_PROPERTY: Property sayVroom never defined on Vehicle at line
> 22 character 0
> v.sayVroom();
> ^
> JSC_INEXISTENT_PROPERTY: Property sayVroom never defined on Vehicle at line
> 23 character 0
> v.sayVroom('yey');
> ^
> JSC_INEXISTENT_PROPERTY: Property sayVroom never defined on Vehicle at line
> 26 character 0
> v2.sayVroom();
> ^
> JSC_INEXISTENT_PROPERTY: Property say never defined on Vehicle at line 27
> character 0
> v2.say('hi');
> ^
Reply all
Reply to author
Forward
0 new messages