Connecting Tech Pros Worldwide Help | Site Map

function object and prototype.

gg9h0st
Guest
 
Posts: n/a
#1: Jan 25 '07
function aa() {};
var bb = new aa();

var dd = new function cc() {};

aa.prototype.rr = 100;
cc.prototype.rr = 100;

---------------------------------------------------------------

i wrote the code above that makes two functions and two object for
those.

bb(object)->aa(function),
dd(object)->cc(function).

i think it's all the same way to make an object for a function.

but if u try to debug,

bb object has rr right after "aa.prototype.rr = 100" statement
but dd deosn't have rr variable.

isn't dd obj refer to cc?

can anyone explain it why?

thanks in advance.

VK
Guest
 
Posts: n/a
#2: Jan 25 '07

re: function object and prototype.




On Jan 25, 9:29 am, "gg9h0st" <mn9h...@hotmail.comwrote:
Quote:
function aa() {};
var bb = new aa();
>
var dd = new function cc() {};
>
aa.prototype.rr = 100;
cc.prototype.rr = 100;
>
---------------------------------------------------------------
>
i wrote the code above that makes two functions and two object for
those.
>
bb(object)->aa(function),
dd(object)->cc(function).
>
i think it's all the same way to make an object for a function.
No it is not. The second way is a rarely fustified trick (sometimes for
funny singletons maybe) - for the time being just pretend it doesn't
exist and use the conventional JavaScript object model.

More details can be found at
<http://groups.google.com/group/comp.lang.javascript/msg/34c45102bf9b5a8c>,
also you may read the entire thread as well.

RobG
Guest
 
Posts: n/a
#3: Jan 25 '07

re: function object and prototype.




On Jan 25, 4:29 pm, "gg9h0st" <mn9h...@hotmail.comwrote:
Quote:
function aa() {};
var bb = new aa();
>
var dd = new function cc() {};
Safari throws a parse error here and script execution stops.

Quote:
>
aa.prototype.rr = 100;
cc.prototype.rr = 100;
If browsers get past where Safari errors, they will throw one at this
point: window.cc is not defined, attempting to access its prototype
property is doomed to failure.

The reference provided by VK is a good one.

Quote:
i wrote the code above that makes two functions and two object for
those.
>
bb(object)->aa(function),
dd(object)->cc(function).
>
i think it's all the same way to make an object for a function.
It isn't.

Quote:
>
but if u try to debug,
>
bb object has rr right after "aa.prototype.rr = 100" statement
but dd deosn't have rr variable.
Because an rr property was never added to dd or any object in its scope
chain.
Quote:
>
isn't dd obj refer to cc?
dd refers to a function object constructed using the anonymous object
passed to the new operator (except in Safari :-) ). The anonymous
function named cc has probably already ceased to exist.


--
Rob

gg9h0st
Guest
 
Posts: n/a
#4: Jan 26 '07

re: function object and prototype.




On 1¿ù25ÀÏ, ¿ÀÈÄ3½Ã29ºÐ, "gg9h0st" <mn9h...@hotmail..comwrote:
Quote:
function aa() {};
var bb = new aa();
>
var dd = new function cc() {};
>
aa.prototype.rr = 100;
cc.prototype.rr = 100;
>
---------------------------------------------------------------
>
i wrote the code above that makes two functions and two object for
those.
>
bb(object)->aa(function),
dd(object)->cc(function).
>
i think it's all the same way to make an object for a function.
>
but if u try to debug,
>
bb object has rr right after "aa.prototype.rr = 100" statement
but dd deosn't have rr variable.
>
isn't dd obj refer to cc?
>
can anyone explain it why?
>
thanks in advance.
thanks VK and RobG.

the reference is greate one for me :)

Closed Thread