Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > How are arguments a legit argument to Array.slice?

Reply
Thread Tools

How are arguments a legit argument to Array.slice?

 
 
lorlarz
Guest
Posts: n/a
 
      08-28-2008
In the code sample below, how are arguments a legitimate
argument to Array.slice?


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;
}

myFunction.bind(myObject)();
 
Reply With Quote
 
 
 
 
Richard Cornford
Guest
Posts: n/a
 
      08-28-2008
lorlarz wrote
> In the code sample below, how are arguments a legitimate
> argument to Array.slice?
>
> 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;
> }
>
> myFunction.bind(myObject)();


Code that is intended to be read by humans (so anything posted to Usenet
with the intention of its being examined by other participants in a
group) should be indented (using spaces in posts not tabs as tabs don't
receive uniform (or necessarily useful) handling in newsreader
software).

In the code above - arguments - is never an argument to Array.slice. All
occurrences of - arguments - as an argument are as an argument to -
Function.prototype.call -.

Richard.

 
Reply With Quote
 
 
 
 
RobG
Guest
Posts: n/a
 
      08-29-2008
On Aug 29, 6:36*am, lorlarz <(E-Mail Removed)> wrote:
> In the code sample below, how are arguments a legitimate
> *argument to Array.slice?
>
> 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)) );};


Seems to me that:

return fn.apply(object, args);

is sufficient.


--
Rob

 
Reply With Quote
 
lorlarz
Guest
Posts: n/a
 
      08-29-2008
On Aug 28, 6:16*pm, "Richard Cornford" <(E-Mail Removed)>
wrote:
> lorlarz wrote
>
>
>
>
>
> > In the code sample below, how are arguments a legitimate
> > argument to Array.slice?

>
> > 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;
> > }

>
> > myFunction.bind(myObject)();

>
> Code that is intended to be read by humans (so anything posted to Usenet
> with the intention of its being examined by other participants in a
> group) should be indented (using spaces in posts not tabs as tabs don't
> receive uniform (or necessarily useful) handling in newsreader
> software).
>
> In the code above - arguments - is never an argument to Array.slice. All
> occurrences of - arguments - as an argument are as an argument to -
> Function.prototype.call -.
>
> Richard.- Hide quoted text -
>
> - Show quoted text -


This code is from a draft of a new JavaScript book
by a major expert. He is grabbing
the arguments property of the function and passing
it as an argument to Array.prototype.slice

The contents of the arguments property that the function
receives is just the object myObject, so there seems to be
nothing to slice.

I may have to ask the author himself; perhaps this
is a code error in the draft.
 
Reply With Quote
 
lorlarz
Guest
Posts: n/a
 
      08-29-2008
On Aug 28, 8:22*pm, RobG <(E-Mail Removed)> wrote:
> On Aug 29, 6:36*am, lorlarz <(E-Mail Removed)> wrote:
>
> > In the code sample below, how are arguments a legitimate
> > *argument to Array.slice?

>
> > 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)) );};

>
> Seems to me that:
>
> * return fn.apply(object, args);
>
> is sufficient.
>
> --
> Rob


Yes, what you say at least makes sense to me.
 
Reply With Quote
 
Richard Cornford
Guest
Posts: n/a
 
      08-29-2008
lorlarz wrote:
>On Aug 28, 6:16 pm, Richard Cornford wrote:
>> lorlarz wrote

><snip>
>>> In the code sample below, how are arguments a legitimate
>>> argument to Array.slice?

>>
>>> 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;
>>> }

>>
>>> myFunction.bind(myObject)();

>>
>> Code that is intended to be read by humans ...
>> should be indented ...

><snip>
>> In the code above - arguments - is never an argument to
>> Array.slice. All occurrences of - arguments - as an argument
>> are as an argument to - Function.prototype.call -.
>>
>> Richard.- Hide quoted text -

^^^^^^^^^^^^^^^^^^^^
>> - Show quoted text -

^^^^^^^^^^^^^^^^^^^^^

I did _not_ write that final text. Why are you attributing words to me
that I did not write? That represents a serious breach of netiquette,
and is disingenuous at the very least.

> This code is from a draft of a new JavaScript book


