Talk:JavaScript syntax: Difference between revisions

Content deleted Content added
Removed maintenance template—links fixed
No edit summary
 
(8 intermediate revisions by 6 users not shown)
Line 1:
{{WikiProject Computing |importance=Mid |class=C |software=y |software-importance=Mid}}
{{Talk header}}
{{WikiProject JavaScriptbanner shell|class=C|imortance=}}
{{WikiProject Computing |importance=Mid |class=C |software=y |software-importance=Mid}}
{{WikiProject JavaScript|importance=Top}}
}}
{{American English}}
{{Backwardscopy
|author = Miller, F. P., Vandome, A. F., & McBrewster, J.
Line 9 ⟶ 12:
|comments = {{OCLC|721316846}}, {{ISBN|9786130097844}}.
|bot=LivingBot
}}
{{Broken anchors|links=
* <nowiki>[[Grave accent#Use in programming|backquote]]</nowiki> The anchor (Use in programming) [[Special:Diff/1083439356|has been deleted]]. <!-- {"title":"Use in programming","appear":null,"disappear":{"revid":1083439356,"parentid":1081031463,"timestamp":"2022-04-18T20:37:32Z","removed_section_titles":["As surrogate of apostrophe or (opening) single quote","CITEREFKuhn2001","CITEREF2019","CITEREFEggert2012","Technical notes","ASCII grave","Games","Use in programming","CITEREFOdersky2011"],"added_section_titles":["Unicode"]}} -->
}}
 
== Inaccuracy (needs citation) ==
The following statement: "'''else statements must be cuddled (i.e. "} else {" , all on the same line), or else some browsers may not parse them correctly.'''" found in section 5.1 is incorrect. I have been programming in javascript for 4 years, and have never used that programming style. I have yet to notice any browser incompatibilities. If this statement is true, please provide a citation. - [[User:Kickboy|Kickboy]] 04:07, 24 August 2006 (UTC)
 
:The statement is not present anymore.
:Can we delete this block from 2006? [[Special:Contributions/87.191.37.170|87.191.37.170]] ([[User talk:87.191.37.170|talk]]) 07:26, 9 December 2022 (UTC)
 
== Inheritance without prototyping, etc. ==
Line 108 ⟶ 117:
Should it not be explained that the following:
 
<sourcesyntaxhighlight lang="JavaScript">
if(x==1){
something()
Line 115 ⟶ 124:
somethingelse();
}
</syntaxhighlight>
</source>
the else if works like a shorthand if (without {})
 
<sourcesyntaxhighlight lang="JavaScript">
if(x==1)
do something here
Line 131 ⟶ 140:
}
}
</syntaxhighlight>
</source>
Sorry if that didn't make sense, I find it hard to explain.
 
Line 145 ⟶ 154:
== For loop: end-condition? ==
 
<sourcesyntaxhighlight lang="JavaScript">
for (initial;end-condition;loop statement) {
/*
Line 153 ⟶ 162:
*/
}
</syntaxhighlight>
</source>
 
The condition should be called ''while-condition'', or just ''condition'', but never ''end-condition''. As in every C-like language, the for{} loop cycles '''while''' the condition is satisfied. --[[User:Comocomocomocomo|Como]] ([[User talk:Comocomocomocomo|talk]]) 14:28, 23 October 2008 (UTC)
Line 208 ⟶ 217:
The code could be changed to also illustrate the ternary operator <tt>? :</tt> :
 
<sourcesyntaxhighlight lang="JavaScript">
return diff > 0 ? gcd(segmentB, diff) : gcd(segmentA, -diff)
</syntaxhighlight>
</source>
 
Since the code might be copied, it would be well to change it, after testing, to the more efficient method, in which the second argument to the internal <tt>gcd</tt> is either <tt>A mod B</tt> or <tt>B mod A</tt>, which is approximately equally easy to read; or to the wondrous non-recursive form :
 
<sourcesyntaxhighlight lang="JavaScript">
function GCD(U, V) { // or HCF
while (true) {
if (!(U %= V)) return V
if (!(V %= U)) return U } }
</syntaxhighlight>
</source>
 
And, for the same reason, the alternative acronym HCF should be included. [[Special:Contributions/82.163.24.100|82.163.24.100]] ([[User talk:82.163.24.100|talk]]) 20:01, 24 July 2009 (UTC)
Line 268 ⟶ 277:
 
