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 functions that generate objects containing values and functions, just as you'd do in JavaScript. On the other hand, JavaScript only supports floating-point numbers and strings as built-in data types.
These seem like fairly shallow differences to me, so these things开发者_开发问答 aside, what are some more important differences between them?
- Classical inheritance in Python, Prototypal inheritance in ECMAScript
- ECMAScript is a braces and semicolons language while Python is white-space and indent/block based
- No
var
keyword in Python, implicit globals in ECMAScript, both are lexically scoped - Closures in Python 2.5 and lower ( re: Alex Martelli's comment ) are somewhat "limited" because the bindings are read-only, you can't access private variables like you could in ECMAScript
- There's no
undefined
in Python, exceptions are thrown - Immutable list arrays in Python ( tuples )
- No
switch
statement in Python but instead you're encouraged to use a dictionary in that manner, sometimes its convenient assigning properties to lambdas and executing them - ECMAScript 3 does not have a
yield
statement, norlet
expressions/statements, norarray comprehension
s - however these are included in Mozilla's JS which is non-standard raise
vsthrow
,except
vscatch
( Python, JS )- Native Unicode strings in ECMAScript
- keyword operators such as
and
,is
, andnot
are used in Python - Python doesn't support counters such as
i++
- Python's for loop is "smart" so you don't need to use a counter for enumerating through lists, nor do you run into prototypal properties inherited from
Object.prototype
- You don't have to use the
new
operator in Python to create objects - Python is duck-typed
I stole a good bit of info from http://hg.toolness.com/python-for-js-programmers/raw-file/tip/PythonForJsProgrammers.html
Typing: Javascript and Python are both dynamically typed, whereas javascript is weakly, python strongly typed.
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.
I'll add a few I haven't seen mentioned yet:
- JavaScript supports object-literal notation. Python doesn't exactly work the same way, but Python dictionaries are similar to JavaScript associative arrays.
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.- Python has, like, a standard library and stuff. (And yes, I know about Rhino et al., but the libraries they give you are not standard. There's no standardized way to read a file in JavaScript... that I know of.)
- You can run JavaScript in a browser. Python... not so much. ;)
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.
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.
精彩评论