And the code is indented in that presentation (that is not a question,
and I don't need to see it in order to *know* that it *is* indented).

> by a major expert.


YMMV

> He is grabbing


What it the technical meaning of "grabbing" in javascript?

> the arguments property of the function and passing
> it as an argument to Array.prototype.slice


No he is not. That is not what the code above does, as I have already
told you once; at no point in the code above does an - arguments -
object get used as an argument to the - slice - method.

> The contents of the arguments property that the function
> receives is just the object myObject, so there seems to be
> nothing to slice.


The - slice - method is used in order to transfer the values of the
'array index' properties of an - arguments - object to corresponding
properties of a newly created Array object. This allows - shift - to be
called on that Array in order to extract its first element (the
reference to the - myObject - object) and leave any other values for
later use in any call to the - fn - function.

> I may have to ask the author himself;


You still may have to work out what the correct question is.

> perhaps this is a code error in the draft.


No, the code is fine; a straight reproduction of a run-of-the-mill -
bind - implementation. Obviously the accompanying text is not very
effective as an explanation of what it going on here, but that is 'major
experts' for you.

Richard.

 
Reply With Quote
 
lorlarz
Guest
Posts: n/a
 
      08-29-2008
On Aug 29, 9:34*am, "Richard Cornford" <(E-Mail Removed)>
wrote:
> lorlarz wrote:
> >On Aug 28, 6:16 pm, Richard Cornford wrote:
> >> lorlarz wrote

> ><snip>
> >>> In the code sample below, how are arguments a legitimate
> >>> argument to Array.slice?

>
> >>> 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;
> >>> }

>
> >>> myFunction.bind(myObject)();

>
> >> Code that is intended to be read by humans ...
> >> should be indented ...

> ><snip>
> >> In the code above - arguments - is never an argument to
> >> Array.slice. All occurrences of - arguments - as an argument
> >> are as an argument to - Function.prototype.call -.

>
> >> Richard.- Hide quoted text -

>
> * * * * * *^^^^^^^^^^^^^^^^^^^^>> - Show quoted text -
>
> * *^^^^^^^^^^^^^^^^^^^^^
>
> I did _not_ write that final text. Why are you attributing words to me
> that I did not write? That represents a serious breach of netiquette,
> and is disingenuous at the very least.
>
> > This code is from a draft of a new JavaScript book

>
> And the code is indented in that presentation (that is not a question,
> and I don't need to see it in order to *know* that it *is* indented).
>
> > by a major expert.

>
> YMMV
>
> > He is grabbing

>
> What it the technical meaning of "grabbing" in javascript?
>
> > the arguments property of the function and passing
> > it as an argument to Array.prototype.slice

>
> No he is not. That is not what the code above does, as I have already
> told you once; at no point in the code above does an - arguments -
> object get used as an argument to the - slice - method.
>
> > The contents of the arguments property that the function
> > receives is just the object myObject, so there seems to be
> > nothing to slice.

>
> The - slice - method is used in order to transfer the values of the
> 'array index' properties of an - arguments - object to corresponding
> properties of a newly created Array object. This allows - shift - to be
> called on that Array in order to extract its first element (the
> reference to the - myObject - object) and leave any other values for
> later use in any call to the - fn - function.
>
> > I may have to ask the author himself;

>
> You still may have to work out what the correct question is.
>
> > perhaps this is a code error in the draft.

>
> No, the code is fine; a straight reproduction of a run-of-the-mill -
> bind - implementation. Obviously the accompanying text is not very
> effective as an explanation of what it going on here, but that is 'major
> experts' for you.
>
> Richard.- Hide quoted text -
>
> - Show quoted text -


You were automatically quoted by google and it looks correct to
me. There is just a single ">" in front of your lines and
">>" in front of where you quote me.

In any case, I simple took the google quote and did nothing
but reply below. If there is any validity to your
concern, you will have to take it up with google.

I am trying, but do not yet quite understand your
explanation. Could you spell it out bit by bit
more?

I agree with you that the expert did indeed
fail to explain the code, becuase he provides
no explanation. You may have done a bit better,
but I am still unclear. I think you may be able
to bring me "over the line" if you just detail
a bit more what happens.

(As with my last reply, I used the google
quotinog of your post and did nothing
to alter the way it was quoted.)
 
Reply With Quote
 
lorlarz
Guest
Posts: n/a
 
      08-29-2008
On Aug 29, 10:25*am, lorlarz <(E-Mail Removed)> wrote:
> On Aug 29, 9:34*am, "Richard Cornford" <(E-Mail Removed)>
> wrote:
>
>
>
>
>
> > lorlarz wrote:
> > >On Aug 28, 6:16 pm, Richard Cornford wrote:
> > >> lorlarz wrote
> > ><snip>
> > >>> In the code sample below, how are arguments a legitimate
> > >>> argument to Array.slice?

>
> > >>> 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;
> > >>> }

