http://www.linchaoqun.com/html/cms/content.jsp?id=1509528630774 Python3笔记:Python与ECMAScript部分语法对比
https://frankfang.github.io/es-6-tutorials/ ES 6 新特性列表
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference 官方的javascript参考文档
python:支持面向对象和函数式编程的多范式编程语言。
es6:是一门面向原型的编程语言
最近再看了一遍es6,发现部分语法有点接近python,js再不是当年那个仓促诞生、简陋甚至丑陋的动态弱类型语言了。
hello world:
py:
- print("hello")
- python hello.py #运行python程序
- python -V #查看版本 python 3.6.3 https://www.python.org/downloads/ 去官网下载安装python(升级python版本时,要先卸载旧的,再重新安装新版本)
- pip install bs4 # 安装第三方包
- import bs4 # 代码中导入第三方包
ES6:
- console.log("hello");
- node hello.js // 运行
- node -v //查看版本 v9.3.0 https://nodejs.org/dist/ 下载node-v9.3.0-x64.msi。(升级node版本时,到官网下载新版本,直接安装即可,无须卸载旧版。)
- npm install vue //安装第三方包 npm install vue -g // 全局安装(如果npm受限,就用cnpm。自己搜索)
- var http = require("http"); // 导入第三方包
- import { stat, exists, readFile } from 'fs'; //导入第三方包
简单查看和遍历:
py:
a1 = "helloworld" # 字符串 a1 = [1, 2, 3, 4, 55, 6] # 列表 a1 = ("aa", "bb") # 元组 print("类型:",type(a1)) # 查看类型 print("长度:",len(a1)) # 查看长度 for i in a1: # 遍历 print(i) dict1 = {'name': 'pp', 'age': 20, "gender": "man"} # 字典 for key, val in dict1.items(): # 遍历字典 print(key, "--", val)
es6:
var a1 = "helloworld"; //字符串 var a1 = [1, 2, 3, 4, 55, 6]; //数组 console.log("类型:" +(typeof a1)); // 查看类型 console.log("长度:" + a1.length); // 查看长度 for (let i=0;i<a1.length;i++){ // 遍历 console.log(a1[i]); } let dict1 = new Map([["a","aa1"], ['b', 'bb2'], ['c', 'ccc']]); //字典 dict1.forEach(function (value, key) { // 遍历字典 console.log(key,value); }) for (let [key,value] of dict1) { // 这样遍历也可以 console.log(key,value); }
作用域、赋值:
py: http://www.runoob.com/python3/python3-function.html
函数内是局部变量,函数外是全局变量。 当内部作用域想修改外部作用域的变量时,就要用到global和nonlocal关键字
# global示例 num = 1 def fun1(): global num # 需要使用 global 关键字声明 num = 222 print("函数内部---",num) print(num) fun1() print(num)