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

How are arguments a legit argument to Array.slice?

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)();
Aug 28 '08 #1
12 2018
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.

Aug 28 '08 #2
On Aug 29, 6:36*am, lorlarz <lorl...@gmail.comwrote:
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

Aug 29 '08 #3
On Aug 28, 6:16*pm, "Richard Cornford" <Rich...@litotes.demon.co.uk>
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.
Aug 29 '08 #4
On Aug 28, 8:22*pm, RobG <rg...@iinet.net.auwrote:
On Aug 29, 6:36*am, lorlarz <lorl...@gmail.comwrote:
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.
Aug 29 '08 #5
lorlarz <lo*****@gmail.comwrites:
On Aug 29, 9:34*am, "Richard Cornford" <Rich...@litotes.demon.co.uk>
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.'
Aug 29 '08 #6
lorlarz <lo*****@gmail.comwrites:

(you can trim your quotings a lot more without losing context)
Let me see if I can spell the situation out for myself.
The call method looks for a real array
No. The call method looks for an object and, optionally, some more
arguments.
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.
The call method the calls the "Array.prototype.slice" function with
"this" set to that object. I.e., when calling "slice", it is as if it
was a method on the arguments object.

The slice function is generic, and doesn't need an array as its "this"
value, just something with a "length" property.
The slice function then creates a new array that contains the same
elements as its "this" value. This converts the arguments object
to an array (or rather, creates a new array).
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); ??
Array.prototype.slice references a function. This is the same function
that Array objects inherit through their prototype chain. I.e., if
var a = [];
is an array, then Array.prototype.slice and a.slice is the same function.

/L
--
Lasse Reichstein Nielsen
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Aug 29 '08 #7
RobG <rg***@iinet.net.auwrites:
On Aug 29, 6:36*am, lorlarz <lorl...@gmail.comwrote:
>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.
The above code allows partial application. I.e.,

function foo(a,b,c) { /*...*/ }
var foob = foo.bind(object, 42);
foob(37,"doh"); // equvialent to object.call(foo,42,37,"doh")

The conversion from arguments-object to array is probably used because
the "concat" function requires an array as argument, and preferably an
array to work on (although it might work on an arguments object).

/L
--
Lasse Reichstein Nielsen
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Aug 29 '08 #8
On Aug 29, 2:40*pm, Lasse Reichstein Nielsen <lrn.unr...@gmail.com>
wrote:
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.
[snip]
--
Lasse Reichstein Nielsen
*DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
* 'Faith without judgement merely degrades the spirit divine.'
Believe it or not, I think I understand. The function is
being called with only one argument (the arguments object)
and thus that is the context of the function call on
the slice method. Did I say that correctly?
Aug 29 '08 #9
On Aug 29, 2:48*pm, Lasse Reichstein Nielsen <lrn.unr...@gmail.com>
wrote:
[snip]
>
Array.prototype.slice references a function. This is the same function
that Array objects inherit through their prototype chain. I.e., if
*var a = [];
is an array, then Array.prototype.slice and a.slice is the same function.

/L
--
Lasse Reichstein Nielsen
*DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
* 'Faith without judgement merely degrades the spirit divine.'
Again, I believe I understand. If you are not
dealing with a specific array, you need to
call Array.prototype to use the slice method.

I am still not clear on why Array.slice would not
work though. Have an old Java background
that might be causing confusion.
Aug 29 '08 #10
On Aug 29, 3:01*pm, Lasse Reichstein Nielsen <lrn.unr...@gmail.com>
wrote:
RobG <rg...@iinet.net.auwrites:
On Aug 29, 6:36*am, lorlarz <lorl...@gmail.comwrote:
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.

The above code allows partial application. I.e.,

*function foo(a,b,c) { /*...*/ }
*var foob = foo.bind(object, 42);
*foob(37,"doh"); // equvialent to object.call(foo,42,37,"doh")

The conversion from arguments-object to array is probably used because
the "concat" function requires an array as argument, and preferably an
array to work on (although it might work on an arguments object).

/L
--
Lasse Reichstein Nielsen
*DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
* 'Faith without judgement merely degrades the spirit divine.'- Hide quoted text -
Indeed partial application is exactly the context the
code came up in. I bow to your expertise. You certainly
know what you are talking about. You surely
have my respect.

I will strive to make up for google's short-comings
in responding to other. Best wishes.
Aug 29 '08 #11
lorlarz wrote:
On Aug 28, 6:16 pm, "Richard Cornford" <Rich...@litotes.demon.co.uk>
wrote:
>lorlarz wrote


