473,326 Members | 2,192 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,326 software developers and data experts.

Function.call and Function.apply

I would like to check if my understanding of the two functions are
correct.

Is this correct? The following three lines are equivalent, for
example, [1] can be replaced with [2] or [3] anywhere in a script
without changing the return value and without changing any side effect.
[1] var r = o.f(a,b);
[2] var r = o.f.call(o,a,b);
[3] var r = o.f.apply(o,[a,b]);

( o is an object and o.f is a function. )

The following three lines are equivalent:
[1'] var r = g(a,b);
[2'] var r = g.call(this,a,b);
[3'] var r = g.apply(this,[a,b]);

( g is a function, for example,
var g = function(a,b){return [this,a,b];}. )

Aug 25 '06 #1
1 6656
"HopfZ" <ho******@gmail.comwrites:
Is this correct? The following three lines are equivalent
....
[1] var r = o.f(a,b);
[2] var r = o.f.call(o,a,b);
[3] var r = o.f.apply(o,[a,b]);
Correct

The following three lines are equivalent:
[1'] var r = g(a,b);
[2'] var r = g.call(this,a,b);
[3'] var r = g.apply(this,[a,b]);
Only if used where "this" refers to the global object (i.e., the call
is in the global scope or inside a function called in this way).

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Aug 25 '06 #2

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

Similar topics

9
by: Penn Markham | last post by:
Hello all, I am writing a script where I need to use the system() function to call htpasswd. I can do this just fine on the command line...works great (see attached file, test.php). When my...
10
by: Brett Foster | last post by:
Perhpas somebody can give me a hand with this little problem. Given: var object; -- an object var func; -- a function call back Now, I want to know how to get the following effect: ...
1
by: Thomas Brunko | last post by:
Hy, I'm working on a hash table and came upon a small problem. For a better explanation I've included an extract of the header file for the hash module: typedef struct TABLE * TABLE_P; typedef...
6
by: Alexis Nikichine | last post by:
Hello, Today, I have a function: function f() { } and am looking for a way of distinguishing, from inside f, whether it
21
by: sks | last post by:
Hi , could anyone explain me why definition to a pure virtual function is allowed ?
3
by: Jeff Stewart | last post by:
I've been working with the JavaScript Shell in Firefox on a mad-scientist problem I've had in my head. Assume a function named 'object' that is designed to create an object based on a prototype...
6
by: rep_movsd | last post by:
Hi folks I was on topcoder and came across an interesting problem... It involved dynamic programming ( storing function results in a map to avoid repeated computation ) and I ended up having...
12
by: viza | last post by:
Hi I have program1.c: typedef int (*fn_t)(int); int fn( int f ){ return f; }
9
by: Andrew Poulos | last post by:
Say I have this: var foo = function(x, y) { return x + y; }; var bar = function(fname, param1, param2) { /* * how do I call the function "foo" * and pass parameters to it?
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.