标签:
http://mp.weixin.qq.com/s?__biz=MzAxODE2MjM1MA==&mid=2651550987&idx=1&sn=f7a84b59de14d0b99d5e12a265d55fd2&scene=0#wechat_redirect
http://mp.weixin.qq.com/s?__biz=MzAxODE2MjM1MA==&mid=2651550991&idx=1&sn=ed60b424de47cceb2b81e7df40b57efa&scene=0#wechat_redirect
http://javascript-puzzlers.herokuapp.com/
/* 44 javascript puzzlers http://javascript-puzzlers.herokuapp.com/ // 001 var res = [‘1‘,‘2‘,‘3‘].map(parseInt); console.log(res);// 输出结果为:[1,NaN,NaN] // 输出结果为:[parseInt(‘1‘,0),parseInt(‘2‘,1),parseInt(‘3‘,2)] function foo1(){ // 查看传入的参数都是啥; var l = arguments.length; console.log(‘arguments begin:‘); for(var i=0;i<l;i++){ console.log(arguments[i]); } console.log(‘arguments end;‘); } var res2 = [‘1‘,‘2‘,‘3‘].map(foo1); // map方法会给原数组中的每一个元素都按顺序调用一次callback函数; // callback函数会被传入3个参数:数组元素,元素索引,原数组本身; var a = ‘global a‘; function foo2(){ console.log(this.a); } foo2(); // 直接调用会输出 ‘global a‘ var obj = {}; obj.a = ‘object a‘; var res = [1,2,3].map(foo2,obj); // 会输出三个‘object a‘ // map方法传入一个回调函数,并且传入一个回调函数中的this对象; console.log(res); // [undefined, undefined, undefined] // 002 var res = [typeof null, null instanceof Object]; console.log(res); // [‘object‘,false] // js共有6种数据类型 Undefined,String,Number,Boolean,Object,Null // 每种对应的typeof为‘undefined‘,‘string‘,‘number‘,‘boolean‘,‘object‘,‘object‘ // Function 不是数据类型,但是typeof Function 返回 ‘function‘ // JavaScript instanceof运算符代码 // http://www.ibm.com/developerworks/cn/web/1306_jiangjj_jsinstanceof/ function instance_of(L,R){ // L表示左表达式,R表示右表达式 var O = R.prototype; // 取R的显示原型 L = L.__proto__; // 取L的隐式原型 while(true){ if(L === null){ return false; } if(O === L){ // 这里重点:当O严格等于L时,返回true return true; } L = L.__proto__; } } function Aoo(){} function Foo(){} Foo.prototype = new Aoo(); // JavaScript原型继承 var foo = new Foo(); console.log(foo instanceof Foo); // true console.log(foo instanceof Aoo); // true console.log(Object instanceof Object); //true console.log(Function instanceof Function); //true console.log(Number instanceof Number); // false console.log(String instanceof String); // false console.log(Function instanceof Object); // true console.log(Foo instanceof Function); // true console.log(Foo instanceof Foo); // false // 003 //var res = [[3,2,1].reduce(Math.pow),[].reduce(Math.pow)]; // 这一行就会直接报 TypeError,不能对空数组调用reduce方法; //console.log(res); //var res = [3,2,1].reduce(Math.pow); //console.log(res); // 输出9 , Math.pow(3,2),Math.pow(9,1) function foo1(){ var l = arguments.length; console.log(‘arguments begin:‘); for(var i=0;i<l;i++){ console.log(arguments[i]); } console.log(‘arguments end:‘); } //var res = [‘a‘,‘b‘,‘c‘].reduce(foo1); // reduce中的回调函数会获得四个参数: 初始值(或者上一次回调函数的返回值,没有返回值就是undefined),当前值,当前值索引,原数组 // 如果没有初始值,就会把数组第一个值作为初始值; //var res = [‘a‘,‘b‘,‘c‘].reduce(foo1,‘A‘); //var obj = {}; //var res = [1,2,3].reduce(obj); // 如果回调函数不是函数,也会直接抛出TypeError异常 // 004 var val = ‘smtg‘; console.log(‘Value is ‘ + (val===‘smtg‘)?‘Something‘:‘Nothing‘); // + 优先级 大于 ? , 等价于 ‘Value is true‘?‘Something‘:‘Nothing‘ // 005 var name=‘World‘; (function(){ if(typeof name === ‘undefined‘){ var name = ‘Jack‘; console.log(‘Goodbye ‘+name); }else{ console.log(‘Hello ‘+name); } })(); (function(){ var a; if(typeof a === ‘undefined‘){ var name = ‘Jack‘; console.log(‘Goodbye ‘+name); }else{ console.log(‘Hello ‘+name); } })(); (function(){ var a=‘‘; if(typeof a === ‘undefined‘){ var name = ‘Jack‘; console.log(‘Goodbye ‘+name); }else{ console.log(‘Hello ‘+name); // 这个变量的确被提升了;Hoisting } })(); (function(){ var a=‘‘; if(typeof a === ‘undefined‘){ var name = ‘Jack‘; console.log(‘Goodbye ‘+name); }else{ console.log(‘Hello ‘+name1); // 这个就会报异常,name1 is not defined } })(); // 006 var END = Math.pow(2,53); // 9007199254740992 var START = END - 100; var count = 0; //for(var i= START;i<=END;i++){ // 这个就是死循环了; // count++; //} //console.log(count); var end1 = END+1; // end1与END是一样的; console.log(END); console.log(end1); var end2 = Math.pow(2,100); // 已经是指数表示方式了;1.2676506002282294e+30 var end21 = end2+1; console.log(end2); console.log(end21); // 在console中可以表示的最大值是Math.pow(2,1023),8.98846567431158e+307 // Math.pow(2,1024)终于显示Infinity // Number.MAX_VALUE = 1.7976931348623157e+308 // 007 var ary = [0,1,2]; ary[10] = 10; var res = ary.filter(function(x){return x===undefined;}); console.log(res); // 官方Array.filter()的polyfill: if (!Array.prototype.filter) { Array.prototype.filter = function(fun/*, thisArg*[DEL THIS]/) { ‘use strict‘; if (this === void 0 || this === null) { throw new TypeError(); } var t = Object(this); var len = t.length >>> 0; if (typeof fun !== ‘function‘) { throw new TypeError(); } var res = []; var thisArg = arguments.length >= 2 ? arguments[1] : void 0; for (var i = 0; i < len; i++) { if (i in t) { // 首先判断是否在数组内; var val = t[i]; // NOTE: Technically this should Object.defineProperty at // the next index, as push can be affected by // properties on Object.prototype and Array.prototype. // But that method‘s new, and collisions should be // rare, so use the more-compatible alternative. if (fun.call(thisArg, val, i, t)) { res.push(val); } } } return res; }; } */ // 008 var one = 0.1; var two = 0.2; var six = 0.6; var eight = 0.8; var res = [two-one==one, eight-six==two]; console.log(res); // [true,false]
标签:
原文地址:http://www.cnblogs.com/stono/p/5641897.html