How different are the semantics between Python and JavaScript?

后端 未结 6 1194
無奈伤痛
無奈伤痛 2021-01-30 09:36

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

相关标签:
6条回答
  • 2021-01-30 09:42

    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.

    0 讨论(0)
  • 2021-01-30 09:49

    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.

    0 讨论(0)
  • 2021-01-30 09:50

    Typing: Javascript and Python are both dynamically typed, whereas javascript is weakly, python strongly typed.

    0 讨论(0)
  • 2021-01-30 09:51
    1. Classical inheritance in Python, Prototypal inheritance in ECMAScript
    2. ECMAScript is a braces and semicolons language while Python is white-space and indent/block based
    3. No var keyword in Python, implicit globals in ECMAScript, both are lexically scoped
    4. 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
    5. There's no undefined in Python, exceptions are thrown
    6. Immutable list arrays in Python ( tuples )
    7. 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
    8. ECMAScript 3 does not have a yield statement, nor let expressions/statements, nor array comprehensions - however these are included in Mozilla's JS which is non-standard
    9. raise vs throw, except vs catch ( Python, JS )
    10. Native Unicode strings in ECMAScript
    11. keyword operators such as and, is, and not are used in Python
    12. Python doesn't support counters such as i++
    13. 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
    14. You don't have to use the new operator in Python to create objects
    15. 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

    0 讨论(0)
  • 2021-01-30 09:53

    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.

    0 讨论(0)
  • 2021-01-30 09:58

    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. ;)
    0 讨论(0)
提交回复
热议问题