This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 检测对象类型 | |
* @param: obj {JavaScript Object} | |
* @param: typeStr {String} 以大写开头的 JS 类型名 | |
* @return: {Boolean} | |
*/ | |
function isType(obj, typeStr) { | |
return Object.prototype.toString.call(obj).slice(8, -1) === typeStr; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
print('Converting') | |
try: | |
print(int('x')) | |
except: | |
print('Converting failed!') | |
else: | |
print('Converting successful!') | |
finally: | |
print('done') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# The bad way | |
f = open('pimpin-aint-easy.txt') | |
text = f.read() | |
for line in text.split('\n'): | |
print(line) | |
f.close() | |
# The good way | |
with open('pimpin-aint-easy.txt') as f: | |
for line in f: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
needle = 'd' | |
haystack = ['a', 'b', 'c'] | |
# The bad way | |
found = False | |
for letter in haystack: | |
if needle == letter: | |
print('Found!') | |
found = True | |
break |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ages = { | |
'Mary': 31, | |
'Jonathan': 28 | |
} | |
# The bad way | |
if 'Dick' in ages: | |
age = ages['Dick'] | |
else: | |
age = 'Unknown' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# The bad way | |
x = 10 | |
y = -10 | |
print('Before: x = %d, y = %d' % (x, y)) | |
tmp = y | |
y = x | |
x = tmp | |
print('After: x = %d, y = %d' % (x, y)) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
x_list = [1, 2, 3] | |
y_list = [2, 4, 6] | |
# The bad way | |
for i in range(len(x_list)): | |
x = x_list[i] | |
y = y_list[i] | |
print(x, y) | |
# The good way |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cities = ['Marseille', 'Amsterdam', 'New York', 'London'] | |
# The bad way | |
i = 0 | |
for city in cities: | |
print(i, city) | |
i += 1 | |
# The good way |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function smoothScroll(el, to, duration) { | |
if (duration < 0) { | |
return; | |
} | |
var difference = to - $(window).scrollTop(); | |
var perTick = difference / duration * 10; | |
this.scrollToTimerCache = setTimeout(function() { | |
if (!isNaN(parseInt(perTick, 10))) { | |
window.scrollTo(0, $(window).scrollTop() + perTick); | |
smoothScroll(el, to, duration - 10); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getEntry(globPath) { | |
var files = glob.sync(globPath); | |
var entries = {}, | |
entry, dirname, basename, pathname, extname; | |
for (var i = 0; i < files.length; i++) { | |
entry = files[i]; | |
dirname = path.dirname(entry); | |
extname = path.extname(entry); | |
basename = path.basename(entry, extname); |
NewerOlder