最近在看js相关的资料,相起了前端的以前面试中的一个问题;
What’s the result of:
100['toString']['length']
A couple of good pieces of deception in this one. Numbers end up wrapped by the Number type when you use them, and the Number type has a toString() method. However, you’re not actually callingtoString() in this case, you’re actually accessing the length property of the toString() method. It makes more sense to look at the code like this:
100.toString.length [...]
从这段代码上理解
var o = { x: 8, valueOf: function(){ return this.x + 2; }, toString: function(){ return this.x.toString(); } }, result = o < “9″; alert(o);
Questions:
What is the value result? What is the value displayed in the alert?
参考mozilla上的解释,可以去覆盖对象上的valueOf方法。
参考:
http://www.nczonline.net/blog/2010/02/16/my-javascript-quiz/
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/ValueOf
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/toString
//抽牌法优化 function shuffle_pick(m) { var arr = new Array(m); for (var i=0; i>0;) { var rnd = Math.floor(Math.random()*i); arr2.push(arr[rnd]); arr[rnd] = arr[--i]; } return arr2; }
参考:
http://www.cnblogs.com/jkisjk/archive/2012/04/23/javascript_shuffle.html
http://wenku.baidu.com/view/c4fea82658fb770bf78a55b7.html
sort方法传入一个函数,此函数随机返回1或-1,达到排列数组元素。
arr.sort(function(a,b){ return a > b ? -1 : 1;});
考虑时间复杂度和执行效率:
if (!Array.prototype.st) { Array.prototype.st = function() { for(var j, x, i = this.length; i; j = parseInt(Math.random() * i), x = this[--i], this[i] = this[j], this[j] = x); return this; }; }
随机交换n次,其复杂度为O(n),而sort(快排)复杂度为O(nlogn).
写下这个题目主要是在接受另一位同事项目中发现的问题,在switch语句中case比较忽略了值是强类型比较。
var v = 5; switch(v){ case ’5′: console.info(“hi”); //这个一直不会执行,因为数据类型不匹配 }
最后是统一对比较的字符进行显示转换String(v),或者统一的数据类型。也让自己在使用时候注意下JS中的细节,不要掉进这个陷阱中了。
参考:
http://dancewithnet.com/2008/10/27/javascript-gotchas/
http://www.evotech.net/blog/2007/07/javascript-switch-statement-quirks/
Currying is the technique of transforming a function that takes multiple arguments
in such a way that it can be called as a chain of functions each with a single argument.
将多拥有多个参数的函数形式转化为拥有单一参数的函数形式,curring的概念将函数式编程的概念和默认参数以及可变参数结合在一起.一个带n个参数,curried的函数固化第一个参数为固定参数,并返回另一个带n-1个参数的函数对象,分别类似于LISP的原始函数car和cdr的行为。currying能泛化为偏函数应用(partial function application, PFA),p 这种函数将任意数量(顺序)的参数的函数转化为另一个带剩余参数的函数对象。
参考:
http://extralogical.net/articles/currying-javascript.html
http://www.cnblogs.com/rubylouvre/archive/2009/11/09/1598761.html
http://extralogical.net/articles/currying-javascript.html
html5中自定义data属性:
属性名定义:
Attribute Name
The data attribute name must be at least one character long and must be prefixed with ‘data-’. It should not contain any uppercase letters.
属性值定义:
Attribute Value
The attribute value can be any string.
Using this syntax, we can add application data to our markup [...]
在ruby on rails编程时接触过面向方面AOP编程,也就是在执行某项操作前或者以后调用,这操作完全不用关心在它执行前或执行后做了什么。但是不同的业务逻辑情况可能会进行条件判断,如:保存数据前对数据的有效性校验,校验的结果决定了保存操作是否执行,但是执行保存的操作确完全不知道在保存数据前的操作。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。
那么在js中去如何实现这样的功能?前辈已经进行了一定的探索和分析http://www.cnblogs.com/rubylouvre/archive/2009/08/08/1541578.html
http://www.cnblogs.com/riceball/archive/2007/09/02/jsInject.html
在实际的前端开发中也遇到过这样的需求:前端容灾中需要判断异步调用的请求是否成功的次数来决定是否启用的备用的域名。那么在跨域的jsonp异步请求中就需要发送的请求是否成功的执行了指定的回调函数,如果执行回调的次数在小于指定的回调次数后就需要启用备份地址。
具体的实现代码整理后再贴上来。
通过这个面试题目复习下正则表达式
var someText=”web2.0 .net2.0″;
var pattern=/(\w+)(\d)\.(\d)/g;
var outCome_exec=pattern.exec(someText);
var outCome_matc=someText.match(pattern);
What is outCome_exec[1] and outCome_matc[1]?
分别输出什么?
分类目录
- html5/css3 (1)
- javaScript (34)
- stylesheet (26)
- 其他分类 (14)
- 可用性 (11)
- 可访问性 (5)
- 开发理论 (11)
- 手机开发 (29)
- 算法学习 (1)
- 设计相关 (4)
近期评论
- kevinpeng 发表在《JavaScript数据类型》
- nicolazj 发表在《JavaScript数据类型》
- kevinpeng 发表在《IE9浏览器jsonp请求响应规范》
- Doris 发表在《How a web page loads (转)》
- Carrie 发表在《readyState五种状态详解》
