Both these languages seem extremely similar to me. Although Python supports actual classes instead of being prototype-based, in Python classes are not all that different from f
In python, "self" is explicitly passed to a member function, and is not a special keyword or anything. In javascript, "this" is dynamically scoped. you can fiddle with the scope of a member function by calling apply() on it.
Being a JavaScript developer and done some Python stuff (thanks to Google App Engine) I would say that the two major differences between JavaScript and Python would be
Formatting. JavaScript doesn't care about the looks of your code (think of all the code minimizers and what the resulting looks like)
Unicode support. JavaScript is all the way unicode, GAE's Python 2.5 not so much (having Latin 1 as the default character set). So having the need to support non-latin characters can be a real PITA if your'e not sure what you are doing.
Typing: Javascript and Python are both dynamically typed, whereas javascript is weakly, python strongly typed.
var
keyword in Python, implicit globals in ECMAScript, both are lexically scopedundefined
in Python, exceptions are thrownswitch
statement in Python but instead you're encouraged to use a dictionary in that manner, sometimes its convenient assigning properties to lambdas and executing themyield
statement, nor let
expressions/statements, nor array comprehension
s - however these are included in Mozilla's JS which is non-standardraise
vs throw
, except
vs catch
( Python, JS )and
, is
, and not
are used in Pythoni++
Object.prototype
new
operator in Python to create objectsI stole a good bit of info from http://hg.toolness.com/python-for-js-programmers/raw-file/tip/PythonForJsProgrammers.html
In Python, whitespace is part of the language. In Javascript, braces define code blocks and spaces are ignored. Furthermore, Python has bindings for the Java API, .net, and other cool fancy libraries. Javascript is pretty limited in the library department when compared to Python, but it has some neat windowing libraries and such.
I'll add a few I haven't seen mentioned yet:
JavaScript objects/arrays support that cool feature where you don't need to quote (single-word) strings when creating new objects:
var foo = { bar: "baz" };
Accessing associative array keys in JavaScript can be done using dot notation, in addition to brace notation. That is, these are the same:
foo.bar; //returns "baz"
foo["bar"]; // returns "baz"
Python's anonymous function (lambda
) syntax is not as flexible as JavaScript's anonymous functions.