473,586 Members | 2,546 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Still have a question about a common JavaScript routine

I still have a question regarding the following code,
in a commonly used routine. First, Here's the code in
question:

Function.protot ype.bind = function(){
var fn = this, args = Array.prototype .slice.call(arg uments), object =
args.shift();
return function(){
return fn.apply(object ,
args.concat(Arr ay.prototype.sl ice.call(argume nts)));
};
};
var myObject = {};
function myFunction(){
return this == myObject;
}

In the code,
return fn.apply(object ,
args.concat(Arr ay.prototype.sl ice.call(argume nts)));
,
it seems apply is getting an extra,
array element (because the first element in arguments
is still the object (the one setting the context of "this).

In short, it looks to me like the object element which
sets the context of "this" is in the final args array
twice (twice at the beginning of the array).

This can't be. I must be wrong, Please explain .
Sep 6 '08 #1
9 1531
lorlarz wrote:
I still have a question regarding the following code,
in a commonly used routine. First, Here's the code in
question:

Function.protot ype.bind = function(){
var fn = this, args = Array.prototype .slice.call(arg uments), object =
args.shift();
return function(){
return fn.apply(object ,
args.concat(Arr ay.prototype.sl ice.call(argume nts)));
};
};
var myObject = {};
function myFunction(){
return this == myObject;
}

In the code,
return fn.apply(object ,
args.concat(Arr ay.prototype.sl ice.call(argume nts)));
,
it seems apply is getting an extra,
array element (because the first element in arguments
is still the object (the one setting the context of "this).
The anonymous function's arguments object is not the same arguments
object of the enclosing bind function.

In short, it looks to me like the object element which
sets the context of "this" is in the final args array
twice (twice at the beginning of the array).
It would seem to be less confusing to pass in an actual parameter
variable for the context, and include a comment:-

/**
* @param {Object} context - the - this - value to be used.
* @param {arguments} [1..n] optional arguments that are prepended
* to returned function's call.
* @return {Function} a function that applies the original
* function with - context - as the thisArg.
*/
Function.protot ype.bind = function(contex t){
...
}

This will affect the args variable and the way you call slice.

Take a closer look at args.shift, too.

http://bclary.com/2004/11/07/#a-15.4.4.9
Garrett
Sep 6 '08 #2
On Sep 6, 12:37*pm, dhtml <dhtmlkitc...@g mail.comwrote:
lorlarz wrote:
I still have a question regarding the following code,
in a commonly used routine. *First, Here's the code in
question:
Function.protot ype.bind = function(){
var fn = this, args = Array.prototype .slice.call(arg uments), object=
args.shift();
return function(){
return fn.apply(object ,
args.concat(Arr ay.prototype.sl ice.call(argume nts)));
* *};
};
var myObject = {};
function myFunction(){
return this == myObject;
}
In the code,
return fn.apply(object ,
args.concat(Arr ay.prototype.sl ice.call(argume nts)));
,
it seems apply is getting an extra,
array element (because the first element in arguments
is still the object (the one setting the context of "this).

The anonymous function's arguments object is not the same arguments
object of the enclosing bind function.
In short, it looks to me like the object element which
sets the context of "this" is in the final args array
twice (twice at the beginning of the array).

It would seem to be less confusing to pass in an actual parameter
variable for the context, and include a comment:-

/**
* * @param {Object} context - the - this - value to be used.
* * @param {arguments} [1..n] optional arguments that are prepended
* * to returned function's call.
* * @return {Function} a function that applies the original
* * function with - context - as the thisArg.
* */
Function.protot ype.bind = function(contex t){
* ...

}

This will affect the args variable and the way you call slice.

Take a closer look at args.shift, too.

http://bclary.com/2004/11/07/#a-15.4.4.9

Garrett
Your clarification was great and I understand and I
agree with your input as well. Thank you.
Sep 6 '08 #3
lorlarz wrote:
On Sep 6, 12:37 pm, dhtml <dhtmlkitc...@g mail.comwrote:
>lorlarz wrote:
>>I still have a question regarding the following code,
in a commonly used routine. First, Here's the code in
question:
Function.prot otype.bind = function(){
var fn = this, args = Array.prototype .slice.call(arg uments), object =
args.shift( );
return function(){
return fn.apply(object ,
args.concat(A rray.prototype. slice.call(argu ments)));
};
};
var myObject = {};
function myFunction(){
return this == myObject;
}
In the code,
return fn.apply(object ,
args.concat(A rray.prototype. slice.call(argu ments)));
,
it seems apply is getting an extra,
array element (because the first element in arguments
is still the object (the one setting the context of "this).
The anonymous function's arguments object is not the same arguments
object of the enclosing bind function.
>>In short, it looks to me like the object element which
sets the context of "this" is in the final args array
twice (twice at the beginning of the array).
It would seem to be less confusing to pass in an actual parameter
variable for the context, and include a comment:-

/**
* @param {Object} context - the - this - value to be used.
* @param {arguments} [1..n] optional arguments that are prepended
* to returned function's call.
* @return {Function} a function that applies the original
* function with - context - as the thisArg.
*/
Function.proto type.bind = function(contex t){
...

}

This will affect the args variable and the way you call slice.

Take a closer look at args.shift, too.

http://bclary.com/2004/11/07/#a-15.4.4.9

Garrett

Your clarification was great and I understand and I
agree with your input as well. Thank you.
Well I didn't really help you all that much. But you could post up some
code to show us.

There may also be the consideration that this method does two things:
1) partial apply
2) bind

Most of the you won't usually need all of this functionality together.
And will just need a bind.

There are a few potential problems with adding Function.protot ype.bind:

If, for example, the next version of EcmaScript includes
Function.protot ype.bind, and it does it differently than your
user-defined one, it might confuse other programmers who looked at the
calls to the would-be non-standard bind.

Some versions of bind that I've seen just a bind (fast), while others
use partialApply + bind (slower).

Existing code stuck with a partialApply + bind assinged to
Function.protot ype.bind could be tweaked for efficiency, refactored to
use some other approach that is efficient and uses a safer name,
allowing for the possibility of deprecating (not removing) the old
Function.protot ype.bind.

Garrett
Sep 7 '08 #4
On Sep 6, 10:12*pm, dhtml <dhtmlkitc...@g mail.comwrote:
lorlarz wrote:
On Sep 6, 12:37 pm, dhtml <dhtmlkitc...@g mail.comwrote:
lorlarz wrote:
I still have a question regarding the following code,
in a commonly used routine. *First, Here's the code in
question:
Function.proto type.bind = function(){
var fn = this, args = Array.prototype .slice.call(arg uments), object =
args.shift() ;
return function(){
return fn.apply(object ,
args.concat(Ar ray.prototype.s lice.call(argum ents)));
* *};
};
var myObject = {};
function myFunction(){
return this == myObject;
}
In the code,
return fn.apply(object ,
args.concat(Ar ray.prototype.s lice.call(argum ents)));
,
it seems apply is getting an extra,
array element (because the first element in arguments
is still the object (the one setting the context of "this).
The anonymous function's arguments object is not the same arguments
object of the enclosing bind function.
>In short, it looks to me like the object element which
sets the context of "this" is in the final args array
twice (twice at the beginning of the array).
It would seem to be less confusing to pass in an actual parameter
variable for the context, and include a comment:-
/**
* * @param {Object} context - the - this - value to be used.
* * @param {arguments} [1..n] optional arguments that are prepended
* * to returned function's call.
* * @return {Function} a function that applies the original
* * function with - context - as the thisArg.
* */
Function.protot ype.bind = function(contex t){
* ...
}
This will affect the args variable and the way you call slice.
Take a closer look at args.shift, too.
>http://bclary.com/2004/11/07/#a-15.4.4.9
Garrett
Your clarification was great and I understand and I
agree with your input as well. *Thank you.

Well I didn't really help you all that much. But you could post up some
code to show us.

There may also be the consideration that this method does two things:
1) partial apply
2) bind

Most of the you won't usually need all of this functionality together.
And will just need a bind.

There are a few potential problems with adding Function.protot ype.bind:

If, for example, the next version of EcmaScript includes
Function.protot ype.bind, and it does it differently than your
user-defined one, it might confuse other programmers who looked at the
calls to the would-be non-standard bind.

Some versions of bind that I've seen just a bind (fast), while others
use partialApply + bind (slower).

Existing code stuck with a partialApply + bind assinged to
Function.protot ype.bind could be tweaked for efficiency, refactored to
use some other approach that is efficient and uses a safer name,
allowing for the possibility of deprecating (not removing) the old
Function.protot ype.bind.

Garrett- Hide quoted text -

- Show quoted text -
Here is a script to put in a web page and run, using the
new bind method of functions:
Function.protot ype.bind = function(){
var fn = this, args = Array.prototype .slice.call(arg uments), object =
args.shift();
return function(){
return fn.apply(object ,
args.concat(Arr ay.prototype.sl ice.call(argume nts)));
};

};
var myObject = {};
function myFunction(then ame){

alert(thename);
return this == myObject;

}
alert((myFuncti on.bind(myObjec t, "joe"))());

Since the returned function gets all its arguments from
myFunction (the object using the new Function bind methed),
the second argument sent the returned function's first --
after the object is stripped of as its context.
Sep 7 '08 #5
On Sep 6, 10:12*pm, dhtml <dhtmlkitc...@g mail.comwrote:
lorlarz wrote:
On Sep 6, 12:37 pm, dhtml <dhtmlkitc...@g mail.comwrote:
lorlarz wrote:
I still have a question regarding the following code,
in a commonly used routine. *First, Here's the code in
question:
Function.proto type.bind = function(){
var fn = this, args = Array.prototype .slice.call(arg uments), object =
args.shift() ;
return function(){
return fn.apply(object ,
args.concat(Ar ray.prototype.s lice.call(argum ents)));
* *};
};
var myObject = {};
function myFunction(){
return this == myObject;
}
In the code,
return fn.apply(object ,
args.concat(Ar ray.prototype.s lice.call(argum ents)));
,
it seems apply is getting an extra,
array element (because the first element in arguments
is still the object (the one setting the context of "this).
The anonymous function's arguments object is not the same arguments
object of the enclosing bind function.
>In short, it looks to me like the object element which
sets the context of "this" is in the final args array
twice (twice at the beginning of the array).
It would seem to be less confusing to pass in an actual parameter
variable for the context, and include a comment:-
/**
* * @param {Object} context - the - this - value to be used.
* * @param {arguments} [1..n] optional arguments that are prepended
* * to returned function's call.
* * @return {Function} a function that applies the original
* * function with - context - as the thisArg.
* */
Function.protot ype.bind = function(contex t){
* ...
}
This will affect the args variable and the way you call slice.
Take a closer look at args.shift, too.
>http://bclary.com/2004/11/07/#a-15.4.4.9
Garrett
Your clarification was great and I understand and I
agree with your input as well. *Thank you.

Well I didn't really help you all that much. But you could post up some
code to show us.

There may also be the consideration that this method does two things:
1) partial apply
2) bind

Most of the you won't usually need all of this functionality together.
And will just need a bind.

There are a few potential problems with adding Function.protot ype.bind:

If, for example, the next version of EcmaScript includes
Function.protot ype.bind, and it does it differently than your
user-defined one, it might confuse other programmers who looked at the
calls to the would-be non-standard bind.

Some versions of bind that I've seen just a bind (fast), while others
use partialApply + bind (slower).

Existing code stuck with a partialApply + bind assinged to
Function.protot ype.bind could be tweaked for efficiency, refactored to
use some other approach that is efficient and uses a safer name,
allowing for the possibility of deprecating (not removing) the old
Function.protot ype.bind.

Garrett- Hide quoted text -
Here is a script to put in a web page and run, using the
new bind method of functions:

Function.protot ype.bind = function(){
var fn = this, args = Array.prototype .slice.call(arg uments), object =
args.shift();
return function(){
return fn.apply(object ,
args.concat(Arr ay.prototype.sl ice.call(argume nts)));

};

};

var myObject = {};
function myFunction(then ame){

alert(thename);
return this == myObject;

}

alert((myFuncti on.bind(myObjec t, "joe"))());

Since the returned function gets all its arguments from
myFunction (the function using the new Function bind methed),
the second argument is the returned function's first --
after the object is stripped of as its context.

Sep 7 '08 #6
lorlarz wrote:
On Sep 6, 10:12 pm, dhtml <dhtmlkitc...@g mail.comwrote:
>lorlarz wrote:
>>On Sep 6, 12:37 pm, dhtml <dhtmlkitc...@g mail.comwrote:
lorlarz wrote:
>Garrett- Hide quoted text -
I did not type that ^. Are you using Google Groups?

Here is a script to put in a web page and run, using the
new bind method of functions:

Function.protot ype.bind = function(){
var fn = this, args = Array.prototype .slice.call(arg uments), object =
args.shift();
return function(){
return fn.apply(object ,
args.concat(Arr ay.prototype.sl ice.call(argume nts)));

};

};

var myObject = {};
function myFunction(then ame){

alert(thename);
return this == myObject;

}

alert((myFuncti on.bind(myObjec t, "joe"))());

Since the returned function gets all its arguments from
myFunction (the function using the new Function bind methed),
the second argument is the returned function's first --
after the object is stripped of as its context.
Correct.

But why not pass in a parameter for the context?

Function.protot ype.bind = function(contex t) {

};

I understood you were in agreement with what I wrote (not that you have
to be), and that was what I wrote in my initial reply.

The benefit would be that the code would be clearer because you wouldn't
have to call args.shift(). It would also be a little faster.

(It will be much easier to read your code if you indent your code with
spaces.)

Garrett
Sep 8 '08 #7
On Sep 7, 9:54*pm, dhtml <dhtmlkitc...@g mail.comwrote:
lorlarz wrote:
On Sep 6, 10:12 pm, dhtml <dhtmlkitc...@g mail.comwrote:
lorlarz wrote:
On Sep 6, 12:37 pm, dhtml <dhtmlkitc...@g mail.comwrote:
lorlarz wrote:
Garrett- Hide quoted text -

I did not type that ^. Are you using Google Groups?


Here is a script to put in a web page and run, using the
new bind method of functions:
Function.protot ype.bind = function(){
var fn = this, args = Array.prototype .slice.call(arg uments), object=
args.shift();
return function(){
return fn.apply(object ,
args.concat(Arr ay.prototype.sl ice.call(argume nts)));
* * * * };
};
var myObject = {};
function myFunction(then ame){
alert(thename);
return this == myObject;
}
alert((myFuncti on.bind(myObjec t, "joe"))());
Since the returned function gets all its arguments from
myFunction (the function using the new Function bind methed),
the second argument is the returned function's first --
after the object is stripped of as its context.

Correct.

But why not pass in a parameter for the context?

Function.protot ype.bind = function(contex t) {

};

I understood you were in agreement with what I wrote (not that you have
to be), and that was what I wrote in my initial reply.

The benefit would be that the code would be clearer because you wouldn't
have to call args.shift(). It would also be a little faster.

(It will be much easier to read your code if you indent your code with
spaces.)

Garrett
I do agree with you. I am just presenting a code example
from an upcoming book from a javascript expert, which I
have an early copy of. He did not explain the code, so
I came to this newsgroup to get a full understanding of
it as is. In my own work (if I need to) I will be sure
to use your variations on the routine, which I agree are
better.

I am watching google more closely now, to correctly
quote you (and others); sorry, I missed that one
misquote (though it was of something empty, so
your good reputation is hopefully fully intact).
Still, I will police google, as you and others have
requested and not make the error again.

Thanks for you help. You have my respect.

Sep 8 '08 #8
lorlarz wrote:
On Sep 7, 9:54 pm, dhtml <dhtmlkitc...@g mail.comwrote:
>lorlarz wrote:
>>On Sep 6, 10:12 pm, dhtml <dhtmlkitc...@g mail.comwrote:
lorlarz wrote:
On Sep 6, 12:37 pm, dhtml <dhtmlkitc...@g mail.comwrote:
>lorlarz wrote:
Garrett- Hide quoted text -
I did not type that ^. Are you using Google Groups?


>>Here is a script to put in a web page and run, using the
new bind method of functions:
Function.prot otype.bind = function(){
var fn = this, args = Array.prototype .slice.call(arg uments), object =
args.shift( );
return function(){
return fn.apply(object ,
args.concat(A rray.prototype. slice.call(argu ments)));
};
};
var myObject = {};
function myFunction(then ame){
alert(thename );
return this == myObject;
}
alert((myFunc tion.bind(myObj ect, "joe"))());
Since the returned function gets all its arguments from
myFunction (the function using the new Function bind methed),
the second argument is the returned function's first --
after the object is stripped of as its context.
Correct.

But why not pass in a parameter for the context?

Function.proto type.bind = function(contex t) {

};

I understood you were in agreement with what I wrote (not that you have
to be), and that was what I wrote in my initial reply.

The benefit would be that the code would be clearer because you wouldn't
have to call args.shift(). It would also be a little faster.

(It will be much easier to read your code if you indent your code with
spaces.)

Garrett

I do agree with you. I am just presenting a code example
from an upcoming book from a javascript expert, which I
have an early copy of. He did not explain the code, so
I came to this newsgroup to get a full understanding of
it as is. In my own work (if I need to) I will be sure
to use your variations on the routine, which I agree are
better.
So you're editing a book that you need help understanding.

The code is written in a complicated that way confuses you. You agree
that the code should be changed to be less confusing. As a technical
editor, it would seem to be a part of your role to make sure that a
method had the correct parameters, was formatted properly, and had
explanatory comment.
I am watching google more closely now, to correctly
quote you (and others); sorry, I missed that one
misquote (though it was of something empty, so
your good reputation is hopefully fully intact).
Still, I will police google, as you and others have
requested and not make the error again.
The issue with "-show quoted text-" (at least to me) makes it look like
there is something else there and if there is, then what is it and are
you replying to that?

You could try a newsreader. Thunderbird works pretty well. There are
free news servers that you can use.

Garrett
Sep 8 '08 #9
On Sep 8, 5:06*pm, dhtml <dhtmlkitc...@g mail.comwrote:
[snip]
>
If the book is anything like any of the other JavaScript books on
Manning, it won't be worth the paper it is written on.
I think that 2 recent Manning Publishers books were excellent:

ASP.NET AJAX in Action
and
jQuery in Action

(and for whatever reason, even though I have read about 150 technical
books, these seem to be the only 2 by Manning Publishing).
[snip]
>
Garrett
Sep 9 '08 #10

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

Similar topics

8
2028
by: Eric Osman | last post by:
My javascript program has reason to want to stop. For example, function A has 5 lines, the second of which calls function B, which has 5 lines, the first of which calls function C. But function C discovers that something is very wrong so it does an "alert" saying something like Sorry, couldn't make the necessary connection
1
521
by: Ian Sedwell | last post by:
Hi guys Many thanks to all who replied to my original question. Actually, it's dead easy and the way I was doing it was correct the first time. You do indeed simply call the VBScript routine from the JavaScript routine. It doesn't matter whether the scripts are in external files, or embedded in the HTML document, so long as the VBScript is...
9
2106
by: Astra | last post by:
Hi everybody Wonder if you could help me out. I created a simple JavaScript routine to enable a user to click backwards and forwards between small news articles. This routine works fine in IE and Safari, but in the latest FireFox I get no title or article, but do see the prev and next links and the article number. My HTML and JS...
687
23194
by: cody | last post by:
no this is no trollposting and please don't get it wrong but iam very curious why people still use C instead of other languages especially C++. i heard people say C++ is slower than C but i can't believe that. in pieces of the application where speed really matters you can still use "normal" functions or even static methods which is...
34
440
by: wilson | last post by:
Hi All, I am a novice at C and just have learned pointer for a short period. Today one error had occured while executing my program below. And I cannot find the error out since it's checked "OK" by Dev C++. Here is:(I want to exchange the value of "a" and "b" with function "swap") void swap(int *pa, int *pb) {
4
22582
by: Dave | last post by:
I am beginning to write some C# pages to an app I created with VB.Net. There is one VB routine I use frequently - a javascript alert box. I have created a C# version, but it does not handle newlines. I get an "unterminated string constant" error message. Googling this problem gives some hints that there are others with this problem, but I...
3
3557
by: Sam Kong | last post by:
Hi group, I want to have some advice about immutable objects. I made a constructor. function Point(x, y) { this.x = x; this.y = y; }
1
1082
by: DaveG | last post by:
Hi All Vb 2003 I am working on a project which uses multiple forms (MDI). I am hoping to use a common routine to launch most of the forms. The problem I have is that I cannot get a BaseClass variable. Public Sub FormsLauncher(Byval fn as Form, Byval bc as ???)
8
4024
by: rn5a | last post by:
I have gone through a no. of posts in this NewsGroup regarding my problem but alas, couldn't come across one which would have helped me in resolving the issue. My problem is this: An ASPX Form has a Button. When the Button is clicked, I want a JavaScript confirm dialog to pop-up with the options 'OK' & 'Cancel'. I have done this using the...
0
7841
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...
0
8204
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7965
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6617
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...
1
5712
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...
0
5392
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...
0
3869
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1452
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1184
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...

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.