一般来说,Collection的声明是共用的,所以不在client也不再server中。 Meteor.js内部集成了MongoDB,好像目前也只支持MongoDB,若要结合其他,可以发起HTTP,DDP,RPC等(后续讲到) 创建一个集合,注意:这里的Mongo是全局变量 Post = new Mongo.Collection('post') 在客户端和服务端中,都有这个概念,虽然定义的语句相同,但是实际的意义却是不同。它们通过(Pub/Sub)来对数据进行同步。 其CURD的操作和MongoDB的语法一致,比如:
1 2 3 4
> db.posts.insert({title: "A new post"});
> db.posts.find(); { "_id": ObjectId(".."), "title" : "A new post"};
客户端集合 客户端的集合更加有趣。当你在客户端申明 Posts = new Mongo.Collection(‘posts’); 你实际上是创建了一个本地的,在浏览器缓存中的真实的 Mongo 集合。 当我们说客户端集合被”缓存”是指它保存了你数据的一个子集,而且对这些数据提供了十分快速的访问。
this.unblock() On the server, methods from a given client run one at a time. The N+1th invocation from a client won’t start until the Nth invocation returns. However, you can change this by calling this.unblock. This will allow the N+1th invocation to start running in a new fiber.
cache.setbit(key, uid, 1) //====================// cache.bitop('and' , destKey, key1, key2) //计算交集 let a = cache.bitcount(key1) || 1 let b = cache.bitcount(destKey) // 交集的长度 per = b/a
srem o(n) n is the number of members to be removed
smembers O(N) where N is the set cardinality
scard o(1)
zset
zadd O(log(N)) for each item added, where N is the number of elements in the sorted set.
zrange O(log(N)+M) with N being the number of elements in the sorted set and M the number of elements returned.
zrem O(M*log(N)) with N being the number of elements in the sorted set and M the number of elements to be removed. zrangebyscore O(log(N)+M) with N being the number of elements in the sorted set and M the number of elements being returned. If M is constant (e.g. always asking for the first 10 elements with LIMIT), you can consider it O(log(N)).
What is the result ofthis expression? (or multiple ones) var name = 'World!'; (function () { if (typeof name === 'undefined') { var name = 'Jack'; console.log('Goodbye ' + name); } else { console.log('Hello ' + name); } })();
答案: Goodbye Jack 变量提升,所以在第一句的if时, name 是为undefined,直到var name 才被定义
6
1 2 3 4 5 6 7 8
What is the result ofthis expression? (or multiple ones) var END = Math.pow(2, 53); var START = END - 100; var count = 0; for (var i = START; i <= END; i++) { count++; } console.log(count);
What is the result ofthis expression? (or multiple ones) var ary = [0,1,2]; ary[10] = 10; ary.filter(function(x) { return x === undefined;});
答案: [] filter() 方法使用指定的函数测试所有元素,并创建一个包含所有通过测试的元素的新数组。 即所谓的过滤函数. 执行到第二句的时候 , ary = [0, 1, 2, undefined * 7, 10] Array.prototype.filter is not invoked for the missing elements 另:
1 2 3 4
var ary = [0,1,2, undefined]; ary[10] = 10; ary.filter(function(x) { return x === undefined;}); // --> [undefined]
8
1 2 3 4 5 6
What is the result ofthis expression? (or multiple ones) var two = 0.2 var one = 0.1 var eight = 0.8 var six = 0.6 [two - one == one, eight - six == two]
答案: Do not know! switch uses === internally and new String(x) !== x
10
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
What is the result ofthis expression? (or multiple ones) functionshowCase2(value) { switch(value) { case'A': console.log('Case A'); break; case'B': console.log('Case B'); break; caseundefined: console.log('undefined'); break; default: console.log('Do not know!'); } } showCase2(String('A'));
答案: Case A String(x) does not create an object but does return a string, i.e. typeof String(1) === “string”
11
1 2 3 4 5 6 7 8 9 10 11 12
What is the result ofthis expression? (or multiple ones) functionisOdd(num) { return num % 2 == 1; } functionisEven(num) { return num % 2 == 0; } functionisSane(num) { return isEven(num) || isOdd(num); } var values = [7, 4, '13', -9, Infinity]; values.map(isSane);
答案: [true, true, true, false, false] Infinity % 2 gives NaN, -9 % 2 gives -1 (modulo operator keeps sign so it’s result is only reliable compared to 0)
12
1 2 3 4 5
What is the result ofthis expression? (or multiple ones)
parseInt(3, 8) parseInt(3, 2) parseInt(3, 0)
答案: 3, NaN, 3
13
1 2
'5' + 3 '5' - 3
答案: “53”, 2 javascript 的隐式转换,
14
1 2
What is the result ofthis expression? (or multiple ones) 1 + - + + + - + 1
答案: 2
15
1 2 3 4
What is the result ofthis expression? (or multiple ones) var ary = Array(3); ary[0]=2 ary.map(function(elem) { return'1'; });
答案: [“1”, undefined × 2] 上面提到过,对为初始化的不起作用
16
1 2 3 4 5 6 7 8 9 10
What is the result ofthis expression? (or multiple ones) functionsidEffecting(ary) { ary[0] = ary[2]; } functionbar(a,b,c) { c = 10; sidEffecting(arguments); return a + b + c; } bar(1,1,1)
答案: 21 arguments 对象并不是一个真正的Array。它类似于数组,但没有数组所特有的属性和方法,除了 length. in javascript variables are tied to the arguments object so changing the variables changes arguments and changing arguments changes the local variables even when they are not in the same scope. 在js中,变量和arguments 是绑定在一起的,所以改变了变量就是改变了arguments,改变了arguments 改变了当前的变量 ,及时不是在相同的作用域内.
17
1 2 3 4
What is the result ofthis expression? (or multiple ones) var a = 111111111111111110000, b = 1111; a + b;
同 第六题
18
1 2 3
What is the result ofthis expression? (or multiple ones) var x = [].reverse; x();
答案: VM1357:2 Uncaught TypeError: Array.prototype.reverse called on null or undefined(…) 暂时不知道怎么解释.
19
1
Number.MIN_VALUE > 0
答案: false
在调试工具中为 5e-324, Number.MIN_VALUE 是最小的大于0的数
20
1 2
What is the result ofthis expression? (or multiple ones) [1 < 2 < 3, 3 < 2 < 1]
What is the result ofthis expression? (or multiple ones) // the most classic wtf 2 == [[[2]]]
答案: true 如果我们这样赋值 var a = [[[2]]] 相当于 a[0][0] = [2] a[0][0][0] = 2 == 比较的是值 ,故返回 true
22
1 2 3 4
What is the result ofthis expression? (or multiple ones) 3.toString() 3..toString() 3...toString()
答案: error, “3”, error
摘自stackoverflow: That is because when JavaScript sees a period after an integer, it assumes the value after it will be its decimals. Because of that, simply using 5.toString() will make JavaScript find toString where it expected numbers. 当javascript遇到整数后面带有小数点,为认为后面的是这个数的小数部分,所以3.toString() 当然 当申明一个变量a,赋值为3的时候,a.toString() 是可行的
23
1 2 3 4 5 6
What is the result ofthis expression? (or multiple ones) (function(){ var x = y = 1; })(); console.log(y); console.log(x);
答案: 1, error
这题在考作用域的时候经常考到, var x = y = 1; 相当于 y = 1; var x = y; 所以在这里y 是全局变量,而x只在这个函数里面, 所以log(y) 为 1, x 就是is not defined
24
1 2 3 4 5
What is the result ofthis expression? (or multiple ones) var a = /123/, b = /123/; a == b a === b
What is the result ofthis expression? (or multiple ones) var a = {}, b = Object.prototype; [a.prototype === b, Object.getPrototypeOf(a) === b]
答案: [false, true] Object.getPrototypeOf() 方法返回指定对象的原型(也就是该对象内部属性[[Prototype]]的值)。 a 是一个Object, Object中 除了Functions有prototype属性之外都没有, 所以 a.prototype = undefined; Every Object instead has an internal property accessible via Object.getPrototypeOf
27
1 2 3 4
What is the result ofthis expression? (or multiple ones) functionf() {} var a = f.prototype, b = Object.getPrototypeOf(f); a === b
答案: false f.prototype is the object that will become the parent of any objects created with new f while Object.getPrototypeOf returns the parent in the inheritance hierarchy. 在控制台中,a 是一个Object, b 是一个 function
28
1 2 3 4 5
What is the result ofthis expression? (or multiple ones) functionfoo() { } var oldName = foo.name; foo.name = "bar"; [oldName, foo.name]
答案: [“foo”, “foo”] function.name 返回函数的名称. Writable no Enumerable no Configurable yes Note that in non-standard, pre-ES6 implementations the configurable attribute was false as well. 另: 当函数使用new Function(…) 或者 直接Function(…) 来创建的时候,其name属性为””, 如:
1 2 3 4 5 6 7
var f = function() {}; var object = { someMethod: function() {} };
What is the result ofthis expression? (or multiple ones) "1 2 3".replace(/\d/g, parseInt)
答案: “1 NaN 3” String.prototype.replace invokes the callback function with multiple arguments where the first is the match, then there is one argument for each capturing group, then there is the offset of the matched substring and finally the original string itself. so parseInt will be invoked with arguments [1, 0], [2, 2], [3, 4].
What is the result ofthis expression? (or multiple ones) functioncaptureOne(re, str) { var match = re.exec(str); return match && match[1]; } var numRe = /num=(\d+)/ig, wordRe = /word=(\w+)/i, a1 = captureOne(numRe, "num=1"), a2 = captureOne(wordRe, "word=1"), a3 = captureOne(numRe, "NUM=2"), a4 = captureOne(wordRe, "WORD=2"); [a1 === a2, a3 === a4]
答案: [true, false] Regular expressions in JavaScript if defined using the /g flag will carry a state across matches, even if they are actually used on different strings (the lastIndex property). This means a3 will be null as the regular expression was applied starting from the index of the last matched string, even if it was a different one.
39
1 2 3 4
What is the result ofthis expression? (or multiple ones) var a = newDate("2014-03-19"), b = newDate(2014, 03, 19); [a.getDay() === b.getDay(), a.getMonth() === b.getMonth()]
答案: [fasle, false] 注意b 的构造方法 月份从0开始算, 所以a 返回 Wed Mar 19 2014 08:00:00 GMT+0800 (CST) b 返回 Sat Apr 19 2014 00:00:00 GMT+0800 (CST) 然后getDay 是返回一个星期中的第几天(从0开始)(这有时候会是个坑) 要想返回一个月中的第几天,请使用getDate
40
1 2 3 4 5 6
What is the result ofthis expression? (or multiple ones) if ('http://giftwrapped.com/picture.jpg'.match('.gif')) { 'a gif file' } else { 'not a gif file' }
答案: a gif file 传入的不是一个正则表达式对象,所以会隐式转换成一个正则表达式对象, .gif –> /.gif/ 匹配的字符串为[“/gif”]
41
1 2 3 4 5 6 7 8 9 10
What is the result ofthis expression? (or multiple ones) functionfoo(a) { var a; return a; } functionbar(a) { var a = 'bye'; return a; } [foo('hello'), bar('hello')]
答案:[“hello”, “bye”] Variabled declarations are hoisted, but in this case since the variable exists already in the scope, they are removed altogether. In bar() the variable declaration is removed but the assignment remains, so it has effect.
router.get('/wechat', function *() { // 获取微信的请求,注意是 get var signature = this.query.signature; var echostr = this.query.echostr; var timestamp = this.query.timestamp; var nonce = this.query.nonce;
// 这里的token 要和你表单上面的token一致 var token = 'MMDBB';
// 根文档上面的,我们需要对这三个参数进行字典序排序 var arr = [token, timestamp, nonce]; arr.sort(); var tmpStr = arr.join('');
// 排序完成之后,需要进行sha1加密, 这里我们使用node.js 自带的crypto模块 var sha1 = crypto.createHash('sha1'); sha1.update(tmpStr); var resStr = sha1.digest('hex'); console.log(signature, 'resStr: ', resStr);