<div style="font-size:75%;line-height:150%;margin-left:2.5em;padding-bottom:1.5em;">
<sourcesyntaxhighlight lang="JavaScript">
// Automatic type coercion
//Boolean operands will be converted to a number, if possible, or to a string (if the other is a string)
Line 286 ⟶ 295:
alert(true === 1); // false
alert(true === "1"); // false
</syntaxhighlight>
</source>
 
Examples with "falsy" and "truthy" values.
 
<sourcesyntaxhighlight lang="JavaScript">alert(3?true:false); // true
alert(true == 3); // false
// 3 is logically true(known as "truthy"), as are all non-0 non-nan numbers. However, it is not "true", even with type coercion.
Line 298 ⟶ 307:
 
alert(false == +'NaN'); // false (shorthand for getting a NaN value)
</syntaxhighlight>
</source>
</div>
:I think some of the permutations could potentially be eliminated but it's necessary to show, for example, that neither <code>true==2</code> nor <code>false==2</code> behave the way one might expect.
Line 333 ⟶ 342:
== Function example code misleading ==
 
<sourcesyntaxhighlight lang="JavaScript">
var obj1 = {a : 1};
var obj2 = {b : 2};
Line 342 ⟶ 351:
foo(obj1, 3); // Does not affect obj1 at all. 3 is additional parameter
alert(obj1.a + " " + obj2.b); // writes 1 3
</syntaxhighlight>
</source>
 
This is misleading because obj1 can be modified by the function. It is quite odd (in an explanation) to pass a parameter to a function and then ignore it.
Consider:
<sourcesyntaxhighlight lang="JavaScript">
var obj1 = {a : 1};
var obj2 = {b : 2};
Line 356 ⟶ 365:
foo(obj1, 3); // Does affect obj1. 3 is additional parameter
alert(obj1.a + " " + obj2.b); // writes 7 3
</syntaxhighlight>
</source>
Because the object parameter is passed by reference it can be modified.
This is OR for me. I am just learning. But I was surprised that obj1 can be modified in the function, by p.a=7, yet p=obj2 does not affect obj1. This should be explained. [[User:QuentinUK|QuentinUK]] ([[User talk:QuentinUK|talk]]) 02:27, 28 June 2011 (UTC)
Line 406 ⟶ 415:
 
The following
<sourcesyntaxhighlight lang="JavaScript">
alert( !0 === Boolean( !0 ) === !!1 === Boolean( 1 ) === true );
alert( !!0 === Boolean( 0 ) === !1 === Boolean( !1 ) === false );
alert( !"" === Boolean( !"" ) === !!"s" === Boolean( "s" ) === true );
alert( !!"" === Boolean( "" ) === !"s" === Boolean( !"s" ) === false );
</syntaxhighlight>
</source>
Gives the impression that equality can be chained together. A===B===C being true when they are all equal, this is not the case. [[User:QuentinUK|QuentinUK]] ([[User talk:QuentinUK|talk]]) 10:57, 11 August 2011 (UTC)
 
Line 493 ⟶ 502:
The source of information can be the standard manual of the language if it exist, please so don't expect hundreds of references.
Just a list of manuals describing the evolution of the language, because some constructs appeared from the first version, others in later versions. It would be ridiculous to tag such article urging for more references. It is a simple task for an experienced JavaScript programmer with some formal education in IT. [[User:Elias|Elias]] ([[User talk:Elias|talk]]) 06:56, 25 December 2019 (UTC)
: {{re|Elias}}. Thumbing up. I agree the article must be shortened. I also suggest to move examples to a separate one.
: {{tq|i=1|[...] The article may also include a semantic description [..] }} It would be disastrous to the article as specs contain a lot (I mean A LOT) of elaborated [[Interface description language|IDL]] definitions for very simple language constructs. I stand for providing simple examples avoiding making article overly academic/specific. <span style="font-size: small" >[[User:Alexander_Davronov|<span style='color:#a8a8a8'>AXO</span><span style="color:#000">NOV</span>]] [[User talk:Alexander_Davronov|(talk)]] [[Special:Contributions/Alexander_Davronov|⚑]]</span> 08:35, 13 October 2020 (UTC)
 
== Module syntax ==
Module syntax which is the most basic thing of every JS interpreter is missing. I suggest to make section on how module system works per different implementations like browser/server(e.g. Google Chrome/Node.js). I suggest to avoid making it bloated as to address issue pointed out by {{u|Elias}}. <span style="font-size: small" >[[User:Alexander_Davronov|<span style='color:#a8a8a8'>AXO</span><span style="color:#000">NOV</span>]] [[User talk:Alexander_Davronov|(talk)]] [[Special:Contributions/Alexander_Davronov|⚑]]</span> 08:35, 13 October 2020 (UTC)