在koa1.0中使用mongoose的Promise

在mongoose使用Promise

mongoose的Promise使用起来还是很方便的只要在查询语句后面根成exec()方法即可。

例如

1
db.User.findOne({}).exec()

这时候返回的即是一个Promise对象。

在koa中使用Promise

只需在promise前面加上 yield 即可获取promise reslove时候的结果,如果需要捕获promise reject结果需要把整个语句放入try-catch

例如

generator-promise

generator 函数和 promise 对象的结合使用

当使用generator函数进行“流程管理”或使用koajs时候,往往需要使用异步操作。

angularjs 全局非阻塞消息通知

目的

因为我的一个angularjs fruit-angular 项目要用的关系,而我又不想用bootstrap或者什么消息通知库之类,所以自己写了个。

预览

2016年阿里秋季校招前端题

吐槽

首先这个博文不是内容不是全部的前端题目,而只是我考的其中一道题,每个人的题目也不一样,写这个文章是自我安慰下昨天答错的题,尽管也没什么用了。

不过不得不说,阿里题目出的比京东强多了,但是编程题不能离开浏览器用本地编辑器也是很不爽的,但是我还是切出来用编辑器了。从今年秋季内推面试挂了到昨天笔试不理想,我想我的校招阿里之路也再见了。

题目

题目是大概这样的:
定义个查找方法和排序方法来查找数据,没有规定 .orderBy() 或者 where() 是不是可选的。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

var data = [
{ userId: 8, title: 'title1' },
{ userId: 11, title: 'other' },
{ userId: 15, title: null },
{ userId: 19, title: 'title2' }
];

var find = function(origin){
//your code are here...
}

//查找data中,符合条件的数据,并进行排序
var result = find(data).where({"title": /\d$/}).orderBy('userId', 'desc');
console.log(result);
// 返回值
// [{ userId: 19, title: 'title2'}, { userId: 8, title: 'title1' }];

解答

方法很简单,在 find 函数中定义个构造函数并返回其实例。给这个构造函数添加方法就行。没有用到原型链什么的,当时想多了 :-(

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

var find = function(data) {
function A(data) {
this.$data = [];
var $this=this;
this.where = function(where) {
data.forEach(function(e) {
for (var key in where) {
if (where[key] instanceof RegExp) {
if (where[key].test(e[key])) {
$this.$data.push(e);
}
} else if (where[key] == e[key]) {
$this.$data.push(e);
}
}
});
return this;
};
this.orderBy=function(id,order){
this.$data.sort(function(a,b){
if(order="desc"){
return b[id]-a[id];
}
});
return this.$data;
}
};
return new A(data);
}
var result = find(data).where({ "title": /\d$/ }).orderBy('userId', 'desc');
console.log(result);
//[ { userId: 19, title: 'title2' },
// { userId: 8, title: 'title1' } ]

用javascript生成回型矩阵 蛇形矩阵

回型/蛇形矩阵形式

1
2
3
4

1 2 3
8 9 4
7 6 5

上述这种形式即为蛇形矩阵。

ES6-Map

ES6引入Map的目的

js 的对象本质上就是键值对集合也就是 Hash结构,但是只能用字符串作为键值,而 Map 就可以解决这个问题。

用法

1
2
3
4
5
6
7
8
9
10
11
12

var m= new Map();
var o={
a:1
};

m.set(o,"123");
m.get(o); // "123"

m.has(o); // true
m.delete(o); // true
m.has(o); // false

Set数据结构

Set

基本用法

Set 是ES6 新的数据结构,类似于数组,但每一项都是唯一的没有重复项。

利用deviceorientation事件获取手机姿态

兼容性

兼容性

api

首先在三位空间内正常拿着手机 x 轴方向由左向右, y 轴从下到上, z 轴垂直于手机屏幕向外。

ES6 Generator 函数

简介

Genertor 函数是ES6提供的一种异步编程解决方法,从语法上,首先从语法上可以理解成一个成一个状态机,封装了多个内部状态。

执行 Generertor 函数会返回一个遍历器对象。也就是说, Generator 函数处理是状态机,还是一个遍历器对象生成函数。返回的遍历器对象,一次遍历Generator函数内部每一个状态。

使用fetch来取代Ajax

为什么要使用Fetch

传统ajax or XMLHttpRequest()往往非常复杂的,而且并不好记忆。例如手写一个ajax

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
var xhr = new XMLHttpRequest();
//不考虑浏览器兼容性
xhr.open("get","garychang.cn",true);
xhr.onreadystatechange=function(){
if(xhr.readyState==4){
if(xhr.status>=200&&xhr.status<300||xhr.status==304){
console.log(xhr.responseText);
}
}
}
//或者 onload方法稍稍简单点。
xhr.onerror=function(err){
//error......
}
xhr.send(null);
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×