今天出去面试了,星期六,嘉定,金沙江西路。
面试的题目大概4个:
1.实现一个基于数组底层的循环队列?
2.node中require时模块会加载多次吗?
3.Dom中如果一个元素绑定多个click方法,点击后会怎么样?
4.jq中那种选择器效率最高?
答得不是很好,感觉自己还是略有些紧张,不过面试官很和善也很有耐心,虽然被录取希望不大,但是还是很开心能有这次面试。
下面是答案:
1.
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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
| function CircleQueue(size){ this.initQueue(size); } CircleQueue.prototype = { initQueue : function(size){ this.size = size; this.list = new Array(); this.capacity = size + 1; this.head = 0; this.tail = 0; }, enterQueue : function(ele){ if(typeof ele == "undefined" || ele == ""){ return; } var pos = (this.tail + 1) % this.capacity; if(pos == this.head){ return; }else{ this.list[this.tail] = ele; this.tail = pos; } }, delQueue : function(){ if(this.tail == this.head){ return; }else{ var ele = this.list[this.head]; this.head = (this.head + 1) % this.capacity; return ele; } }, find : function(ele){ var pos = this.head; while(pos != this.tail){ if(this.list[pos] == ele){ return pos; }else{ pos = (pos + 1) % this.capacity; } } return -1; }, queueSize : function(){ return (this.tail - this.head + this.capacity) % this.capacity; }, clearQueue : function(){ this.head = 0; this.tail = 0; }, isEmpty : function(){ if(this.head == this.tail){ return true; }else{ return false; } } };
|
2.
不会,module中又module.load机制,会对已经加载的module进行缓存。
3.
方法都会执行,因为js是异步的,但是执行的顺序并不能保证。
4.
ID > Tag > Class
ID 选择器是速度最快的,这主要是因为它使用 JavaScript 的内置函数 getElementById();
其次是类型选择器,因为它使用 JavaScript 的内置函数 getElementsByTag();
速度最慢的是 Class 选择器,其需要通过解析 HTML
文档树,并且需要在浏览器内核外递归,这种递归遍历是无法被优化的。
选择器性能优化建议:
尽量使用 ID 选择器
少直接使用 Class 选择器,尽量结合 Tag 使用,如 input.myclass
多用父子关系,少用嵌套关系
缓存 jQuery 对象
链式调用
还有,像$(“div .f1”).eq(0) 和$(“div .f1”).first() 效率差不多,有时会差几毫秒。 $(“div .f1:eq(0)”) 这个最慢了。