从100到10000的方法论 ——《从优秀到卓越》

你是否在迷茫,如何升职加薪,赢取白富美,走上人生巅峰?

先讲个故事:
故事的主角是森林里的狐狸和刺猬,狐狸生来狡猾,行动敏捷迅速,每天都在谋划怎么抓住各种小动物。刺猬笨拙,迟缓,但是心里清楚要去哪里,找什么吃的。一天,狐狸和刺猬在路上偶遇,狐狸暗自高兴,终于又让我碰上这只小刺猬了,这次它肯定跑不了,一个劲的扑上去。刺猬不慌不忙,心里纳闷,这傻狐狸怎么就是不吸取教训呢?然后缩成了团刺球。上次被刺的疼了好几天,狐狸忽然想到上次的经历,心里一阵抽搐,无奈急忙停止动作,悻悻离开。

...more

五月的风

1

《五月的风》是1941年的一首曲子,那个年代是慢节奏的,是从容的。在那5年之后,第一台计算机诞生;再16年后,也就是1972年,第一个「Hello, World!」程序诞生;时间越走越快,越来越多的人加入程序猿大军,有的人「Code the world」,有的人成为了代码的搬运工,有的人和我的五月一个节奏,没日夜的加班。

...more

Commonly Used JavaScript Functions

Dynamic call functions
// function to call
function toCallFn() {
console.log('fn called');
}

const dynamicCallFn = { toCallFn };
dynamicCallFn['toCallFn']();

References:
http://stackoverflow.com/questions/676721/calling-dynamic-function-with-dynamic-parameters-in-javascript
http://stackoverflow.com/questions/34655616/create-an-instance-of-a-class-in-es6-with-a-dynamic-name

Check is float string
if (!isNaN(value) && value.toString().indexOf('.') != -1) {
alert('value is float');
}​
Add event listener with param
var param;
$selector.addEventListener("click", () => fn(param));

Remake: After adding event listeners, event functions will be triggered in sequence.

Map - Reduce
// To calculate total cost
let nums = [{cost: 20}, {cost: 10}]
let totalCost = nums.map(n => n.cost).reduce((sum, cost) => {
return sum + cost
}) // 30

Official documentation of reduce

How to check whether a DOM element has event listener?

Ans: No JavaScript function to do it. While you can add a boolean variable to to DOM element and use a boolean variable to check if it is true before adding another eventListener to it.

Get array min/max
var min = Math.min.apply(null, arr);
var max = Math.max.apply(null, arr);
Add/remove DOM element classname
// add class name
var $el = document.getElementById("div");
$el.className += " className";

// remove class name
$el.className = $el.className.replace(/\bclassName\b/,'');
Reference value variables !!!
a = [{'b': 1, 'c': 2}]
b = a.slice() // [{'b': 1, 'c': 2}]
b = b.map(e => {e.b += 1; return e}) // [{'b': 2, 'c': 2}]
a // [{'b': 1, 'c': 2}]
// a is changed as b is changed!!

// To prevent this
b = b.map(e => {e.b += 1; return Object.assign(null, e)});
Pass variables in object to function

When passing variables using { a, b, c }, in the receiving function, the variable name should be the same. e.g.

// call fn
fn ({a ,b c})

// define fn
function fn ({a, b, c}) { console.log('fn') }

// define fn in this way will give error
function fn ({first, second, third}) {}
For loop using forEach

When using forEach, it will loop over all items and will not stop with break or return.

Use logical operator to assign value

When use AND/ && to assign value, if first is true, return value of the second operand; if first is false, return value of first operand.

// if originalVal is true, value === newVal
// if originalVal is false, value === originalVal
var value = originalVal && newVal;
if (value) {...}

true && "val"; // "val"
NaN && "anything"; // NaN
0 && "anything"; // 0
Get date range of a week
function getDateRangeOfWeek(date) {
let mon = date.getDate() - date.getDay() + 1;
let sun = mon + 6;
return {
monday: new Date(date.setDate(mon)),
sunday: new Date(date.setDate(sun)),
lastMonday: new Date(date.setDate(mon - 7)),
lastSunday: new Date(date.setDate(sun - 7))
}

所见即所缚,设计一个新软件该有怎样的思维方式?

所见即所缚

你们有没有碰到过这么一种情况?在小组进行头脑风暴时,有人讲出一个不错的想法,它就会在脑子里挥之不去,刚想到点别的,还没深入,就又回到了这个想法。知道的越多,思维越受束缚。创新难,难在不容易走出思维的牢房和知识的边界。以前大家都用带键盘的诺基亚手机,习惯了手机有按键,研发一款新手机时,有几家手机厂商能够想到将键盘删除,改成触屏手机?现在大家习惯了触屏手机,在下一代个人通信设备中,会是以什么样的方式进行人机交互?前端开发,也是一个道理。

...more

JavaScript bind() 的用法

本以为学会了独孤九剑,结果握剑姿势都不对。

bind() 怎么用?码的过程中用了无数次,$('button').bind('click', function() {...} );,结果要给室友解释时,竟无从开口,果然,行不行,溜一溜就知道了。赶紧把自己关进小黑屋,再磨磨剑。

...more