Prev: FAQ Topic - How can I see in javascript if a web browser acceptscookies? (2010-06-16)
Next: FAQ Topic - How can I see in javascript if a web browser accepts cookies? (2010-06-16)
From: Dmitry A. Soshnikov on 17 Jun 2010 09:22 On 17.06.2010 17:13, Dmitry A. Soshnikov wrote: <snip> > There, if you want to extract elements > from "something" where "something", err, where "something" is _any_ term, but not only array (list). This is a common ideology, thought, in other languages is presented partly, e.g. only for arrays. Although, JS 1.7 supports this feature not only for arrays, but for objects also. Dmitry.
From: VK on 17 Jun 2010 09:35 (continuation, see http://groups.google.com/group/comp.lang.javascript/msg/a5f99302e2a1c5a5) 2. Swap property values of two JavaScript objects JavaScript objects are passed to function by reference value (the "reference value" term is used to disambiguate C-like *reference which doesn't exist in JavaScript). That allows to make a separate swap subroutine as long as the property name can be passed as a literal, for instance: var jso1 = {'prop1' : 10}; var jso2 = {'prop1' : 15}; swap(jso1, jso2, 'prop1'); function swap(jsobj1, jsobj2, prop) { var tmp = jsobj1[prop]; jsobj1[prop] = jsobj2[prop]; jsobj2[prop] = tmp; } End of property values of two JavaScript objects situation. To be continued with the last situation. Corrections are welcome.
From: Benjamin 'BeRo' Rosseaux on 17 Jun 2010 09:38 Am 17.06.2010 11:55, schrieb Tim Streater: > In article<Xns9D9A77ADAEDF1eejj99(a)194.109.133.242>, > "Evertjan."<exjxw.hannivoort(a)interxnl.net> wrote: > >> Dmitry A. Soshnikov wrote on 16 jun 2010 in comp.lang.javascript: >> >>> Swapping without "third" can be done with simple arithmetic operations: >>> >>> A = A - B >>> B = A + B >>> A = B - A >>> >> >> Many many years ago, >> on a microprocessor called 2650, >> there was an assembler opcode called: >> >> swab >> >> .. some programmers over here thought >> this was the native spelling of swap. >> >> However it ment "swap registers a and b" > > Was it not "swap bytes" on an operand in the PDP-11? So you might say: > > swab R3 or swab (SP) > > for example to swap the bytes in register 3 or the top item on the stack. > Even on x86/x64 does exist a swap opcode instruction called xchg as shorthand for "exchange", for example xchg eax,ebx which swaps the content between the registers eax and ebx.
From: Richard Cornford on 17 Jun 2010 10:16 On Jun 17, 1:51 pm, Dmitry A. Soshnikov wrote: > On 17.06.2010 0:41, Lasse Reichstein Nielsen wrote: <snip> >>> Swapping without "third" can be done with simple arithmetic >>> operations: > >>> A = A - B >>> B = A + B >>> A = B - A > >> Try that for strings :) > > Yeah, I know sure. Just mentioned thinking about numbers at that > time (with numbers also can be overflow, by the way). <snip> With javascript's double precision floating point numbers overflows are possible, but loss of precision seems (much) more likely. Consider what will happen if A is a number at the upper range of possible number representations, say 1.0e308 and B is a number close to the smallest (positive) number, say 5.0e-324. But the numbers don't have to be as extreme as those:- var A = 1.0e9; var B = 1.0e-9; A = A - B B = A + B A = B - A // A is now zero // B is now 1000000000 I have a vague recollection of once being shown a 'swapping' algorithm for signed integers (with no third variable) that used only bitwise operations to swap the values, avoiding any risk of overflows. Obviously in javascript that would only be viable for 32 bit integers, and adding two of those will not overflow the range of integers that can be represented by an IEEE double precision floating point number. Richard.
From: VK on 17 Jun 2010 10:41
(continuation, see http://groups.google.com/group/comp.lang.javascript/msg/a5f99302e2a1c5a5 http://groups.google.com/group/comp.lang.javascript/msg/c90c13f264ada061 ) 3. Swap elements of HTMLCollection (DOM0) or NodeList (DOM1 and higher) Such collections normally do not allow direct assignments to their members so specific DOM methods usage will be needed. IE DOM model provides a proprietary swapNode method: http://msdn.microsoft.com/en-us/library/ms536774%28VS.85%29.aspx The W3C DOM model is still missing such method so different voodoo dances with removeChild/appendChild/etc. are so far needed. As the native swap method times more effective than any voodoo, first it should be tried to use it if presented. If not, then the most simple/effective replacement for init.W3Swap is highly welcome in this thread. <!DOCTYPE html> <html> <head> <title>Test</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <script type="text/javascript"> var swapNodes = null; function test() { var pcol = document.getElementsByTagName('P'); swapNodes(pcol, 1, 2); } function init() { swapNodes = (/*@cc_on true || @*/ false) ? init.IESwap : initW3Swap; test(); } init.IESwap = function(collection, index1, index2) { var parentElement = collection.item(0).parentElement; parentElement.children(index1). swapNode( parentElement.children(index2) ); } init.W3Swap = function(parentElement, index1, index2) { // your code goes here } </script> </head> <body onload="init()"> <p>P1</p> <p>P2</p> <p>P3</p> </body> </html> .. |