>
> > >>> myFunction.bind(myObject)();

>
> > >> Code that is intended to be read by humans ...
> > >> should be indented ...
> > ><snip>
> > >> In the code above - arguments - is never an argument to
> > >> Array.slice. All occurrences of - arguments - as an argument
> > >> are as an argument to - Function.prototype.call -.

>
> > >> Richard.- Hide quoted text -

>
> > * * * * * *^^^^^^^^^^^^^^^^^^^^>> - Show quoted text -

>
> > * *^^^^^^^^^^^^^^^^^^^^^

>
> > I did _not_ write that final text. Why are you attributing words to me
> > that I did not write? That represents a serious breach of netiquette,
> > and is disingenuous at the very least.

>
> > > This code is from a draft of a new JavaScript book

>
> > And the code is indented in that presentation (that is not a question,
> > and I don't need to see it in order to *know* that it *is* indented).

>
> > > by a major expert.

>
> > YMMV

>
> > > He is grabbing

>
> > What it the technical meaning of "grabbing" in javascript?

>
> > > the arguments property of the function and passing
> > > it as an argument to Array.prototype.slice

>
> > No he is not. That is not what the code above does, as I have already
> > told you once; at no point in the code above does an - arguments -
> > object get used as an argument to the - slice - method.

>
> > > The contents of the arguments property that the function
> > > receives is just the object myObject, so there seems to be
> > > nothing to slice.

>
> > The - slice - method is used in order to transfer the values of the
> > 'array index' properties of an - arguments - object to corresponding
> > properties of a newly created Array object. This allows - shift - to be
> > called on that Array in order to extract its first element (the
> > reference to the - myObject - object) and leave any other values for
> > later use in any call to the - fn - function.

>
> > > I may have to ask the author himself;

>
> > You still may have to work out what the correct question is.

>
> > > perhaps this is a code error in the draft.

>
> > No, the code is fine; a straight reproduction of a run-of-the-mill -
> > bind - implementation. Obviously the accompanying text is not very
> > effective as an explanation of what it going on here, but that is 'major
> > experts' for you.

>
> > Richard.- Hide quoted text -

>
> > - Show quoted text -

>
> You were automatically quoted by google and it looks correct to
> me. * There is just a single ">" in front of your lines and
> ">>" in front of where you quote me.
>
> In any case, I simple took the google quote and did nothing
> but reply below. *If there is any validity to your
> concern, you will have to take it up with google.
>
> I am trying, but do not yet quite understand your
> explanation. *Could you spell it out bit by bit
> more?
>
> I agree with you that the expert did indeed
> fail to explain the code, becuase he provides
> no explanation. *You may have done a bit better,
> but I am still unclear. *I think you may be able
> to bring me "over the line" if you just detail
> a bit more what happens.
>
> (As with my last reply, I used the google
> *quotinog of your post and did nothing
> *to alter the way it was quoted.)- Hide quoted text -
>
> - Show quoted text -


P.S. I do see what google is doing to you, Cornford,
and it just did it to me ABOVE. I will make sure
I make up for the terrible thing google does
ALTHOUGH THAT "quoted text" IS ABSOLUTELY EMPTY
and only a weird person would worry or take offense.

No offense.

Still, again, Cornford, I would be most appreciative
if you could detail you last response (if possible).
 
Reply With Quote
 
lorlarz
Guest
Posts: n/a
 
      08-29-2008
On Aug 29, 9:34*am, "Richard Cornford" <(E-Mail Removed)>
wrote:
> lorlarz wrote:
> >On Aug 28, 6:16 pm, Richard Cornford wrote:
> >> lorlarz wrote

> ><snip>
> >>> In the code sample below, how are arguments a legitimate
> >>> argument to Array.slice?

>
> >>> 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;
> >>> }

>
> >>> myFunction.bind(myObject)();

>
> >> Code that is intended to be read by humans ...
> >> should be indented ...

> ><snip>
> >> In the code above - arguments - is never an argument to
> >> Array.slice. All occurrences of - arguments - as an argument
> >> are as an argument to - Function.prototype.call -.

