473,778 Members | 1,852 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

function pointers vs direct calling in javascript

Hi,
I wanted to know the performance of calling a function pointer v/s a
normal function call in javascript in a scenario where there are
multiple calls in the js to the same function. Please share your
thoughts if someone has worked on this.

Thanks,
Sampat.
Nov 20 '07
15 4288
On Nov 22, 12:46 pm, Randy Webb <HikksNotAtH... @aol.comwrote:
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?')
}
Agreed. That is one of the reasons why I prefix globals with g_ and so
called constants with c_ .

A case where I would? The altering of a global variable, an array, or
several other things.
var myArray = new Array();

Give it some thought :)
Huh? I am confused.
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 :)
I was able to guess the output without running it. The answer would be
'global variable' because AFAIK javascript starts searching for an
identifier from the closest scope to the most generic scope.

But then again, I might be wrong so please feel free to correct me. :-)
Nov 22 '07 #11
ge************* *@gmail.com said the following on 11/22/2007 11:38 AM:
On Nov 22, 12:46 pm, Randy Webb <HikksNotAtH... @aol.comwrote:
>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?')
}
Agreed. That is one of the reasons why I prefix globals with g_ and so
called constants with c_ .
l_ for local might be a better prefix.

<snip>
The answer would be 'global variable' because AFAIK javascript
starts searching for an identifier from the closest scope to the
most generic scope.
When looking it up, yes. When assigning to it, no. The use of the 'var'
keyword stops it from going "up" the scope chain.

I think there is an old thread somewhere where Richard Cornford
explained it better than I have. I just can't find it right now.
Searching the Google archives for "var" is, well, kinda pointless :)

--
Randy
Chance Favors The Prepared Mind
comp.lang.javas cript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Nov 23 '07 #12
On Nov 23, 11:46 am, Randy Webb <HikksNotAtH... @aol.comwrote:
[...]
When looking it up, yes. When assigning to it, no. The use of the 'var'
keyword stops it from going "up" the scope chain.

I think there is an old thread somewhere where Richard Cornford
explained it better than I have.
Here's one that includes - with -:

<URL:
http://groups.google.com.au/group/co...4a50b8acebecd5
>

--
Rob
Nov 23 '07 #13
obG wrote:
On Nov 22, 4:37 pm, getsanjay.sha.. .@gmail.com wrote:
>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';
(Sigh. [psf 10.1] More or less; rather less. It should be

window['variableName'] = 'foo';

if that; `window' does not need to refer to the Global Object.)

IOW, a variable that is not declared with `var' is not a variable at all.
The identifier will refer to a property of an object O in the Scope Chain.
O is often the Global Object, which should be at the end of the Scope Chain,
but O does not need to. During identifier resolution, the first object that
reports to have such a property will be O.

In fact, O may be not a host object which makes this initialization-only
approach error-prone. It is known that in the MSHTML DOM IDs and names of
markup elements make up property names of an host object that is before the
Global Object in the Scope Chain which is why you get a runtime error if you
try write access to those properties.)

(Maybe I should create a template for that as people keep forgetting it even
though I post the reminder frequently.)
PointedEars
Nov 23 '07 #14
Thanks a lot Randy, Rob and Thomas for your enlightening. It would
take some time for all that to seep inside but when in doubt I know
where to turn to. :-)

Thanks and regards,
/~STS
Nov 25 '07 #15
Thomas 'PointedEars' Lahn wrote:
IOW, a variable that is not declared with `var' is not a variable at all.
The identifier will refer to a property of an object O in the Scope Chain.
O is often the Global Object, which should be at the end of the Scope Chain,
but O does not need to. During identifier resolution, the first object that
reports to have such a property will be O.

In fact, O may be not a host object which makes this initialization-only
approach error-prone. [...]
That should have either been "may not be a _native_ object" or "_may be_ a
host object_", of course. Sorry for causing confusion.
PointedEars
Nov 25 '07 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2033
by: nick | last post by:
I have the following code: var ocevent = function(v) { alert('javascript event: u clicked '+v); return false; };
2
9801
by: Chuck Martin | last post by:
I am having a most frustrating problem that references, web searches, and other resources are no help so far in solving. Basically, I'm trying to design a pop-up window to be called with a funciton in a link. that function can have parameters for URL and window name passed to it. This works peachy in Firefox (1.0). With IE 6 (6.0.29) on two separate computers, I get an "onject expected" error. Going to the MS-based debugger just tells me...
10
7315
by: Martin Vorbrodt | last post by:
Example code in one of my books intrigues me: class B { public: B* Clone() const { B* p = DoClone(); assert(typeid(*p) == typeid(*this)); return p; }
5
4643
by: mike | last post by:
If I have a document like: <script> function mike_test() {alert('hi');} </script> <iframe src="blank.html" id="my_iframe1"> </iframe> and in blank.html I have:
32
3533
by: David Mark | last post by:
I've got a collection of functions that accept a function or object (paired with a method name) as a callback. For the longest time I have relied on this test. (typeof cb == 'function') This should work as well, but I am not sure how well it degrades in older browsers. I think there are issues with functions created in another context (eg frame) as well.
13
7814
by: Steve | last post by:
On page 392 of "Javascript the definitive guide" a function is called like this:- <form action="processform.cgi" onsubmit="return validateForm();"> Why, in this instance, is the return statement used in calling the function validateForm rather than being included inside the function? Thanks in advance,
5
3654
by: Immortal Nephi | last post by:
I would like to design an object using class. How can this class contain 10 member functions. Put 10 member functions into member function pointer array. One member function uses switch to call 10 member functions. Can switch be replaced to member function pointer array? Please provide me an example of source code to show smart pointer inside class. Thanks....
7
3829
by: ghulands | last post by:
I am having trouble implementing some function pointer stuff in c++ An object can register itself for many events void addEventListener(CFObject *target, CFEventHandler callback, uint8_t event); so I declared a function pointer like typedef void (CFObject::*CFEventHandler)(CFEvent *theEvent);
53
8417
by: souporpower | last post by:
Hello All I am trying to activate a link using Jquery. Here is my code; <html> <head> <script type="text/javascript" src="../../resources/js/ jquery-1.2.6.js"</script> <script language="javascript" type="text/javascript">
0
9629
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9465
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10127
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8954
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7474
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6723
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5370
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5497
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2863
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.