473,624 Members | 2,302 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How are arguments a legit argument to Array.slice?

In the code sample below, how are arguments a legitimate
argument to Array.slice?
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;
}

myFunction.bind (myObject)();
Aug 28 '08 #1
12 2051
lorlarz wrote
In the code sample below, how are arguments a legitimate
argument to Array.slice?

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

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.protot ype.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.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)));};
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...@litote s.demon.co.uk>
wrote:
lorlarz wrote


In the code sample below, how are arguments a legitimate
argument to Array.slice?
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;
}
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.protot ype.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.ne t.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.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)));};

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...@litote s.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(arg uments)

This calls the "call" method on the function "Array.prototyp e.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.somen ame = Array.prototype .slice;
... arguments.somen ame();

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/rasterTriangleD OM.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.prototyp e.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(arg uments); ??
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/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Aug 29 '08 #7
RobG <rg***@iinet.ne t.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.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)));};

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/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Aug 29 '08 #8
On Aug 29, 2:40*pm, Lasse Reichstein Nielsen <lrn.unr...@gma il.com>
wrote:
I'm jumping into the middle here, but was the problem the
interpretation of
*Array.prototyp e.slice.call(ar guments)

This calls the "call" method on the function "Array.prototyp e.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.some name = Array.prototype .slice;
*... arguments.somen ame();

just without actualy creating a property on the arguments object.
[snip]
--
Lasse Reichstein Nielsen
*DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.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...@gma il.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/rasterTriangleD OM.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

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

Similar topics

4
8014
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, *args): print len(args) >>> a=A()
66
4967
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
4011
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 from a user's program and use a C backend to evaluate the tree. I'm using Lex, Yacc and C. Now, there are some functions that are built into this language and others which are not. The problem I'm having is that I haven't found a way to represent...
5
411
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
4453
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 accomplish. // - - - - - - - - begin code - - - - - - - typedef int sm_t; typedef int bg_t; sm_t sm; bg_t bg;
41
2541
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 += arguments;
11
4055
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 += arguments + '\n'; }
15
2313
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
1663
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 this From <URL: http://groups.google.com/group/comp.lang.javascript/msg/98c27152ea5c7d55?dmode=source> var args = .slice.call(arguments,1); var args = Array.prototype.slice.call(arguments, 1); I've tried the following which is short and it...
0
8177
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8681
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8629
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8341
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
7170
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4084
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4183
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2611
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1793
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.