This code works fine, although what I'm interested in knowing is, if there's a way that works better and is which I prefer better, which it seem to not be working. Is it possible to do it that way, check the second code example in order to comprehend, what I have in mind when I say "preferred way".
code 1
function a(y){this.b=y;};
var ax = new a("oka");
alert(ax.b);
code 2 (preferred way but does not work)
function a(){this.b = alert(y);this.y = y;}
var ax = new a();
ax.y="okay";
ax.b;
Your use of
this
is mostly fine, but the problem is that this line:...calls
alert
and assigns its return value tob
. If you wantedb
to be a function, you'd do:...so:
Side note: The overwhelming convention in JavaScript is to give constructor functions (functions you call via
new
) names starting with a capital letter. SoA
rather thana
, for instance.