Kin'blog

喜欢coding。喜欢大海。


  • Home

  • Archives

  • Brief

  • Tags

  • About

《JavaScipt 语言精粹》养分

Posted on Sep 13 2014 |

《JavaScipt 语言精粹》 作者: Douglas Crockford

拿到这本书的时候就读错了名字。悲剧啊。内容写的挺详细的,就是书中用得铁路图实在看着有点费劲。

养分

1. beget创建一个使用原对象作为其原型的新的对象。

1
2
3
4
5
6
7
if (typeof Object.beget !== 'function') {
Object.beget = function(obj) {
var F = function() {};
F.prototype = obj;
return new F();
}
}

Read more »

使用 mongoose 清除数据

Posted on Aug 28 2014 |

使用 mongoose 封装的方法来删除 mongodb 数据库中的 database、 collections、docs。

  • 删除数据库

1
2
3
4
5
6
7
8
9
10
var mongoose = require('mongoose');

var dbUri = "mongodb://localhost/test";
mongoose.connect(dbUri, function() {
var db = mongoose.connection.db;
db.dropDatabase(function(err) {
if (err) return cb(err);
mongoose.disconnect();
});
});
Read more »

“随心所欲”指定 this 的绑定对象

Posted on Aug 16 2014 |

有的时候,函数的调用者(this关键字的绑定对象)并不是你期望的对象。面对这样情况,我们希望可以自定义this的绑定对象。在 javascript 中可以是用call apply bind方法来指定 this 关键字的绑定对象。  

a. call 方法

先看个例子:

eg-1:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
var me = {
fullName: 'aikin'
};
var you = {
fullName: 'you'
};
 
function speakFullName() {
console.log(this.fullName);
}
 
speakFullName(); //=> undefined
speakFullName.call(me); //=> aikin
speakFullName.call(you); //=> you

Read more »
1…111213…15
aikin

aikin

45 posts
60 tags
RSS
GitHub Segmentfault
© 2019 aikin
Powered by Hexo
Theme - NexT.Mist