473,386 Members | 1,668 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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(somePara, someFunction) {
...
someButton.addListener("execute", function(e) {
someFunction(someData);
});
...
}

I google for some documentation and this version works well:

bar : function(somePara, someFunction) {
...
someButton.addListener("execute", function(e) {
someFunction.call(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 1170
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(somePara, someFunction) {
...
someButton.addListener("execute", function(e) {
someFunction(someData);
});
...
}
Do you get an error? Which one exactly?
I google for some documentation and this version works well:

bar : function(somePara, someFunction) {
...
someButton.addListener("execute", function(e) {
someFunction.call(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(somePara, someFunction) {
...
someButton.addListener("execute", function(e) {
someFunction(someData);
});
...
}

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(somePara, someFunction) {
...
someButton.addListener("execute", function(e) {
someFunction.call(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
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...
99
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...
6
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
by: Patrick Kutch | last post by:
Please take a look at the following code snippit: ------------------------------------------------------- template<class TraitClass=int> class TestClass { public: void CallFunc() {...
15
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...
7
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...
30
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...
4
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...
14
by: v4vijayakumar | last post by:
Why we need "virtual private member functions"? Why it is not an (compile time) error?
2
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>...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...

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.