var origin = [ {id:1,title:'title1'}, {id:2,title:'abcd'}, {id:3,title:'title2'}, {id:4,title:'efg'} ]; var find = function(data){ this.data = data; this.where = function(obj){ var key = Object.keys(obj), val = obj[key]; this.data = this.data.filter(function(x){ return val.test(x[key]); }) return this; }; this.order = function(key,val){ //不造desc啥意思 return this.data.sort(function(s1,s2){ return s2[key] - s1[key] }); } return { data:this.data, where:this.where, order:this.order } } var result = find(origin).where({'title':/\d$/}).order('id','desc'); //desc 非遞增 console.log(result);
var items = [ {name:'item1'}, {name:'item2'} ]; var str = '<div ali-for="item in items">{{item.name}}<div>'; var ParseDom = function(str){ var reg1 = /\sali-for=\"[a-zA-Z\s]+\"/gi, reg2 = /\{{2}[a-zA-Z\.]+\}{2}/gi, reg3 = /\.[a-zA-Z]+/gi, len = items.length; var temp = str.replace(reg1,''), fill = str.match(reg2)[0], key = str.match(reg2)[0].match(reg3)[0].slice(1), clone_temp ='', str = ''; for(var i=0;i<len;i++){ clone_temp = temp; str+=clone_temp.replace(fill,items[i][key]); } console.log(str); } ParseDom(str);
var aa = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15], sum = 0; sum = a.slice(0,11).reduce(function(pre,current){ return pre + current; }); console.log(sum);
// 方法1: var q = Array(10).join(',').split(',').map(function(item,idx){ return idx; }); console.log(q); // 方法2: Object.keys(String(Array(11)); // 在ES5,若是此方法的參數不是一個對象(原始的),那麼它會形成 TypeError。 // 在ES2015,非對象的參數將被強制轉換爲一個對象。 // 方法3: Array.from({length:10},(v,i)=>i) // 方法4: [...Array(10).keys()] // 方法5: Array(10).fill().map((v,i)=>i)
一些類型轉換的例子:
http://frontenddev.org/link/c...
http://javascript.ruanyifeng....
http://www.cnblogs.com/rubylo...javascript
var test = (function(a){ this.a = a; return function(b){ return this.a + b; } }(function(a,b){ return a; }(1,2))) console.log(test(4)) //5
var a = {n:1}; var b = a; a.x = a = {n:2}; alert(a.x); // --> undefined alert(b.x); // --> {n:2}
具體解釋:戳這裏html
功能是實現了,,不過代碼有待改進^^java