>>In the code sample below, how are arguments a legitimate
argument to Array.slice?
[snip]
>
This code is from a draft of a new JavaScript book
by a major expert.
LOL. I think I know whose book this is.

He is grabbing
the arguments property of the function and passing
it as an argument to Array.prototype.slice
Is he using his sword?
The contents of the arguments property that the function
receives is just the object myObject, so there seems to be
nothing to slice.
I think I see Lasse is helping you just a little too much. I've
mentioned the EcmaScript specification to you before. Do a little
research there first. You can search that specification for "call(" .
Then search for "slice(" and see what that does.

You can find the "unofficial edition" spec in HTML on bclary.com and the
official version in pdf here:
http://www.ecma-international.org/pu...s/Ecma-262.htm

Garrett
Aug 29 '08 #12
On Aug 29, 5:48*pm, dhtml <dhtmlkitc...@gmail.comwrote:
lorlarz wrote:
On Aug 28, 6:16 pm, "Richard Cornford" <Rich...@litotes.demon.co.uk>
wrote:
lorlarz wrote
>In the code sample below, how are arguments a legitimate
argument to Array.slice?

[snip]
This code is from a draft of a new JavaScript book
by a major expert. *

LOL. I think I know whose book this is.

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

Is he using his sword?
The contents of the arguments property that the function
receives is just the object myObject, so there seems to be
nothing to slice.

I think I see Lasse is helping you just a little too much. *I've
mentioned the EcmaScript specification to you before. Do a little
research there first. You can search that specification for "call(" .
Then search for "slice(" and see what that does.

You can find the "unofficial edition" spec in HTML on bclary.com and the
official version in pdf here:http://www.ecma-international.org/pu...s/Ecma-262.htm

Garrett
I really do not consider you to have a legitimate criticism
of me asking the question. IF THIS rather hard-to-understand
routine is not something one can ask about here, WHAT IS??
One can _always_ find _all_ the answers elsewhere (FACT), but
can sometimes learn more more clearly from some of the better
people HERE. And, that is what the group is about. (Otherwise,
if someone can always find the answer elsewhere & in _your view_
should not ask the question here, NO QUESTIONS would be
"legitimate" here!!!! Isn't this silly???)

Now, how is this particular thing is harder than just call and
slice? Well, first you must know that arguments is an object
and that an object is seen as a single entity. Then you must
realize that the first argument to call simply sets the
context of "this" plus that slice is just being using to make
this object a one element array. This is not something all
readily understand.

Truth is I have a remaining question . It is that in
the code,
return fn.apply(object,
args.concat(Array.prototype.slice.call(arguments)) );
,
it seems apply (there) is potentially getting an extra,
array element (because the first element in arguments
is still the object (the one setting the context of "this).

Thus, I am not sure I still fully understand and
am trying not to be embarrassed asking the question
HERE, despite criticism of asking the question I
aready have.

So, Lasse, I am still listening.
Aug 31 '08 #13

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

Similar topics

4
by: KanZen | last post by:
I'm trying to understand the difference between __setitem__ and an ordinary method. For example: >>> class A(object): def __getitem__(self, *args): print len(args) def normalMethod(self,...
66
by: Darren Dale | last post by:
Hello, def test(data): i = ? This is the line I have trouble with if i==1: return data else: return data a,b,c,d = test()
6
by: Melkor Ainur | last post by:
Hello, I'm attempting to build an interpreter for a pascal-like language. Currently, I don't generate any assembly. Instead, I just build an abstract syntax tree representing what I've parsed...
5
by: matevz bradac | last post by:
Hi, I'm trying to implement delayed function execution, similar to OpenGL's display lists. It looks something like this: beginList (); fcn1 (); fcn2 ();
11
by: truckaxle | last post by:
I am trying to pass a slice from a larger 2-dimensional array to a function that will work on a smaller region of the array space. The code below is a distillation of what I am trying to...
41
by: Telmo Costa | last post by:
Hi. I have the following code: -------------------------------------- function Tunnel() { //arguments(???); } function Sum() { var sum = 0; for (i=0; i<arguments.length; i++) sum +=...
11
by: -Lost | last post by:
I have this generic function (JavaScript newbie here, so don't think I am going to impress you): function blah() { var container = ''; for(var i = 0; i < arguments.length; i++) { container...
15
by: arnuld | last post by:
i am not able to figure out the error: /* C++ Primer - 4/e * * exercise 7.16 * STATEMENT: * write a programme that accepts the arguments to main. print * the values passed to main. *...
5
by: Peter Michaux | last post by:
There seem to be some options for converting the arguments object inside a function to an instance of Array. I'm curious if anyone has encountered any problems with any particular techniques for...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

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.