Re: function pointers vs direct calling in javascript
- From: Randy Webb <HikksNotAtHome@xxxxxxx>
- Date: Thu, 22 Nov 2007 02:46:43 -0500
getsanjay.sharma@xxxxxxxxx said the following on 11/22/2007 1:37 AM:
On Nov 21, 1:13 pm, Randy Webb <HikksNotAtH...@xxxxxxx> wrote:Told ya you wouldn't believe me :)I believe you. :-)
So you mean there are cases where this would be true? Any examples?Is this so that it doesn't go out to search theThat's a possibility, although it isn't in this case.
scope tree to look for 'tempVar2' or for some other reason?
A case where I wouldn't want it to go outside looking for it?
var tempVar = new Array();
//lots of entries in tempVar here
function something(){
tempVar = "...."
}
function somethingElse(){
alert('hey, where did my array go?')
}
A case where I would? The altering of a global variable, an array, or several other things.
Here is what I think of 'var', correct me if I am wrong.An detailed explanation with any relevant links would be greatlyDetailed explanation of the use of the var keyword? I am sure there is
appreciated.
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.
'var' doesn't as such declare a variable. You don't need 'var' keyword
to *declare* variables or put them in the symbol tree in javascript.
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 myArray = new Array();
Give it some thought :)
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?
It isn't that simple though.
var someVar = "global variable";
function someFunction(){
var someVar = "local variable";
someVar = "modified in the function";
alert(window['someVar'])
}
Without testing, what will the alert say, and why? Then test it and see if you were right without testing it :)
--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
.
- Follow-Ups:
- Re: function pointers vs direct calling in javascript
- From: getsanjay . sharma
- 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: Running j/s only at certain times of the year?
- Next by Date: Re: Valid one time variable assign via var someVar = (this = Object) ? Getvar() : this;
- 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
|