You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@couchdb.apache.org by "James A. Duncan" <ja...@reasonablysmart.com> on 2008/12/14 17:22:03 UTC
'this' in map functions
I've been following CouchDB for a while, but only started playing with
it seriously very recently.
One thing I keep catching myself with is that map functions receive
the document as a parameter, and I keep looking for it in 'this'.
Maybe that's because I spend too much time with JavaScript, or maybe
it's because I don't spend enough time with CouchDB. A one-line patch
follows, that allows for 'this' to be used as well as the positional
arg.
There may well be good reasons why this isn't sensible, and really
it's just sugar, but I thought I'd dive in and give it a try.
Regards,
James.
--- /Users/jduncan/oldmain.js 2008-12-14 11:04:11.000000000 -0500
+++ /opt/local/share/couchdb/server/main.js 2008-12-14
11:03:16.000000000 -0500
@@ -91,7 +91,7 @@
for (var i = 0; i < funs.length; i++) {
map_results = [];
try {
- funs[i](doc);
+ funs[i].apply(doc, [ doc ]);
buf.push(toJSON(map_results));
} catch (err) {
if (err == "fatal_error") {
Re: 'this' in map functions
Posted by James Duncan <ja...@reasonablysmart.com>.
On 14-Dec-08, at 4:12 PM, Chris Anderson wrote:
> James,
>
> Glad to see you getting into the code base. CouchDB is surprisingly
> easy to hack on once you get familiar with the different modules.
>
> I think in this case its best to stick to the more functional style by
> explicitly passing the doc in. Especially because we'd like to be
> freezing the doc.
Oh definitely, the patch doesn't remove the positional doc, it just
makes the doc "this" as well.
> If you're interested in scripting server side JavaScript, keep an eye
> out for the _external server module which is coming into trunk soon.
> It proxies http requests to a JSON server, so you can pretty easily
> build JavaScript responders.
I am very interested in server side JavaScript -- basically that's
what my company, Reasonably Smart, does. I'm actually looking at
CouchDB as the back-end datastore. There is a bunch of stuff that I'd
like to be able to do with and I'm just getting started :-)
> The code is sitting in davisp's Github repo but should be moving to
> trunk this week.
>
> http://github.com/davisp/couchdb/commits/external3/ (see the last 4
> commits)
That could be very useful - thanks!
Regards,
James.
>
> Chris
>
> On Sun, Dec 14, 2008 at 8:22 AM, James A. Duncan
> <ja...@reasonablysmart.com> wrote:
>>
>> I've been following CouchDB for a while, but only started playing
>> with it
>> seriously very recently.
>>
>> One thing I keep catching myself with is that map functions receive
>> the
>> document as a parameter, and I keep looking for it in 'this'.
>> Maybe that's
>> because I spend too much time with JavaScript, or maybe it's
>> because I don't
>> spend enough time with CouchDB. A one-line patch follows, that
>> allows for
>> 'this' to be used as well as the positional arg.
>>
>> There may well be good reasons why this isn't sensible, and really
>> it's just
>> sugar, but I thought I'd dive in and give it a try.
>>
>> Regards,
>> James.
>>
>> --- /Users/jduncan/oldmain.js 2008-12-14 11:04:11.000000000 -0500
>> +++ /opt/local/share/couchdb/server/main.js 2008-12-14
>> 11:03:16.000000000 -0500
>> @@ -91,7 +91,7 @@
>> for (var i = 0; i < funs.length; i++) {
>> map_results = [];
>> try {
>> - funs[i](doc);
>> + funs[i].apply(doc, [ doc ]);
>> buf.push(toJSON(map_results));
>> } catch (err) {
>> if (err == "fatal_error") {
>>
>>
>
>
>
> --
> Chris Anderson
> http://jchris.mfdz.com
Re: 'this' in map functions
Posted by Chris Anderson <jc...@gmail.com>.
James,
Glad to see you getting into the code base. CouchDB is surprisingly
easy to hack on once you get familiar with the different modules.
I think in this case its best to stick to the more functional style by
explicitly passing the doc in. Especially because we'd like to be
freezing the doc.
If you're interested in scripting server side JavaScript, keep an eye
out for the _external server module which is coming into trunk soon.
It proxies http requests to a JSON server, so you can pretty easily
build JavaScript responders.
The code is sitting in davisp's Github repo but should be moving to
trunk this week.
http://github.com/davisp/couchdb/commits/external3/ (see the last 4 commits)
Chris
On Sun, Dec 14, 2008 at 8:22 AM, James A. Duncan
<ja...@reasonablysmart.com> wrote:
>
> I've been following CouchDB for a while, but only started playing with it
> seriously very recently.
>
> One thing I keep catching myself with is that map functions receive the
> document as a parameter, and I keep looking for it in 'this'. Maybe that's
> because I spend too much time with JavaScript, or maybe it's because I don't
> spend enough time with CouchDB. A one-line patch follows, that allows for
> 'this' to be used as well as the positional arg.
>
> There may well be good reasons why this isn't sensible, and really it's just
> sugar, but I thought I'd dive in and give it a try.
>
> Regards,
> James.
>
> --- /Users/jduncan/oldmain.js 2008-12-14 11:04:11.000000000 -0500
> +++ /opt/local/share/couchdb/server/main.js 2008-12-14
> 11:03:16.000000000 -0500
> @@ -91,7 +91,7 @@
> for (var i = 0; i < funs.length; i++) {
> map_results = [];
> try {
> - funs[i](doc);
> + funs[i].apply(doc, [ doc ]);
> buf.push(toJSON(map_results));
> } catch (err) {
> if (err == "fatal_error") {
>
>
--
Chris Anderson
http://jchris.mfdz.com