JavaScript Tips – Switching from ActionScript to JavaScript: Tips for Writing Code

coding-stylejavascriptprogramming practices

I am quite comfortable with using actionscript3 and flash. I also have some experience with Java. But recently, I started learning JavaScript and node.js. I ultimately want to make a 3d game in threejs, but for now I simply want to make a chat application.

I want to get into the habit of using JavaScript. Unfortunately, I am finding it very difficult to start using JavaScript. The difficulties I am facing are

  • No suggestions for different methods and properties pop up, like they do in the Eclipse IDE and Flash Builder IDE. Like when you press dot after object, it shows all the methods and properties relating to that object and it becomes really easy to see what the parameters are and needed to be passed and stuff.

  • How to organize code? In ActionScript3, I could simply make different classes in different packages. I could then import and use those classes in one main file. How does it work in JavaScript?

  • Functions. I have seen people use anonymous functions in javascript. But I am compelled to write external functions as I am in the habit of it. What is better in JavaScript workflow? What do you advice?

  • How is code executed within a HTML file? So if I have several <script></script> tags and I declare a variable in one of them, can the tags below it and above it access that variable?

Best Answer

Autocompletion

Javascript is a dynamic language and it's impossible to get anywhere near to what you have in Actionscript or Java. Just accept what your IDE (WebStorm for instance) might suggest for you. This goes for any interpreted language really.

How to organize code?

Have a look at requirejs or node.js's own module require function.

Anonymous functions.

Yes, get use to them. Javascript is very much function-based so you'd just have to accept that part. Here is a great starting point.

Several tags and I declare a variable in one of them, can the tags below it and above it access that variable?

Nope, only tags below it. Try this:

<body>
   <script> console.log(x); </script>
   <script> var x = 1; </script>
</body>

Console prints "x is undefined".

Related Topic