From: Thomas 'PointedEars' Lahn on
David Mark wrote:

> Asen Bozhilov wrote:
>> Before few weeks ago Garret Smith payed attention for "Conditional
>> statement with an assignment expression":
>> <URL:http://groups.google.bg/group/comp.lang.javascript/msg/
>> 9299befb874eb9a0>
>>
>> He maintain theory for readable of code in statements like this one:
>>
>> var x;
>> if (x = function(){})
>> {
>> x();
>>
>> }
>>
>> This is *complete* valid and *safe* approach:

It is probably safe as long as `x' does not refer to a host object prior to
assignment.

> Sure, it is technically sound. But it is considered bad form (style)
> in that it can be mistaken for a test rather than an assignment. You
> have to consider the programmer that comes next.

It has been BCP since JavaScript 1.2 (Netscape Navigator 3.0) that
assignments be only used in conditional expressions where necessary, and
then only parenthesized (to work around the peculiarity in JavaScript 1.2
that the result of unparanthesized assignments was not the result of the
evaluation of the right-hand side but the boolean success status of the
assignment.¹)

So this should be at least

var x;
if ((x = function() {}))
{
x();
}

but, since

var x = function() {};
if (x)
{
x();
}

suffices, the latter should be preferred here. (Nota bene: JavaScript 1.2
does not support anonymous function expressions.)

That is different, for example, with

var x, y, z;

/* conditionally assign an object reference to x */

if (x && (y = x.p) && (z = y.q) && z.f)
{
z.f();
}

where as an optimization we can save the assignments to `y' and `z' if `x'
converts to `false' and access to `x.p' is likely to throw an exception,
and the assignment to `z' if `x.p' converts to false and `y.q' is likely to
throw an exception. This would otherwise require several (nested) `if'
statements.

That said, a type-converting test is usually insufficient to determine
whether it is safe that a property be [[Call]]ed, and it is error-prone
with host objects. Cf. several user-defined approaches of isMethod(),
isHostMethod(). And isNativeMethod(), IIRC. (Unfortunately, ES5 does not
appear to have managed specifying a *public* `IsCallable' method that can
be implemented so that it could be used in a backwards-compatible way.)


PointedEars
___________
¹ This is connected with the deprecation of the `language' attribute
of the SCRIPT element in HTML 4.01, where specifying e.g.
`language="JavaScript1.2"' changed program behavior in a
non-interoperable way.
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm> (404-comp.)
From: "Michael Haufe ("TNO")" on
On Dec 30, 7:47 pm, David Mark <dmark.cins...(a)gmail.com> wrote:
> On Dec 30, 8:36 pm, Asen Bozhilov <asen.bozhi...(a)gmail.com> wrote:
>
> > Before few weeks ago Garret Smith payed attention for "Conditional
> > statement with an assignment expression":
> > <URL:http://groups.google.bg/group/comp.lang.javascript/msg/
> > 9299befb874eb9a0>
>
> > He maintain theory for readable of code in statements like this one:
>
> > var x;
> > if (x = function(){})
> > {
> >   x();
>
> > }
>
> > This is *complete* valid and *safe* approach:
>
> Sure, it is technically sound.  But it is considered bad form (style)
> in that it can be mistaken for a test rather than an assignment.  You
> have to consider the programmer that comes next.

Proper style dictates that an extra pair of parenthesis is required of
course:

var x;
if((x = function(){})){
x();
}

strict mode (in Fx at least) will throw a warning if its left out:
http://thenewobjective.com/temp/aWarning.html
From: Asen Bozhilov on
Thomas 'PointedEars' Lahn wrote:

> It has been BCP since JavaScript 1.2 (Netscape Navigator 3.0) that
> assignments be only used in conditional expressions where necessary, and
> then only parenthesized (to work around the peculiarity in JavaScript 1.2
> that the result of unparanthesized assignments was not the result of the
> evaluation of the right-hand side but the boolean success status of the
> assignment.)

Thanks for your response and useful information about JavaScript 1.2
and Netscape Navigator 3.0. I have Netscape Navigator 3.0 on my local
machine. There are no distinct between parenthesized and
unparanthesized assignments.

function f(){}

window.alert(f.length); //0
if ((f.length = true))
{
window.alert(f.length); //0
}

window.alert(f.length = 10); //10
window.alert(f.length); //0

Only difference is:

var x = null;
if (x = true);

| Error: test for equality (==) mistyped as assignment (=)?



From: Christian Kirsch on
Thomas 'PointedEars' Lahn schrieb:

> It has been BCP since JavaScript 1.2 (Netscape Navigator 3.0) that

Bad coding practice?
Best coding practice?
Brutally concocted proposition?

I really don't mind acronyms as long as they are commonplace. But since
you prefer "getElementById" over "$" in JavaScript, why not take the
time to spell out what you mean in human readable text?

BTW: The first hit google offers for BCP is "Best corporate publishing".

From: JR on
On 1 jan, 13:33, Christian Kirsch <c...(a)bru6.de> wrote:
> Thomas 'PointedEars' Lahn schrieb:
>
> > It has been BCP since JavaScript 1.2 (Netscape Navigator 3.0) that
>
> Bad coding practice?
> Best coding practice?
> Brutally concocted proposition?
>
> I really don't mind acronyms as long as they are commonplace. But since
> you prefer "getElementById" over "$" in JavaScript, why not take the
> time to spell out what you mean in human readable text?

ITA :D

> BTW: The first hit google offers for BCP is "Best corporate publishing".

BCP == "Best Current Practice", according to:
http://encyclopedia.tfd.com/BCP

--
JR