3 ms·
I can only comment on the following two points: - Define g and s as parameter even though they're only given a value inside the function - Pass document into
by stingraycharles 15y ago
I can only comment on the following two points:
- Define g and s as parameter even though they're only given a value inside the function
- Pass document into the function instead of using it from global scope
This is not necessarily true: javascript passed arguments by reference, and thus g and s are actually "given back" to the caller, who can then do different things with those objects.
d is not necessarily a document, but can be something different too: consider, for example, the contentWindow of an iframe.
- wladimir 15y agoThis is not necessarily true: javascript passed arguments by reference, and thus g and s are actually "given back" to the caller Pass by reference doesn't work that way in JS; function a(b) { b = 10; } var foo; a(foo); alert(foo); Won't display "10". It behaves like passing pointers, not mutable references such as the C++'s &.
- stingraycharles 15y agoI stand corrected, you are right. Then the only thing I can think about is that accepting it as an additional parameter in the function call is less overhead (as in, bytes) than defining 'b' as a var: function a(b) { b = 10; } is smaller than function a() { var b = 10; } and doesn't pollute the global namespace. EDIT: I see dave1010uk also said this.