遍历
python中的for循环很方便,但js中等价的写法 不是 for/in,for/in 语句循环遍历对象的属性:如要遍历打印数组的元素js用forEach
python:
>>> a = [1,2,3,4]
>>> for n in a:
print n
javascript:
a = [1,2,3,4]
a.forEach(function(n){
alert(n)
})
闭包
# python 闭包就好像只有一个方法的类
>>> def sandwich_maker(magic_ingredient):
def make(filling):
return magic_ingredient + ' and ' + filling
return make
>>> ham_and = sandwich_maker('ham')
>>> ham_and('cheese')
'ham and cheese'
>>> ham_and('mustard')
'ham and mustard'
>>> turkey_and = sandwich_maker('turkey')
>>> turkey_and('swiss')
'turkey and swiss'
>>> turkey_and('provolone')
'turkey and provolone'
function sandwichMaker(ingredient){
function make(filling){
return ingredient + ' and ' + filling;
}
return make
}
var hamAnd = sandwichMaker('ham')
hamAnd('cheese')
"ham and cheese"
hamAnd('mustard')
"ham and mustard"
var turkeyAnd = sandwichMaker('turkey');
turkeyAnd('swiss')
"turkey and swiss"
turkeyAnd('Provolone')
"turkey and Provolone"