Live data from Hacker News

Offer HN: I'll answer your javascript questions

news.ycombinator.com

21–30 of 45 posts

Re: Offer HN: I'll answer your javascript questions

#21
post #13
post #4

setTimeout drives me crazy. How can I implement "sleep" without going insane?

So you should never think of "sleep" in javascript. Since the language is single-threaded, really if you pause the script you're pausing the entire environment (whether its in the browser or on the server in something like Node.js). But, to answer your question: // some code runs here var continue = function(data){ //more code runs in here after sleep } setTimeout(function(){ continue(data); },2000); A different patt…

IMO, a better pattern is to use lexical closures in continuation-passing style to share state:

    function sleep(time, continuation) {
        setTimeout(continuation, time);
    }

    function start_process(x) {
        var y = foo(x);
        sleep(1000, function() {
        var z = bar(x, y);
        sleep(2000, function() {
        baz(x, y, z);
        });});
    }
    
    start_process(42);

Re: Offer HN: I'll answer your javascript questions

#22

Ok: What are some good object creation strategies you employ. Basically I know with javascript there are a handful of different ways to create objects. Some (using a framework) give you java-like inheritence, some other methodologies. Much of the time you don't really need to give a shit about inheritence as you are more focused on containership (the word eludes me, probably will remember it 15 minutes after post). S…

encapsulation?

Re: Offer HN: I'll answer your javascript questions

#23

I'm a desktop application developer still finding my feet on the web. Should I bother learning "pure" JavaScript or should I just learn a library like JQuery?

You don't even need to learn javascript for web dev, checkout a project called Coffeescript.

Re: Offer HN: I'll answer your javascript questions

#24
post #22

Ok: What are some good object creation strategies you employ. Basically I know with javascript there are a handful of different ways to create objects. Some (using a framework) give you java-like inheritence, some other methodologies. Much of the time you don't really need to give a shit about inheritence as you are more focused on containership (the word eludes me, probably will remember it 15 minutes after post). S…

encapsulation?

composition. Told you 15 minutes later.

Re: Offer HN: I'll answer your javascript questions

#25

I'm a desktop application developer still finding my feet on the web. Should I bother learning "pure" JavaScript or should I just learn a library like JQuery?

I feel in all cases of this questions (say RoR vs Ruby) the answer must ALWAYS be "learn the language first, then learn the framework". If you don't you will do the following:

a) Write bad code (you barely know the syntax)

b) Get really confused, and thus either start hating it, or simply not able to take full advantage of the framework.

c) Do roundabout ways to solve a problem when there is a very simple solution, but you not understanding the language still made the mistake.

d) Hit a big brick wall when looking at code others made.

I made this sort of mistake before. It is a bad mistake to make.

However to note: jQuery makes dom manipulation fairly easy. It is in no way a replacement to javascript, only the browser's dom lib calls. You just wind up not writing a bunch of utility functions to handle the different ways to get at the same information in the browser. Plus I like the syntax.

Re: Offer HN: I'll answer your javascript questions

#26

Have you tried CoffeeScript and what are your thoughts on it? It's clearly not 'done' yet but I found it really helps clean up my scripts, reduce the amount of typing and avoid a lot of tricky JavaScript mistakes.

Coffeescript is great in theory. The problem is that when debugging stuff in the browser, you're going to be looking at Javascript in Chrome Developer Tools or Firebug. You can hopefully do an inverse mapping from generated JS to the corresponding line in Coffeescript...but that's kind of a pain, especially because it's not a line for line conversion.

Short of patching Chrome Dev Tools to support stepping through Coffeescript itself, not sure how to fix that.

Re: Offer HN: I'll answer your javascript questions

#27
post #9

Earlier quoted context omitted.

If you're a desktop guy, and MS based, learn some silverlight, it's very nice and also a MS-win7 phone. If you're not, Javascript + JQuery is the way to learn for the client side stuff, you also need a server side language (python, ruby, etc).

If possible can you throw some light on which would be good for starters on server side language Python (which I am currently learning) from http://learnpythonthehardway.org/static/LearnPythonTheHardWa... OR PHP (which I already know)

If you only want to learn one language you can just do server-side JS. That said, it's hard to go wrong with Python.

Re: Offer HN: I'll answer your javascript questions

#28

Have you tried CoffeeScript and what are your thoughts on it? It's clearly not 'done' yet but I found it really helps clean up my scripts, reduce the amount of typing and avoid a lot of tricky JavaScript mistakes.

Coffeescript is great in theory. The problem is that when debugging stuff in the browser, you're going to be looking at Javascript in Chrome Developer Tools or Firebug. You can hopefully do an inverse mapping from generated JS to the corresponding line in Coffeescript...but that's kind of a pain, especially because it's not a line for line conversion. Short of patching Chrome Dev Tools to support stepping through Cof…

That's a pretty lame excuse. There also isn't a simple line for line relationship between C code and x86 code or C# code and MSIL code. But there's a standard solution to this problem when debugging: let the compilers generate mapping information.

For something like CoffeeScript, you'd probably have a debug compilation mode where each line of generated code is annotated with comments containing line numbers or full lines of code from the original CoffeeScript source code. It wouldn't be as convenient as a true source-level debugger for CoffeeScript, but it should suffice.

Re: Offer HN: I'll answer your javascript questions

#29
post #6

I'm a desktop application developer still finding my feet on the web. Should I bother learning "pure" JavaScript or should I just learn a library like JQuery?

jQuery is just the de facto DOM API; learn them concurrently.

I wouldn't agree with this 100%. I principally use jQuery unless I am working on something that has to very lightweight. However, there are lots of libraries and different reasons people and companies use them. That's why it's important to know the language in detail so that you can switch between libraries as needed.

Re: Offer HN: I'll answer your javascript questions

#30
What's the best JavaScript deployment strategy you've seen?

I.e., are assets always compiled and bundled? How do you split up your bundles? Would you use namespaces, like Closure? How do you handle dependencies? What percent of the code lives on the page in script tags versus external resources? What testing framework(s) do you use? Do you prefer a certain code style?

Post reply on HN