>
> >> Richard.- Hide quoted text -

>
> * * * * * *^^^^^^^^^^^^^^^^^^^^>> - Show quoted text -
>
> * *^^^^^^^^^^^^^^^^^^^^^
>
> I did _not_ write that final text. Why are you attributing words to me
> that I did not write? That represents a serious breach of netiquette,
> and is disingenuous at the very least.
>
> > This code is from a draft of a new JavaScript book

>
> And the code is indented in that presentation (that is not a question,
> and I don't need to see it in order to *know* that it *is* indented).
>
> > by a major expert.

>
> YMMV
>
> > He is grabbing

>
> What it the technical meaning of "grabbing" in javascript?
>
> > the arguments property of the function and passing
> > it as an argument to Array.prototype.slice

>
> No he is not. That is not what the code above does, as I have already
> told you once; at no point in the code above does an - arguments -
> object get used as an argument to the - slice - method.
>
> > The contents of the arguments property that the function
> > receives is just the object myObject, so there seems to be
> > nothing to slice.

>
> The - slice - method is used in order to transfer the values of the
> 'array index' properties of an - arguments - object to corresponding
> properties of a newly created Array object. This allows - shift - to be
> called on that Array in order to extract its first element (the
> reference to the - myObject - object) and leave any other values for
> later use in any call to the - fn - function.
>
> > I may have to ask the author himself;

>
> You still may have to work out what the correct question is.
>
> > perhaps this is a code error in the draft.

>
> No, the code is fine; a straight reproduction of a run-of-the-mill -
> bind - implementation. Obviously the accompanying text is not very
> effective as an explanation of what it going on here, but that is 'major
> experts' for you.
>
> Richard.


Let me see if I can spell the situation out for myself.
The call method looks for a real array and thus converts
the arguments object into a real array and then sends
the first argument of that resultant array
to the call method as the context
for "this" and IF there where any more arguments
(which in this example there are not), they would
be passed to the called function (Array.slice) IN
that context.

If this is correct, I guess my only remaining question
is what is the word "prototype" in this statement
var args = Array.prototype.slice.call(arguments); ??

I am doing all I can for myself. Can you
bring me "over the line" , Richard??


 
Reply With Quote
 
Lasse Reichstein Nielsen
Guest
Posts: n/a
 
      08-29-2008
lorlarz <(E-Mail Removed)> writes:

> On Aug 29, 9:34*am, "Richard Cornford" <(E-Mail Removed)>
> wrote:
>> Richard.- Hide quoted text -
>>
>> - Show quoted text -

>
> You were automatically quoted by google and it looks correct to
> me. There is just a single ">" in front of your lines and
> ">>" in front of where you quote me.


There is also the text "- Hide quoted text -" and "- Show quoted
text", which are attributed to Richard, and that wasn't there in his
message.

> In any case, I simple took the google quote and did nothing
> but reply below. If there is any validity to your
> concern, you will have to take it up with google.


Uhm, no. You are responsible for the tools you chose to use.
If Google Groups doesn't work satisfactory, you should take
it up with Google.

> I am trying, but do not yet quite understand your
> explanation. Could you spell it out bit by bit
> more?


I'm jumping into the middle here, but was the problem the
interpretation of
Array.prototype.slice.call(arguments)

This calls the "call" method on the function "Array.prototype.slice"
with the argument "arguments".

This gives approximatly the same effect as placing the function
as a method of the arguments object and calling it.

arguments.somename = Array.prototype.slice;
... arguments.somename();

just without actualy creating a property on the arguments object.


> (As with my last reply, I used the google
> quotinog of your post and did nothing
> to alter the way it was quoted.)


And it again introduced spurious text. Maybe you will have
to do something to avoid this, since nothing apparently
doesn't do the job

--
Lasse Reichstein Nielsen
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
FS: 1 MCSE security exam voucher expiring 11/30/04 - legit offer - ebay sale fell through Audi S4 pilot MCSE 0 11-28-2004 04:21 PM
legit product or dump Doom MCSE 12 06-22-2004 06:49 PM
70-306 - Actual Legit Developer Questions Brunswick Lowe MCSD 3 01-13-2004 12:02 AM
Is X-Cleaner Legit? I think it may have wiped my registry. What do i do? Look in my eyes and you'll find me Computer Support 10 08-31-2003 03:15 AM



Advertisments