js笔试面试题(随意)

  • 1 网站的资源的文件优化
 尽可能减少http请求次数,将css, js, 图片各自合并
 文件开启GZip压缩
 浏览器缓存
 开启长连接(Keep-Alive)
 避免重定向
 指定字符集
 样式表css置顶,脚本js置底
 使用 CDN 托管
 使用AJAX缓存,让网站内容分批加载,局部更新
  • 2 响应式布局和自适应布局的区别
        自适应布局会在网页的头部,加一段代码: <meta name="viewport" content="width=device-width, initial-scale=1" />
    viewport是网页默认的宽度和高度,上面这行代码的意思是,网页宽度默认等于屏幕宽度(width=device-width),原始缩放比例(initial-scale=1)为1.0,即网页初始大小占屏幕面积的100%。
        自适应是为了解决如何才能在不同大小的设备上呈现同样的网页
        响应式的概念应该覆盖了自适应,而且涵盖的内容更多。
        响应式可以自动识别屏幕宽度、并做出相应调整的网页设计,布局和展示的内容可能会有所变动
  •  3 box-sizing: border-box;的作用
告诉浏览器去理解你设置的边框和内边距的值是包含在width内的
  •  4 .call 和 .apply的区别
 foo.call(this, arg1,arg2,arg3) == foo.apply(this, arguments)==this.foo(arg1, arg2, arg3)
 call, apply都属于Function.prototype的一个方法,它们的作用一样,只是使用方式不同.
  •  5 null, undefined和undeclared 的区别
 https://www.jianshu.com/p/3adf77175dc9
  •  6 Javascript内置对象、原生对象、宿主对象关系
 原生(本地)对象 (需要 new  例如:Object、Function、Array、RegExp)
 内置对象(不要NEW  直接引用——只有MATH  GLOBAL 例如isNaN()、parseInt() 这些都是GLOBAL对象的方法)
 宿主对象(BOM  DOM  &  自定义对象) 其实说白了ECMAScript官方未定义的对象都属于宿主对象,因为其未定义的对象大多数是自己通过ECMAScript程序创建的对象 
 https://blog.csdn.net/foamflower/article/details/9165691
  •  7  jQuery中的bind(), live(), on(), delegate()
 https://www.cnblogs.com/moonreplace/archive/2012/10/09/2717136.html
  •  8  快速判断一个数是否是2的幂次方,若是,并判断出来是多少次方!
(number & number - 1) == 0
https://blog.csdn.net/Hackbuteer1/article/details/6681157
  •  9    node 单线程 与异步 与并发
  •  10 node怎么解决js嵌套回调问题
  •  11  node怎么解决node.js中的未捕获异常
  •  12  Array.isArray(Array.prototype)  ====> true
  •  13  7 + '2' ===> '72'     7 - '2' ===> 5
  •  14   js的闭包的概念与用途
    //10 个a标签
    var elems = document.getElementsByTagName('a');
    for (var i = 0; i < elems.length; i++) {
        elems[i].addEventListener('click', function (e) {
            e.preventDefault();
            alert('I am link #' + i);
        }, 'false');
    }
    比较 (自执行 与闭包)
    var elems = document.getElementsByTagName('a');
    for (var i = 0; i < elems.length; i++) {
        elems[i].addEventListener('click', (function (lockedInIndex) {
            return function (e) {
                e.preventDefault();
                alert('I am link #' + lockedInIndex);
            };
        })(i), 'false');
    }
  •  15   什么是Promise 和Callback相比呢
  • 16 下面题的输出
    var length = 10;
          function fn() {
          	alert(this.length)
          } 
          var obj = {
          	length: 5,
          	method: function(fn){
          		fn();
          		arguments[0]()
          	}
          }
          obj.method(fn);

      答案:15  1   

具体解释 

In the first place, as fn is passed as a parameter to the function method, the scope (this) of the function fn is window. var length = 10; is declared at the window level. It also can be accessed as window.length or length or this.length (when this === window.)
method is bound to Object obj, and obj.method is called with parameters fn. Though method is accepting only one parameter, while invoking it has passed two parameters; the first is a function callback and other is just a number.
When fn() is called inside method, which was passed the function as a parameter at the global level, this.length will have access to var length = 10 (declared globally) not length = 5 as defined in Object obj.
Now, we know that we can access any number of arguments in a JavaScript function using the arguments[] array.
Hence arguments[0]() is nothing but calling fn(). Inside fn now, the scope of this function becomes the arguments array, and logging the length of arguments[] will return 1.
Hence the output will be as above.

---------------------------------------------------以上美团-----------------------------------------------

  • 数字类型转为字符串:
     String(),  '+',   toString(),   toFixed(),  toExponential(),  toPrecision()
  • 看下面代码:
     var scope = 'global';
         function log() {
         	console.log(arguments)
         	var args = Array.prototype.join.call(arguments, ', ');
         	console.log(args)
         	console.log(this.scope);
         }
         var dog= {
         	scope: 'dog',
         	yelp: function() {
         		var scope = 'dog.yelp';
         		log('wow');
         	}
         };
         dog.yelp();
         dog.yelp.call(dog);
        log.call(dog, 'created');

解析:

一个个看,Array.prototype.join.call(arguments, ', ') 其实 指定的对象上没有join()方法,所以指定到Array.prototype 原型 ,
使用call为其添加join方法,通俗的讲 就是你没有这个技能,但是我(call)教授这个技能,你就有了。类似于arguments.join(',')
https://www.cnblogs.com/zxjwlh/p/6390323.html
  • .call  .apply .bind

  https://juejin.im/entry/57eb38b45bbb50005d754341

  •  如何判断一个变量是字符串,判断一个对象是数组?

  对象是否是数组:

instanceof [] instanceof Array
constructor [].constructor === Array
Object.prototype.toString.call([]) === '[object Array]'
Array.isArray([])
Array.prototype.isPrototypeOf([]);

变量是否是字符串:

typeof ''===string
''.constructor === String
Object.prototype.toString.call([]) === '[object String]
  • js如何实现面向对象和继承机制:
https://github.com/norfish/blog/wiki/%E6%B7%B1%E5%85%A5%E7%90%86
%E8%A7%A3JavaScrip%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1%E5%92%8C%E5%8E%9F%E5%9E%8B%E7%BB%A7%E6%89%BF
http://www.ruanyifeng.com/blog/2011/06/designing_ideas_of_inheritance_mechanism_in_javascript.html
  • js机制 之 冒泡 捕获 传播 和 委托
https://www.cnblogs.com/bfgis/p/5460191.html
  • js自执行函数
https://www.cnblogs.com/TomXu/archive/2011/12/31/2289423.html
  • js内存泄漏的几种情况
内存泄漏是指不再用到的内存,没有及时释放
意外的全局变量,闭包,未清除dom元素的引用,循环引用,连接:https://blog.csdn.net/meijory/article/details/76839072
  • setTimeout 0
http://www.cnblogs.com/silin6/p/4333999.html
https://segmentfault.com/a/1190000002633108
  • 原型与原型链

                              

猜你喜欢

转载自blog.csdn.net/shentibeitaokong/article/details/80978835
今日推荐