Thinking in Javascript

Started by
6 comments, last by Tyranus 9 years, 8 months ago

Hello, i've been reading the forums for a few months but never started a topic or participated in any discussions till now.

I've been using Python for 2-3 years mainly focusing on Web Development with Django/Flask and decided it was time to learn javascript, but not just getting some random snippet to work on my website which is as painful as it gets right now. I want to know what's really going on.

Watching a talk by Douglas Crockford he said something like "To fully understand the beauty of JS you need to think in JS, many people code Python/C/Java in JS and that doesn't cut it" (i think it was along those lines, if i find that video again i'll post the quote)

I'm reading the book Eloquent Javascript (http://eloquentjavascript.net/contents.html) but i don't think that'll be enough.

Does anyone have any links or book recommendations?

Thanks!

Advertisement

I tend to learn programming easier by thinking up something I want to achieve then attempting to code it (and asking questions about 'How to do...' along the way) rather than reading books and doing exercises. Thats how I learnt/am learning Javascript. But this may not be how you learn.

But if you do, I'd try to do something like 'Move a div from the left of the screen to the right' or 'Get some XML from a server'.

Javascript is really powerful because there isn't quite anything called a class, yet you can still achieve sub-classing, polymorphism and abstraction.

An example is:


var someVariable= {"key": "value"}; // this a map

someVariable["Anonymous Function"] = new function(someParams) {
    consolelog(someParams);
};

// call anonymous function
someVariable["Anonymous Function"]("blah");

someVariable = [1, 2, 3]; // this is now an array

// I think you can do this also but may need to check
someVariable = {someProperty: 1.1};
console.log(someVariable.someProperty);  // almost treat the variable as a class with properties.


The problem with Javascript is that it is a hackers language. If you look up several Javascript projects on Github and compare them some of hem will look like they have been written in completly different languages. There is of course new Javascript that makes excesive use of JQuery then there is JSRequire, asm.js and at least a hundred different extensions for simplifying the use of classes. Then there is also the fact that some people use Javascript as a purely functional language (There is a section in elequant Javascript) and some people just stick to classes and objects.

gretty is probably right. Just do a few projects in Javascript. The key to Javascript is that its a "getting things done" language and not a "making code look pretty" language.

I think you'll be fine with the book you're reading and maybe a handy reference. JS is pretty straightforward once you understand the idea behind HTML, CSS, and DOM in general.

"To fully understand the beauty of LEGOs you need to think in LEGOs, many people build with art/science/efficiency in LEGOs and that doesn't cut it"

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

I'm a longtime-lurker, and haven't posted before, but sometime has to be the first!
When I learned javascript I also started with a Python-background. I think my biggest problem was that I was trying to code javascript in the same fashion that I coded Python.

I would recommend to just start coding as much as possible, then switch to some common framework(angular,backbone) and get a feel for how common javascript is written. Looking at the sourcecode for a couple of larger open-source projects might also be very helpful to get a feel for js-syntax.

I recently wrote a blogpost about learning javascript which might or might not be helpful,
http://pointnull.com/learn-javascript/

Oh and if you're a Python programer, you might want to look into Coffeescript, http://coffeescript.org/, the syntax is awesome and quite similar to Python.


The main problem of many programmers is, in my opinion, the desire to do everything just like they did it in other languages. Best example: object orientation.
In JavaScript, there are no classes. The only JavaScript thing which is close to be what a class in other languages is, are the constructors. (And constructors are regular functions which are used for object instanciation elsewhere.)

While working with JavaScript, keep in Mind: there are no private members. Every member is public. (Same goes for Python: "private" members will only be translated, e. g. from "__privateMember" to "_MyClass_privateMember". Knowing this, you can still access private members, but it's evil...)
In general: instead of relying on specific types you should just use objects as if they are what you expect them to be. For at least some type safety or "method overloading" you could do some type checking, but for objects of types other than the primitive types you should just check if they provide the features (methods and/or members) you need.
This also means: you have to use inheritance much less compared to other languages.

You should definitely take a look at what closures are and what you can do using them in JavaScript. Those are used frequntly to hide implementation specific functions to keep the global context clean. (-> Anonymous function containing all library setup calls, which is called immidately.)
Also Closures are used to have some kind of private "members", but due to what a closure is, those are no members ob the object. You will run into some troubles if you want to combine it with inheritance. ;)


@gretty:
var someVariable = {"key":"value"}
This is not a map, it's an object. (You can assign values to an array usign a non numeric key as well, but it still would be an array.)

I agree with Buster2000 (it's a hacker language) and I've even written book about JavaScript that I won't mention here because that could turn this post into spam.

One good thing about JavaScript is that it becomes what you intend it to be. Depending upon which libraries you choose to use.

That's why if you want to create business apps which display forms to users and save data in a database you should use AngularJS.

However, if you want to build games, then you want to know everything about HTML Canvas -- which is manipulated via JavaScript.

Since this is gamedev.net, I will assume that is what you want.

If that is what you want, then you definitely want the following book (not written by me, so this isn't spam):

HTML 5 Canvas (amazon link) by Steve Fulton & Jeff Fulton

This book is very readable and like having a mentor which guides you through the great sample code which builds with each chapter.

Keep in mind -- in case you're wondering why the book title doesn't mention JavaScript -- that HTML5 = HTML, JavaScript & CSS

Also keep in mind when you manipulate the HTML Canvas element you use JavaScript.

The book even works you through creating a couple of games.

Keep on learning, keep on programming.

~Newton

Thanks a lot for the responses, i've finished reading the book and to my surprise i wasn't that far off from the 'correct' way to use javascript.

I tend to learn programming easier by thinking up something I want to achieve then attempting to code it (and asking questions about 'How to do...' along the way) rather than reading books and doing exercises. Thats how I learnt/am learning Javascript. But this may not be how you learn.



But if you do, I'd try to do something like 'Move a div from the left of the screen to the right' or 'Get some XML from a server'.

Indeed, i've used Javascript this way for a while now most of the time using jQuery.

The problem with Javascript is that it is a hackers language. If you look up several Javascript projects on Github and compare them some of hem will look like they have been written in completly different languages.

gretty is probably right. Just do a few projects in Javascript. The key to Javascript is that its a "getting things done" language and not a "making code look pretty" language.

Yeah, i think the most awfully written code i've seen was in JS (it worked like a charm but still, couldn't follow most of what was coded)

I think you'll be fine with the book you're reading and maybe a handy reference. JS is pretty straightforward once you understand the idea behind HTML, CSS, and DOM in general.



"To fully understand the beauty of LEGOs you need to think in LEGOs, many people build with art/science/efficiency in LEGOs and that doesn't cut it"

Thanks for the link Khatharr, and for the LEGO quote laugh.png

I would recommend to just start coding as much as possible, then switch to some common framework(angular,backbone) and get a feel for how common javascript is written. Looking at the sourcecode for a couple of larger open-source projects might also be very helpful to get a feel for js-syntax.

I recently wrote a blogpost about learning javascript which might or might not be helpful,
http://pointnull.com/learn-javascript/

I've started looking into AngularJS and Nodejs a few days ago, that should keep me busy for a while, thanks for the blog link by the way!

The main problem of many programmers is, in my opinion, the desire to do everything just like they did it in other languages. Best example: object orientation.
In JavaScript, there are no classes. The only JavaScript thing which is close to be what a class in other languages is, are the constructors. (And constructors are regular functions which are used for object instanciation elsewhere.)

While working with JavaScript, keep in Mind: there are no private members. Every member is public. (Same goes for Python: "private" members will only be translated, e. g. from "__privateMember" to "_MyClass_privateMember". Knowing this, you can still access private members, but it's evil...)
In general: instead of relying on specific types you should just use objects as if they are what you expect them to be. For at least some type safety or "method overloading" you could do some type checking, but for objects of types other than the primitive types you should just check if they provide the features (methods and/or members) you need.
This also means: you have to use inheritance much less compared to other languages.

You should definitely take a look at what closures are and what you can do using them in JavaScript. Those are used frequntly to hide implementation specific functions to keep the global context clean. (-> Anonymous function containing all library setup calls, which is called immidately.)
Also Closures are used to have some kind of private "members", but due to what a closure is, those are no members ob the object. You will run into some troubles if you want to combine it with inheritance. ;)

Excellent, great advice. I've read and fiddled with closures in Python before but never applied them in any of my projects. With what you tell me it seems they have a niche in JS, will definitely look into it some more.

However, if you want to build games, then you want to know everything about HTML Canvas -- which is manipulated via JavaScript.

Since this is gamedev.net, I will assume that is what you want.

If that is what you want, then you definitely want the following book (not written by me, so this isn't spam):

HTML 5 Canvas (amazon link) by Steve Fulton & Jeff Fulton

That would be one of my goals, correct. Thanks for the heads up and the book recommendation, if you don't mind send me your books name via PM so i can check it out.

I'll check out some game dev resources on HTML5/JS and Python and probably will post on the Game Programming forum next time.

Thanks again!

This topic is closed to new replies.

Advertisement