Re: function pointers vs direct calling in javascript
- From: RobG <rgqld@xxxxxxxxxxxx>
- Date: Thu, 22 Nov 2007 19:21:41 -0800 (PST)
On Nov 22, 4:37 pm, getsanjay.sha...@xxxxxxxxx wrote:
On Nov 21, 1:13 pm, Randy Webb <HikksNotAtH...@xxxxxxx> wrote:> Told ya you wouldn't believe me :)
I believe you. :-)
Is this so that it doesn't go out to search the
scope tree to look for 'tempVar2' or for some other reason?
That's a possibility, although it isn't in this case.
So you mean there are cases where this would be true? Any examples?
An detailed explanation with any relevant links would be greatly
appreciated.
Detailed explanation of the use of the var keyword? I am sure there is
one somewhere but I don't have a link to one. The simplest way to
remember it is that using var on a variable name in a function will
*never* alter a global variable. I always use it unless I explicitly
want to alter a global variable from within a function. The only other
time it can come into play is with inner functions and I never use them.
They give me too big a headache and I have never had a use for them.
Here is what I think of 'var', correct me if I am wrong.
'var' doesn't as such declare a variable.
Yes, it does.
A couple of things you should know:
1. Declarations are processed before any code is executed. That means
that if you don't declare variables, they don't exist until the code
is excuted, a feature that can have great significance for functions:
bar(); // No error, bar() exists when the call is executed
foo(); // Error! foo doesn't exist until the next line is executed.
var foo = function(){}; // Function statement
function bar(){} // Function declaration
2. The use of var establishes the context for the variable. Any
variable used without var will be created as a property of the global
object for that context *when the code is executed*. Note that
undeclared variables with eval may not behave as you expect.
3. Because of 1., using var on a variable multiple times within the
same scope has no effect:
var x = 3;
alert(x); // => 3
var x;
alert(x); // => 3
You don't need 'var' keyword
to *declare* variables
Yes, you do. A variable without var isn't declared, it is created as
a property of the global object when the code executes. It is
equivalent (more or less) to:
window[variableName] = 'foo';
or put them in the symbol tree in javascript.
Not sure about the term "symbol tree".
It's just an indication to the scripting engine that 'please put this
variable in a scope which is local to this function. So saying:
var a = 10;
var a = 11;
doesn't actually create two variables or declare two variables but
refer to the same variable both the times which is 'a' which was
declared and defined using the statement 'a = 10'. Thus prepending the
variable name with 'var' separates it from the global namespace
(scope) (assuming we are not using 'with' in which case we would have
an additional scope).
Is this good enough?
No. The variable a is declared twice, so before any code is executed,
a variable a is created within the scope of the declaration. When the
code is executed, a is firstly assigned a value of 10, then
immediately afterward a value of 11. The declaration is processed
once, the assignment is processed each time the code is executed.
This feature of javascript is often used where feature testing is
required - it can be done once up front and not every time the
function is called, e.g.:
Method 1 - using a function declaration:
function getText(el){
if(typeof el.textContent == 'string') {
return el.textContent;
}
if(typeof el.innerText == 'string') {
return el.innerText;
}
}
Method 2 - using a function expression:
var getText = (function(){
var el = document.getElementsByTagName('script')[0];
if (typeof el.textContent == 'string') {
return function(el){return el.textContent;};
}
if (typeof el.innerText == 'string') {
return function(el){return el.innerText;};
}
})();
The second method should be more efficient as the feature test is only
carried out once, whereas in the first it is done every time getText
is called. Note that for a production system, the above should be
extended to support browsers that haven't implemented either
textContent or innerText - I didn't included the code here for the
sake of brevity.
--
Rob
.
- Follow-Ups:
- Re: function pointers vs direct calling in javascript
- From: Thomas 'PointedEars' Lahn
- Re: function pointers vs direct calling in javascript
- References:
- function pointers vs direct calling in javascript
- From: Sampat
- Re: function pointers vs direct calling in javascript
- From: Randy Webb
- Re: function pointers vs direct calling in javascript
- From: Darko
- Re: function pointers vs direct calling in javascript
- From: Randy Webb
- Re: function pointers vs direct calling in javascript
- From: getsanjay . sharma
- Re: function pointers vs direct calling in javascript
- From: Randy Webb
- Re: function pointers vs direct calling in javascript
- From: getsanjay . sharma
- function pointers vs direct calling in javascript
- Prev by Date: Re: function pointers vs direct calling in javascript
- Next by Date: Re: [OT?] Htm - browser question - Spam origin
- Previous by thread: Re: function pointers vs direct calling in javascript
- Next by thread: Re: function pointers vs direct calling in javascript
- Index(es):
Relevant Pages
|