Skip to content

Instantly share code, notes, and snippets.

View kerryChen95's full-sized avatar

kerryChen95 kerryChen95

  • AutoX
  • Beijing, China
View GitHub Profile
@kerryChen95
kerryChen95 / dabblet.css
Created June 6, 2012 03:25 — forked from kejun/dabblet.css
用table-cell垂直居中
.item {
margin-bottom: 20px;
padding: 10px;
font-size: 0;
border: 1px solid #efefef;
}
.item .pic ,
.item .content {
display: table-cell;
*display: inline;
@kerryChen95
kerryChen95 / gist:5189492
Last active December 15, 2015 02:48
Distinguish empty element and `undefined` valued one in JavaScript array

Real undefined valued element

Way 1: in operator, and for...in loop

Due to indexes are not even defined for empty element, in operator return false.

var arr = new Array(5);
console.log(1 in arr); // false

arr[1] = undefined;
@kerryChen95
kerryChen95 / gist:5189579
Created March 18, 2013 18:33
`this` in global and local execution context
var foo = 'foo';
console.log(this.foo); // 'foo'

In global execution context, declaring a variable means add a property to this

(function () {
  var bar = 'bar';
 console.log(this.bar); // undefined
@kerryChen95
kerryChen95 / gist:5192785
Last active December 15, 2015 03:09
Vaules of indexes on `arguments` object are shared with values of formal parameters
var objInGlobal = {
  a: 'a',
  b: 'b'
};

(function (baseTypeVar, referTypeVar, notPassedArg) {
  baseTypeVar = 2;
  console.log(baseTypeVar === arguments[0]); // true
 

Array in JavaScript is actually object:

  • automatically maintains indexed values and length property.
  • its constructor is Array which provides some methods to be inherited, e.g. forEach.
  • may use non-sequence memory since it's an object.

Array in C is different, it always uses sequence memory, at this point, it is real array.

So what it means?

@kerryChen95
kerryChen95 / gist:5194672
Last active December 15, 2015 03:29
Difference of two ways to "create" a function.
// example 1
(function () {
console.log(typeof func); // 'function'
function func () {}
console.log(typeof func); // 'function'
})();
// example 2
(function () {
@kerryChen95
kerryChen95 / gist:5211637
Created March 21, 2013 08:53
判断浮点数相等;分号引发的血案。

example 1

var sum = 0.1 + 0.2; // 0.30000000000000004

// way 1:
Number(sum.toFixed(5)) === 0.3; // true

// way 2:
(sum - 0.3) < 1e-5; // true
@kerryChen95
kerryChen95 / gist:5211727
Created March 21, 2013 09:13
["dwdwad","dwdawd","dwdwad","ddd","sss"],找“ddd”这个字符串的索引,怎么样牺牲空间复杂度,换取时间复杂度为O(1)
var i, arr = ["dwdwad","dwdawd","dwdwad","ddd","sss"], target = 'ddd';
var hash = {};
for(i = 0; i < arr.length; ++i) {
hash[arr[i]] = i;
}
console.log(hash[target]);
@kerryChen95
kerryChen95 / gist:5228915
Created March 23, 2013 18:38
Float number accuracy.
// Why?
// too long? How long is too long?
Math.ceil(3.0000000000000001); // 3
Math.ceil(3.000000000000001); // 4
@kerryChen95
kerryChen95 / REST-such-name.md
Created March 24, 2013 12:18
我理解的REST这个名字的字面意思

REST: Representational state transfer 表征状态转移

表示各个HTTP请求之间是无状态的,也就是说单个HTTP请求包含了所作操作需要的所有信息。请求会使资源的状态发生变化(GET除外,但如果产生访问记录也算状态变化的话,那么GET也算)。