Talk:JavaScript syntax: Difference between revisions

Content deleted Content added
No edit summary
 
(13 intermediate revisions by 10 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}}
{{User:WildBot/m04|sect={{User:WildBot/m03|1|Trigonometric_functions#Cosine|Cosine}}, {{User:WildBot/m03|1|Trigonometric_functions#Sine|Sine}}|m04}}
{{WikiProject JavaScript|importance=Top}}
}}
{{American English}}
{{Backwardscopy
|author = Miller, F. P., Vandome, A. F., & McBrewster, J.
Line 10 ⟶ 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 109 ⟶ 117:
Should it not be explained that the following:
 
<sourcesyntaxhighlight lang="JavaScript">
if(x==1){
something()
Line 116 ⟶ 124:
somethingelse();
}
</syntaxhighlight>
</source>
the else if works like a shorthand if (without {})
 
<sourcesyntaxhighlight lang="JavaScript">
if(x==1)
do something here
Line 132 ⟶ 140:
}
}
</syntaxhighlight>
</source>
Sorry if that didn't make sense, I find it hard to explain.
 
Line 146 ⟶ 154:
== For loop: end-condition? ==
 
<sourcesyntaxhighlight lang="JavaScript">
for (initial;end-condition;loop statement) {
/*
Line 154 ⟶ 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)
 
No response in several months... I assume everyone agrees, so I dared to change the article. --[[User:Comocomocomocomo|Como]] ([[User talk:Comocomocomocomo|talk]]) 09:26, 3 March 2009 (UTC)
 
: with no doubt
:<code> for(initial; condition; loopstatement) s; </code>
: is indeed syntactic sugar for
:<code> initial; while(condition){ s; loopstatement; } </code> <!-- Template:Unsigned IP --><small class="autosigned">—&nbsp;Preceding [[Wikipedia:Signatures|unsigned]] comment added by [[Special:Contributions/201.124.237.242|201.124.237.242]] ([[User talk:201.124.237.242#top|talk]]) 09:05, 25 December 2019 (UTC)</small> <!--Autosigned by SineBot-->
 
== Reworked Methods and Inheritance ==
Line 204 ⟶ 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 264 ⟶ 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 282 ⟶ 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 294 ⟶ 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 329 ⟶ 342:
== Function example code misleading ==
 
<sourcesyntaxhighlight lang="JavaScript">
var obj1 = {a : 1};
var obj2 = {b : 2};
Line 338 ⟶ 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 352 ⟶ 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 389 ⟶ 402:
 
1) In C whitespace is important for pre-processing.
2() In C++ whitespace in needed for nested templates > > otherwise this becomes >> a shift
 
[[User:QuentinUK|QuentinUK]] ([[User talk:QuentinUK|talk]]) 17:20, 21 July 2011 (UTC)
Line 395 ⟶ 408:
:As in "how so"? Offhand, semicolon insertion, escaping line terminators in string literals, stray byte order marks... js doesn't normalize Unicode equivalents, strings are just so-many 16-bit unsigned integers.—[[User talk:Machine Elf 1735|<span style="text-shadow:#00FADE 0.05em 0.05em 0.07em;white-space: nowrap;font-family: Fraktur, Mathematica6, Georgia, sans-serif">Machine Elf <sup style="font-size:75%;font-family: Georgia, sans-serif">1735</sup></span>]] 01:22, 24 July 2011 (UTC)
:: It is not unlike C in this respect. So the statement that it is unlike C is incorrect. [[User:QuentinUK|QuentinUK]] ([[User talk:QuentinUK|talk]]) 03:18, 25 July 2011 (UTC)
::: I don't know JavaScript, I saw some code with some lines starting with a semicolon, I wrongly interpreted it as a line comment like the // in C++ because other languages, like several assemblers and Lisp, treat semicolon as a line comment.
:::I learned from this article that JavaScript has an ambiguous syntax with this respect.
:::Syntax ambiguities ''should be a section'' in the article, although I think that the whole article should be rewritten focusing on the syntax of the language.
 
== Strict equals transitivity ==
 
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 485 ⟶ 501:
 
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. <!-- Template:Unsigned IP --><small class="autosigned">—&nbsp;Preceding [[WikipediaUser:SignaturesElias|unsigned]] comment added by [[Special:Contributions/201.124.237.242|201.124.237.242Elias]] ([[User talk:201.124.237.242#topElias|talk]]) 06:4456, 25 December 2019 (UTC)</small> <!--Autosigned by SineBot-->
: {{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)