Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Functions and assignment precedence question

Reply
Thread Tools

Functions and assignment precedence question

 
 
Uncle Roastie
Guest
Posts: n/a
 
      09-02-2011
var decoratedFib = function fib(n) {
return decoratedFib(n-1) + decoratedFib(n-2);
}.decorate({...});


My question:
Does "decoratedFib" get assigned "function fib" or
the result of executing "function decorate"?

 
Reply With Quote
 
 
 
 
Richard Cornford
Guest
Posts: n/a
 
      09-02-2011
On Sep 2, 4:43 pm, Uncle Roastie wrote:
> var decoratedFib = function fib(n) {
> return decoratedFib(n-1) + decoratedFib(n-2);
>
> }.decorate({...});
>
> My question:
> Does "decoratedFib" get assigned "function fib"


No.

> or
> the result of executing "function decorate"?


If you mean the result of calling a 'decorate' method of the function
object created by the preceding expression (and assuming one of the
Function or Object prototypes has a 'decorate' method) then the value
assigned to - decoratedFib - is the return value from the call to that
method.

Richard.
 
Reply With Quote
 
 
 
 
Scott Sauyet
Guest
Posts: n/a
 
      09-06-2011
Uncle Roastie wrote:
> var decoratedFib = function fib(n) {
> * * return decoratedFib(n-1) + decoratedFib(n-2);
>
> }.decorate({...});
>
> My question:
> Does "decoratedFib" get assigned "function fib" or
> the result of executing "function decorate"?


Richard Cornford's answer is correct, but it's not as clear as his
prose usually is.

If you look at Javascript's operator precedence ([1]), you might note
that the member operator (".") binds well before the assignment
operator ("="), so anything of this form:

var z = x.y()

will assign to `z` the value of calling the function assigned to the
property `y` of the object `x` in the context of `x`. The fact that
in this case `x` is a function doesn't change that.

So your `decoratedFib` variable will be assigned the result of the
call to `decorate` performed on the named anonymous (!!) function.
The only way that will work is if `decorate` has been added to the
prototype chain of the anonymous function, that is, if it's on
Function.prototype (or possibly Object.prototype).

If decorate looked like this:

Function.prototype.decorate= function() {
var cache = {}, self = this;
return function(n) {
return (n in cache) ? cache[n] : (cache[n] = self(n));
};
};

then you would have a new Fibonacci function that memoized the
results, calculating larger ones much more quickly than the
undecorated version. Of course, then `decorate` should probably be
renamed `memoize`. (Note too that this is not a general-purpose
memoization function. It only works for unary functions of numbers or
strings. It's not hard to build a more general-purpose one, but it's
outside the scope of this discussion.


-- Scott

[1] One good source is https://developer.mozilla.org/en/Jav...tor_Precedence
 
Reply With Quote
 
Dr J R Stockton
Guest
Posts: n/a
 
      09-07-2011
In comp.lang.javascript message <06defdf6-cb61-42b3-8e26-524e673f7ff1@t3
g2000vbe.googlegroups.com>, Tue, 6 Sep 2011 05:24:36, Scott Sauyet
<(E-Mail Removed)> posted:

>
>then you would have a new Fibonacci function that memoized the
>results, calculating larger ones much more quickly than the
>undecorated version.


I have a couple of other examples where cacheing helps (with less subtle
code) at <http://www.merlyn.demon.co.uk/js-misc0.htm#Cash>.

--
(c) John Stockton, nr London UK. ?@merlyn.demon.co.uk IE8 FF3 Op12 Sf5 Cr12
news:comp.lang.javascript FAQ <http://www.jibbering.com/faq/index.html>.
<http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
 
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
precedence of single-line rescue and assignment matt neuburg Ruby 7 05-08-2008 04:37 AM
A question of Precedence: Two functions from a C stack. benmarco7@mail.com C Programming 0 04-23-2008 07:44 PM
operator precedence of assignment Daniel DeLorme Ruby 1 01-02-2007 03:55 PM
please help me in distinguish redefining functions, overloading functions and overriding functions. Xiangliang Meng C++ 1 06-21-2004 03:11 AM
relative precedence of ?: and assignment Andrew Koenig C++ 10 07-14-2003 01:07 PM



Advertisments