从Python到JavaScript:学习第二门编程语言更容易
最近我开始学习JavaScript,而我以前曾经学习过Python,所以我采用的学习方式是比较两种语言的功能,发现这样学的很快。
比如说,在Python中,我们可以使用len()
函数来获取一个字符串的长度:
a_string = 'hello world'
length = len(a_string)
print(length)
# 11
而在JavaScript中,我们可以使用length
属性来获取一个字符串的长度:
let a_string = 'hello world';
let length = a_string.length;
console.log(length);
// 11
再比如说,在Python中,我们可以使用range()
函数来创建一个序列:
a_list = list(range(5))
print(a_list)
# [0, 1, 2, 3, 4]
而在JavaScript中,我们可以使用Array.from()
方法来创建一个数组:
let a_list = Array.from(Array(5).keys());
console.log(a_list);
// [0, 1, 2, 3, 4]
以上就是两种语言功能函数的比较,我觉得学习第二门编程语言更容易,因为我们可以利用自己已经学习的知识来对比,这样才能更快地掌握新的知识。