IIssNan's Notes

Quick notes


  • 首页

  • 归档

  • 标签

  • 搜索

Deciding what not to do is as important as deciding what to do.

Steve Jobs

Function bind polyfill

发表于 2014-08-23 | | 阅读次数

A polyfill function for ES5 Function.prototype.bind:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
if (!Function.prototype.bind) {
Function.prototype.bind = function (obj) {
var slice = [].slice;
var args = slice.call(arguments, 1);
var self = this;
var nop = function () {};
var bound = function () {
return self.apply(
this.instanceof nop ? this : (obj || {}),
args.concat(slice.call(arguments))
);
nop.prototype = self.prototype;
bound.prototype = new nop();
return bound;
};
}
// fn.bind(obj}) => obj.bound => obj.bound(args)
// Ctrl.bind(new Ctrl())

Express - app.router

发表于 2014-08-21 | | 阅读次数

In ExpressJS 3.x, you may see a line code:

1
app.use(app.router);

Briefly, app.router is the component that knows what to do when you call app.get() etc. See the Connect documentation for the router middleware to understand what it does.

Why is order important?

If the router middleware was installed before the session middleware, your router handlers would run before the sesssion middleware so the session wouldn’t be setup until after your handler completed; if the route middleware was installed after the errorHandler middleware, errorHandler wouldn’t get a chance to deal with error from your route handler because it would have been run before the router handler got called.

Bacically, you need to install app.router explicitly if you have other middleware that you need to ensure come after it in the stack.

References:

  • How does app.router work?(SO)
  • app.use(app.router) question (Google Groups)

Split in javascript

发表于 2014-08-20 | | 阅读次数

In JavaScript, split behaves a litte different than expected. For example, we have a string needed to be split into pieces:

1
2
var str = 'Steve Jobs, Deciding what not to do is, ' +
'as important as deciding what to do';

Say, we need to split and format the above string into:

1
Steve Jobs: Deciding what not to do is, as important as deciding what to do.

If we use split without limitation:

1
2
var pieces = str.split(',');
console.log(pieces);

Actually, we got:

1
[Steve Jobs, Deciding what not to do is, as important as deciding what to do]`.

So we add another argument:

1
2
var pieces = str.split(',', 1);
console.log(pieces);

Got Only [Steve Jobs]. The result is not we except. The second argument just identify how many elments in the spilt pieces would return.

So we need to do the split ourself:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
function splitEnhanced(str, seperator, maxSplit) {
var pieces = str.split(seperator);
var result = [];
if (!maxSplit) {
return pieces;
}
while (maxSplit > 0) {
result.push(pieces.shift());
maxSplit--;
}
result.push(pieces.join(seperator));
return result;
}
var pieces = splitEnhanced(str, ',', 1);
console.log(pieces.join(':'));

AngularJs Unit Testing

发表于 2014-08-19 | | 阅读次数
1…111213…37
IIssNan

IIssNan

胡编一通,乱写一气

183 日志
6 分类
111 标签
RSS
GitHub Twitter 豆瓣 知乎
© 2011 - 2017 IIssNan
由 Hexo 强力驱动
主题 - NexT.Pisces
0%