473,699 Members | 2,311 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Functions: call / apply / f()

Hi folks !

I have some code like this that passes an anonymous function:
foo.bar(p1, function(p2) {
alert(p2);
});

in foo.bar, i create a button with a listener; when it is executed, the
following code fails to run:

bar : function(somePa ra, someFunction) {
...
someButton.addL istener("execut e", function(e) {
someFunction(so meData);
});
...
}

I google for some documentation and this version works well:

bar : function(somePa ra, someFunction) {
...
someButton.addL istener("execut e", function(e) {
someFunction.ca ll(null, someData);
});
...
}

I don't understand the difference between f.apply(), f.call() and f()
and why the latter doesn't work

In fact, when using f("foo") the function is executed (I'm sure of that
when I insert alert("Hello")) , but without its argument if in the body
of the anonymous function i use alert(p2) it fails

Please can you explain this behaviour ?

--
Cordialement,

///
(. .)
--------ooO--(_)--Ooo--------
| Philippe Poulard |
-----------------------------
http://reflex.gforge.inria.fr/
Have the RefleX !
Sep 5 '08 #1
2 1181
Philippe Poulard wrote:
I have some code like this that passes an anonymous function:
foo.bar(p1, function(p2) {
alert(p2);
});

in foo.bar, i create a button with a listener; when it is executed, the
following code fails to run:

bar : function(somePa ra, someFunction) {
...
someButton.addL istener("execut e", function(e) {
someFunction(so meData);
});
...
}
Do you get an error? Which one exactly?
I google for some documentation and this version works well:

bar : function(somePa ra, someFunction) {
...
someButton.addL istener("execut e", function(e) {
someFunction.ca ll(null, someData);
});
...
}

I don't understand the difference between f.apply(), f.call() and f()
and why the latter doesn't work

In fact, when using f("foo") the function is executed (I'm sure of that
when I insert alert("Hello")) , but without its argument if in the body
of the anonymous function i use alert(p2) it fails

Please can you explain this behaviour ?
So the alert dialog does not appear? Have you checked the error console
of the browser?
I don't see a reason why f() should fail but f.call() should execute.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Sep 5 '08 #2
Martin Honnen a écrit :
Philippe Poulard wrote:
>I have some code like this that passes an anonymous function:
foo.bar(p1, function(p2) {
alert(p2);
});

in foo.bar, i create a button with a listener; when it is executed, the
following code fails to run:

bar : function(somePa ra, someFunction) {
...
someButton.addL istener("execut e", function(e) {
someFunction(so meData);
});
...
}

Do you get an error? Which one exactly?
No because it is trapped by the framework within which it is executed
(that doesn't log it correctly); but it fails because i don't get the
alert box (I'm sure the block is executed because if I insert
alert("Hello"); before alert(p2); I see the former but not the latter)

Therefore, it seems that a reference to p2 make all the stuff crashing
>
>I google for some documentation and this version works well:

bar : function(somePa ra, someFunction) {
...
someButton.addL istener("execut e", function(e) {
someFunction.ca ll(null, someData);
});
...
}

I don't understand the difference between f.apply(), f.call() and f()
and why the latter doesn't work

In fact, when using f("foo") the function is executed (I'm sure of
that when I insert alert("Hello")) , but without its argument if in the
body of the anonymous function i use alert(p2) it fails

Please can you explain this behaviour ?

So the alert dialog does not appear? Have you checked the error console
of the browser?
I don't see a reason why f() should fail but f.call() should execute.
ok, so it's not my fault :)
since f.call() works, I have changed to it in my code

--
Cordialement,

///
(. .)
--------ooO--(_)--Ooo--------
| Philippe Poulard |
-----------------------------
http://reflex.gforge.inria.fr/
Have the RefleX !
Sep 5 '08 #3

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

Similar topics

5
1964
by: Blair Hall | last post by:
Can anyone please tell me how to correctly use a built in function when there is a function of the same name in local scope? Here is an example. Suppose the following is in myApply.py: def apply(func,seq): # # Code can default to # built-in definition in some cases: return __builtins__.apply(func,seq)
99
5892
by: David MacQuigg | last post by:
I'm not getting any feedback on the most important benefit in my proposed "Ideas for Python 3" thread - the unification of methods and functions. Perhaps it was buried among too many other less important changes, so in this thread I would like to focus on that issue alone. I have edited the Proposed Syntax example below to take out the changes unecessary to this discussion. I left in the change of "instance variable" syntax (...
6
2496
by: svend | last post by:
Hey there, I was thinking about some curried functions in JavaScript, and I made this: <script> function add(a, b, c) { if (arguments.length == 1) { return function (d,e) {return a + d + e} }
9
1626
by: Patrick Kutch | last post by:
Please take a look at the following code snippit: ------------------------------------------------------- template<class TraitClass=int> class TestClass { public: void CallFunc() { SpecializedFunct_<TraitClass>(); }
15
5539
by: Jerry He | last post by:
Hi, Is it possible to create a function that you can use without parenthesizing the arguments? for example, for def examine(str): ..... ..... Is there some way to define it so that I can call it like
7
2174
by: jolie.demiranda | last post by:
Dear all, I'm fairly new to C++ so forgive me is this topic may have been convered before... I'm having a problem with pointers to member functions. Essentially what I want to achieve is the following: class A { public: double (A::*ptrToFunction)(double); }
30
19730
by: Will Pittenger | last post by:
Does C# inline functions? I do not see a inline keyword. Is there an implicit inline? Can the compiler select functions for auto-inlining? I am more used to C++ where all these things are possible. ---------- Will Pittenger E-Mail: mailto:will.pittenger@verizon.net All mail filtered by Qurb (www.qurb.com)
4
1577
by: Daniel | last post by:
Hi, I was reading Douglas Crockford's article on prototypal inheritance at http://javascript.crockford.com/prototypal.html. I think it also relates to a post back in Dec 2005. The mechanism discussed was: function object(o) { function F() {} F.prototype = o; return new F();
14
4207
by: v4vijayakumar | last post by:
Why we need "virtual private member functions"? Why it is not an (compile time) error?
2
1999
by: babaco | last post by:
Hi, I've come upon a situation where I don't understand what's happening. Basically, I'm implementing a kind of 'chain of responsibility' using some covariant types: #include <iostream> #include <string> #include <vector> using namespace std;
0
8685
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
8613
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
9032
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
8880
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7745
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
6532
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
5869
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
4374
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...
1
3054
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.