473,416 Members | 1,774 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,416 software developers and data experts.

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.prototype.bind = function(){
var fn = this, args = Array.prototype.slice.call(arguments), object =
args.shift();
return function(){
return fn.apply(object,
args.concat(Array.prototype.slice.call(arguments)) );
};
};
var myObject = {};
function myFunction(){
return this == myObject;
}

In the code,
return fn.apply(object,
args.concat(Array.prototype.slice.call(arguments)) );
,
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 1517
lorlarz wrote:
I still have a question regarding the following code,
in a commonly used routine. First, Here's the code in
question:

Function.prototype.bind = function(){
var fn = this, args = Array.prototype.slice.call(arguments), object =
args.shift();
return function(){
return fn.apply(object,
args.concat(Array.prototype.slice.call(arguments)) );
};
};
var myObject = {};
function myFunction(){
return this == myObject;
}

In the code,
return fn.apply(object,
args.concat(Array.prototype.slice.call(arguments)) );
,
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.prototype.bind = function(context){
...
}

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...@gmail.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.prototype.bind = function(){
var fn = this, args = Array.prototype.slice.call(arguments), object=
args.shift();
return function(){
return fn.apply(object,
args.concat(Array.prototype.slice.call(arguments)) );
* *};
};
var myObject = {};
function myFunction(){
return this == myObject;
}
In the code,
return fn.apply(object,
args.concat(Array.prototype.slice.call(arguments)) );
,
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.prototype.bind = function(context){
* ...

}

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...@gmail.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.prototype.bind = function(){
var fn = this, args = Array.prototype.slice.call(arguments), object =
args.shift();
return function(){
return fn.apply(object,
args.concat(Array.prototype.slice.call(arguments )));
};
};
var myObject = {};
function myFunction(){
return this == myObject;
}
In the code,
return fn.apply(object,
args.concat(Array.prototype.slice.call(arguments )));
,
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.prototype.bind = function(context){
...

}

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.prototype.bind:

If, for example, the next version of EcmaScript includes
Function.prototype.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.prototype.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.prototype.bind.

Garrett
Sep 7 '08 #4
On Sep 6, 10:12*pm, dhtml <dhtmlkitc...@gmail.comwrote:
lorlarz wrote:
On Sep 6, 12:37 pm, dhtml <dhtmlkitc...@gmail.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.prototype.bind = function(){
var fn = this, args = Array.prototype.slice.call(arguments), object =
args.shift();
return function(){
return fn.apply(object,
args.concat(Array.prototype.slice.call(arguments) ));
* *};
};
var myObject = {};
function myFunction(){
return this == myObject;
}
In the code,
return fn.apply(object,
args.concat(Array.prototype.slice.call(arguments) ));
,
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.prototype.bind = function(context){
* ...
}
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.prototype.bind:

If, for example, the next version of EcmaScript includes
Function.prototype.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.prototype.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.prototype.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.prototype.bind = function(){
var fn = this, args = Array.prototype.slice.call(arguments), object =
args.shift();
return function(){
return fn.apply(object,
args.concat(Array.prototype.slice.call(arguments)) );
};

};
var myObject = {};
function myFunction(thename){

alert(thename);
return this == myObject;

}
alert((myFunction.bind(myObject, "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...@gmail.comwrote:
lorlarz wrote:
On Sep 6, 12:37 pm, dhtml <dhtmlkitc...@gmail.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.prototype.bind = function(){
var fn = this, args = Array.prototype.slice.call(arguments), object =
args.shift();
return function(){
return fn.apply(object,
args.concat(Array.prototype.slice.call(arguments) ));
* *};
};
var myObject = {};
function myFunction(){
return this == myObject;
}
In the code,
return fn.apply(object,
args.concat(Array.prototype.slice.call(arguments) ));
,
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.prototype.bind = function(context){
* ...
}
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.prototype.bind:

If, for example, the next version of EcmaScript includes
Function.prototype.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.prototype.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.prototype.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.prototype.bind = function(){
var fn = this, args = Array.prototype.slice.call(arguments), object =
args.shift();
return function(){
return fn.apply(object,
args.concat(Array.prototype.slice.call(arguments)) );

};

};

var myObject = {};
function myFunction(thename){

alert(thename);
return this == myObject;

}

alert((myFunction.bind(myObject, "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...@gmail.comwrote:
>lorlarz wrote:
>>On Sep 6, 12:37 pm, dhtml <dhtmlkitc...@gmail.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.prototype.bind = function(){
var fn = this, args = Array.prototype.slice.call(arguments), object =
args.shift();
return function(){
return fn.apply(object,
args.concat(Array.prototype.slice.call(arguments)) );

};

};

var myObject = {};
function myFunction(thename){

alert(thename);
return this == myObject;

}

alert((myFunction.bind(myObject, "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.prototype.bind = function(context) {

};

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...@gmail.comwrote:
lorlarz wrote:
On Sep 6, 10:12 pm, dhtml <dhtmlkitc...@gmail.comwrote:
lorlarz wrote:
On Sep 6, 12:37 pm, dhtml <dhtmlkitc...@gmail.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.prototype.bind = function(){
var fn = this, args = Array.prototype.slice.call(arguments), object=
args.shift();
return function(){
return fn.apply(object,
args.concat(Array.prototype.slice.call(arguments)) );
* * * * };
};
var myObject = {};
function myFunction(thename){
alert(thename);
return this == myObject;
}
alert((myFunction.bind(myObject, "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.prototype.bind = function(context) {

};

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...@gmail.comwrote:
>lorlarz wrote:
>>On Sep 6, 10:12 pm, dhtml <dhtmlkitc...@gmail.comwrote:
lorlarz wrote:
On Sep 6, 12:37 pm, dhtml <dhtmlkitc...@gmail.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.prototype.bind = function(){
var fn = this, args = Array.prototype.slice.call(arguments), object =
args.shift();
return function(){
return fn.apply(object,
args.concat(Array.prototype.slice.call(arguments )));
};
};
var myObject = {};
function myFunction(thename){
alert(thename);
return this == myObject;
}
alert((myFunction.bind(myObject, "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.prototype.bind = function(context) {

};

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...@gmail.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
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...
1
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...
9
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...
687
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...
34
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"...
4
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...
3
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
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...
8
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...
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: 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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
0
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...
0
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...
0
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,...
0
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...

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.