javaScript面试题

“每18至24个月,前端都会难一倍”

——赫门 “2015深JS大会《前端服务化之路》主题演讲”

知识点

数据类型运算对象function继承闭包作用域原型链事件RegExpJSONAjaxDOMBOM内存泄漏跨域异步加载模板引擎前端MVC前端MVVM路由模块化CanvasjQueryECMAScript 2015(ES6)Node.jsAngularJSReactCommonJSAMDCMD ......

题目&答案
  • 介绍一下 JS 的基本数据类型。
    <code class="mathematica"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">Undefined</span>、<span class="hljs-keyword" style="color: rgb(0, 0, 136);">Null</span>、Boolean、<span class="hljs-keyword" style="color: rgb(0, 0, 136);">Number</span>、<span class="hljs-keyword" style="color: rgb(0, 0, 136);">String</span></code>
  • 介绍一下 JS 有哪些内置对象。
    <code class="javascript"><span class="hljs-built_in" style="color: rgb(102, 0, 102);">Object</span> 是 JavaScript 中所有对象的父对象
    数据封装类对象:<span class="hljs-built_in" style="color: rgb(102, 0, 102);">Object</span>、<span class="hljs-built_in" style="color: rgb(102, 0, 102);">Array</span>、<span class="hljs-built_in" style="color: rgb(102, 0, 102);">Boolean</span>、<span class="hljs-built_in" style="color: rgb(102, 0, 102);">Number</span>、<span class="hljs-built_in" style="color: rgb(102, 0, 102);">String</span>
    其他对象:<span class="hljs-built_in" style="color: rgb(102, 0, 102);">Function</span>、Argument、<span class="hljs-built_in" style="color: rgb(102, 0, 102);">Math</span>、<span class="hljs-built_in" style="color: rgb(102, 0, 102);">Date</span>、<span class="hljs-built_in" style="color: rgb(102, 0, 102);">RegExp</span>、<span class="hljs-built_in" style="color: rgb(102, 0, 102);">Error</span></code>
  • 列举几条 JavaScript 的基本代码规范。
    <code class="bash">(<span class="hljs-number" style="color: rgb(0, 102, 102);">1</span>)不要在同一行声明多个变量
    (<span class="hljs-number" style="color: rgb(0, 102, 102);">2</span>)如果你不知道数组的长度,使用 push
    (<span class="hljs-number" style="color: rgb(0, 102, 102);">3</span>)请使用 ===/!== 来比较 <span class="hljs-literal" style="color: rgb(0, 102, 102);">true</span>/<span class="hljs-literal" style="color: rgb(0, 102, 102);">false</span> 或者数值
    (<span class="hljs-number" style="color: rgb(0, 102, 102);">4</span>)对字符串使用单引号 <span class="hljs-string" style="color: rgb(0, 136, 0);">''</span>(因为大多时候我们的字符串。特别html会出现<span class="hljs-string" style="color: rgb(0, 136, 0);">")
    (5)使用对象字面量替代 new Array 这种形式
    (6)绝对不要在一个非函数块里声明一个函数,把那个函数赋给一个变量。浏览器允许你这么做,但是它们解析不同
    (7)不要使用全局函数
    (8)总是使用 var 来声明变量,如果不这么做将导致产生全局变量,我们要避免污染全局命名空间
    (9)Switch 语句必须带有 default 分支
    (10)使用 /**...*/ 进行多行注释,包括描述,指定类型以及参数值和返回值
    (11)函数不应该有时候有返回值,有时候没有返回值
    (12)语句结束一定要加分号
    (13)for 循环必须使用大括号
    (14)if 语句必须使用大括号
    (15)for-in 循环中的变量应该使用 var 关键字明确限定作用域,从而避免作用域污染
    (16)避免单个字符名,让你的变量名有描述意义
    (17)当命名对象、函数和实例时使用驼峰命名规则
    (18)给对象原型分配方法,而不是用一个新的对象覆盖原型,覆盖原型会使继承出现问题
    (19)当给事件附加数据时,传入一个哈希而不是原始值,这可以让后面的贡献者加入更多数据到事件数据里,而不用找出并更新那个事件的事件处理器</span></code>
  • 介绍一下 JavaScript 原型,原型链,它们有何特点?
    <code class="javascript">每个对象都会在其内部初始化一个属性,就是prototype(原型),当我们访问一个对象的属性时,如果这个对象内部不存在这个属性,那么他就会去prototype里找这个属性,这个prototype又会有自己的prototype,
    于是就这样一直找下去,也就是我们平时所说的原型链的概念。
    关系:instance.constructor.prototype = instance.__proto__
    <span class="hljs-comment" style="color: rgb(136, 0, 0);">//</span>
    特点:JavaScript对象是通过引用来传递的,我们创建的每个新对象实体中并没有一份属于自己的原型副本,当我们修改原型时,与之相关的对象也会继承这一改变。
    <span class="hljs-comment" style="color: rgb(136, 0, 0);">//</span>
    当我们需要一个属性时,JavaScript引擎会先看当前对象中是否有这个属性,如果没有的话,就会查找它的prototype对象是否有这个属性,如此递推下去,一致检索到<span class="hljs-built_in" style="color: rgb(102, 0, 102);">Object</span>内建对象。
    <span class="hljs-function"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">function</span> <span class="hljs-title">Func</span>(<span class="hljs-params" style="color: rgb(102, 0, 102);"></span>)</span>{}
    Func.prototype.name = <span class="hljs-string" style="color: rgb(0, 136, 0);">"Xiaosong"</span>;
    Func.prototype.getInfo = <span class="hljs-function"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">function</span>(<span class="hljs-params" style="color: rgb(102, 0, 102);"></span>) </span>{
      <span class="hljs-keyword" style="color: rgb(0, 0, 136);">return</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">this</span>.name;
    }
    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">var</span> person = <span class="hljs-keyword" style="color: rgb(0, 0, 136);">new</span> Func();
    <span class="hljs-built_in" style="color: rgb(102, 0, 102);">console</span>.log(person.getInfo());
    <span class="hljs-comment" style="color: rgb(136, 0, 0);">//"Xiaosong"</span>
    <span class="hljs-built_in" style="color: rgb(102, 0, 102);">console</span>.log(Func.prototype);
    <span class="hljs-comment" style="color: rgb(136, 0, 0);">//Func { name = "Xiaosong", getInfo = function() }</span></code>
  • JavaScript 有几种类型的值?能否画一下它们的内存图?
    <code class="mathematica">栈:原始数据类型(<span class="hljs-keyword" style="color: rgb(0, 0, 136);">Undefined</span>,<span class="hljs-keyword" style="color: rgb(0, 0, 136);">Null</span>,Boolean,<span class="hljs-keyword" style="color: rgb(0, 0, 136);">Number</span>,<span class="hljs-keyword" style="color: rgb(0, 0, 136);">String</span>)
    堆:引用数据类型(对象、数组、函数)
    两种类型的区别:存储位置不同
    //
    原始数据类型直接存储在栈(stack)中的简单数据段,占据空间小、大小固定,属于被频繁使用数据,所以放入栈中存储;
    引用数据类型存储在堆(heap)中的对象,占据空间大、大小不固定,如果存储在栈中,将会影响程序运行的性能;引用数据类型在栈中存储了指针,该指针指向堆中该实体的起始地址。当解释器寻找引用值时,会首先检索其在栈中的地址,取得地址后从堆中获得实体。</code>
  • JavaScript 如何实现继承?
    <code class="javascript">(<span class="hljs-number" style="color: rgb(0, 102, 102);">1</span>)构造继承
    (<span class="hljs-number" style="color: rgb(0, 102, 102);">2</span>)原型继承
    (<span class="hljs-number" style="color: rgb(0, 102, 102);">3</span>)实例继承
    (<span class="hljs-number" style="color: rgb(0, 102, 102);">4</span>)拷贝继承
    <span class="hljs-comment" style="color: rgb(136, 0, 0);">//</span>
    原型prototype机制或apply和call方法去实现较简单,建议使用构造函数与原型混合方式。
    <span class="hljs-function"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">function</span> <span class="hljs-title">Parent</span>(<span class="hljs-params" style="color: rgb(102, 0, 102);"></span>) </span>{
      <span class="hljs-keyword" style="color: rgb(0, 0, 136);">this</span>.name = <span class="hljs-string" style="color: rgb(0, 136, 0);">'song'</span>;
    }
    <span class="hljs-function"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">function</span> <span class="hljs-title">Child</span>(<span class="hljs-params" style="color: rgb(102, 0, 102);"></span>) </span>{
      <span class="hljs-keyword" style="color: rgb(0, 0, 136);">this</span>.age = <span class="hljs-number" style="color: rgb(0, 102, 102);">28</span>;
    }
    Child.prototype = <span class="hljs-keyword" style="color: rgb(0, 0, 136);">new</span> Parent(); <span class="hljs-comment" style="color: rgb(136, 0, 0);">//通过原型,继承了Parent</span>
    <span class="hljs-comment" style="color: rgb(136, 0, 0);">//</span>
    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">var</span> demo = <span class="hljs-keyword" style="color: rgb(0, 0, 136);">new</span> Child()l;
    alert(demo.age);
    alert(demo.name); <span class="hljs-comment" style="color: rgb(136, 0, 0);">//得到被继承的属性</span></code>
  • JavaScript 有哪几种创建对象的方式?
    <code class="javascript">javascript创建对象简单的说,无非就是使用内置对象或各种自定义对象,当然还可以用<span class="hljs-built_in" style="color: rgb(102, 0, 102);">JSON</span>;但写法有很多种,也能混合使用。
    <span class="hljs-comment" style="color: rgb(136, 0, 0);">//</span>
    (<span class="hljs-number" style="color: rgb(0, 102, 102);">1</span>)对象字面量的方式
    person={firstname:<span class="hljs-string" style="color: rgb(0, 136, 0);">"Mark"</span>,lastname:<span class="hljs-string" style="color: rgb(0, 136, 0);">"Yun"</span>,age:<span class="hljs-number" style="color: rgb(0, 102, 102);">25</span>,eyecolor:<span class="hljs-string" style="color: rgb(0, 136, 0);">"black"</span>};
    (<span class="hljs-number" style="color: rgb(0, 102, 102);">2</span>)用<span class="hljs-function"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">function</span>来模拟无参的构造函数
    <span class="hljs-title">function</span> <span class="hljs-title">Person</span>(<span class="hljs-params" style="color: rgb(102, 0, 102);"></span>)</span>{}
    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">var</span> person = <span class="hljs-keyword" style="color: rgb(0, 0, 136);">new</span> Person(); <span class="hljs-comment" style="color: rgb(136, 0, 0);">//定义一个function,如果使用new"实例化",该function可以看作是一个Class</span>
    person.name = <span class="hljs-string" style="color: rgb(0, 136, 0);">"Xiaosong"</span>;
    person.age = <span class="hljs-string" style="color: rgb(0, 136, 0);">"23"</span>;
    person.work = <span class="hljs-function"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">function</span>(<span class="hljs-params" style="color: rgb(102, 0, 102);"></span>) </span>{
      alert(<span class="hljs-string" style="color: rgb(0, 136, 0);">"Hello "</span> + person.name);
    }
    person.work();
    (<span class="hljs-number" style="color: rgb(0, 102, 102);">3</span>)用<span class="hljs-function"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">function</span>来模拟参构造函数来实现(用<span class="hljs-title">this</span>关键字定义构造的上下文属性)
    <span class="hljs-title">function</span> <span class="hljs-title">Person</span>(<span class="hljs-params" style="color: rgb(102, 0, 102);">name,age,hobby</span>) </span>{
      <span class="hljs-keyword" style="color: rgb(0, 0, 136);">this</span>.name = name; <span class="hljs-comment" style="color: rgb(136, 0, 0);">//this作用域:当前对象</span>
      <span class="hljs-keyword" style="color: rgb(0, 0, 136);">this</span>.age = age;
      <span class="hljs-keyword" style="color: rgb(0, 0, 136);">this</span>.work = work;
      <span class="hljs-keyword" style="color: rgb(0, 0, 136);">this</span>.info = <span class="hljs-function"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">function</span>(<span class="hljs-params" style="color: rgb(102, 0, 102);"></span>) </span>{
          alert(<span class="hljs-string" style="color: rgb(0, 136, 0);">"我叫"</span> + <span class="hljs-keyword" style="color: rgb(0, 0, 136);">this</span>.name + <span class="hljs-string" style="color: rgb(0, 136, 0);">",今年"</span> + <span class="hljs-keyword" style="color: rgb(0, 0, 136);">this</span>.age + <span class="hljs-string" style="color: rgb(0, 136, 0);">"岁,是个"</span> + <span class="hljs-keyword" style="color: rgb(0, 0, 136);">this</span>.work);
      }
    }
    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">var</span> Xiaosong = <span class="hljs-keyword" style="color: rgb(0, 0, 136);">new</span> Person(<span class="hljs-string" style="color: rgb(0, 136, 0);">"WooKong"</span>,<span class="hljs-number" style="color: rgb(0, 102, 102);">23</span>,<span class="hljs-string" style="color: rgb(0, 136, 0);">"程序猿"</span>); <span class="hljs-comment" style="color: rgb(136, 0, 0);">//实例化、创建对象</span>
    Xiaosong.info(); <span class="hljs-comment" style="color: rgb(136, 0, 0);">//调用info()方法</span>
    (<span class="hljs-number" style="color: rgb(0, 102, 102);">4</span>)用工厂方式来创建(内置对象)
    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">var</span> jsCreater = <span class="hljs-keyword" style="color: rgb(0, 0, 136);">new</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102);">Object</span>();
    jsCreater.name = <span class="hljs-string" style="color: rgb(0, 136, 0);">"Brendan Eich"</span>; <span class="hljs-comment" style="color: rgb(136, 0, 0);">//JavaScript的发明者</span>
    jsCreater.work = <span class="hljs-string" style="color: rgb(0, 136, 0);">"JavaScript"</span>;
    jsCreater.info = <span class="hljs-function"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">function</span>(<span class="hljs-params" style="color: rgb(102, 0, 102);"></span>) </span>{
      alert(<span class="hljs-string" style="color: rgb(0, 136, 0);">"我是"</span>+<span class="hljs-keyword" style="color: rgb(0, 0, 136);">this</span>.work+<span class="hljs-string" style="color: rgb(0, 136, 0);">"的发明者"</span>+<span class="hljs-keyword" style="color: rgb(0, 0, 136);">this</span>.name);
    }
    jsCreater.info();
    (<span class="hljs-number" style="color: rgb(0, 102, 102);">5</span>)用原型方式来创建
    <span class="hljs-function"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">function</span> <span class="hljs-title">Standard</span>(<span class="hljs-params" style="color: rgb(102, 0, 102);"></span>)</span>{}
    Standard.prototype.name = <span class="hljs-string" style="color: rgb(0, 136, 0);">"ECMAScript"</span>;
    Standard.prototype.event = <span class="hljs-function"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">function</span>(<span class="hljs-params" style="color: rgb(102, 0, 102);"></span>) </span>{
      alert(<span class="hljs-keyword" style="color: rgb(0, 0, 136);">this</span>.name+<span class="hljs-string" style="color: rgb(0, 136, 0);">"是脚本语言标准规范"</span>);
    }
    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">var</span> jiaoben = <span class="hljs-keyword" style="color: rgb(0, 0, 136);">new</span> Standard();
    jiaoben.event();
    (<span class="hljs-number" style="color: rgb(0, 102, 102);">6</span>)用混合方式来创建
    <span class="hljs-function"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">function</span> <span class="hljs-title">iPhone</span>(<span class="hljs-params" style="color: rgb(102, 0, 102);">name,event</span>) </span>{
      <span class="hljs-keyword" style="color: rgb(0, 0, 136);">this</span>.name = name;
      <span class="hljs-keyword" style="color: rgb(0, 0, 136);">this</span>.event = event;
    }
    iPhone.prototype.sell = <span class="hljs-function"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">function</span>(<span class="hljs-params" style="color: rgb(102, 0, 102);"></span>) </span>{
      alert(<span class="hljs-string" style="color: rgb(0, 136, 0);">"我是"</span>+<span class="hljs-keyword" style="color: rgb(0, 0, 136);">this</span>.name+<span class="hljs-string" style="color: rgb(0, 136, 0);">",我是iPhone5s的"</span>+<span class="hljs-keyword" style="color: rgb(0, 0, 136);">this</span>.event+<span class="hljs-string" style="color: rgb(0, 136, 0);">"~ haha!"</span>);
    }
    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">var</span> SE = <span class="hljs-keyword" style="color: rgb(0, 0, 136);">new</span> iPhone(<span class="hljs-string" style="color: rgb(0, 136, 0);">"iPhone SE"</span>,<span class="hljs-string" style="color: rgb(0, 136, 0);">"官方翻新机"</span>);
    SE.sell();</code>
  • eval 是做什么的?
    <code class="bash">它的功能是把对应的字符串解析成JS代码并运行;
    应该避免使用<span class="hljs-built_in" style="color: rgb(102, 0, 102);">eval</span>,因为不安全,非常耗性能(<span class="hljs-number" style="color: rgb(0, 102, 102);">2</span>次,一次解析成js语句,一次执行)。</code>
  • 什么是 document 对象?什么是 window 对象?
  • null 和 undefined 有何区别?
    <code class="javascript"><span class="hljs-literal" style="color: rgb(0, 102, 102);">null</span>        表示一个对象被定义了,值为“空值”;
    <span class="hljs-literal" style="color: rgb(0, 102, 102);">undefined</span>   表示不存在这个值。
    <span class="hljs-comment" style="color: rgb(136, 0, 0);">//</span>
    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">typeof</span> <span class="hljs-literal" style="color: rgb(0, 102, 102);">undefined</span>
      <span class="hljs-comment" style="color: rgb(136, 0, 0);">//"undefined"</span>
      <span class="hljs-literal" style="color: rgb(0, 102, 102);">undefined</span> :是一个表示<span class="hljs-string" style="color: rgb(0, 136, 0);">"无"</span>的原始值或者说表示<span class="hljs-string" style="color: rgb(0, 136, 0);">"缺少值"</span>,就是此处应该有一个值,但是还没有定义。当尝试读取时会返回 <span class="hljs-literal" style="color: rgb(0, 102, 102);">undefined</span>; 
      例如变量被声明了,但没有赋值时,就等于<span class="hljs-literal" style="color: rgb(0, 102, 102);">undefined</span>。
    <span class="hljs-comment" style="color: rgb(136, 0, 0);">//</span>
    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">typeof</span> <span class="hljs-literal" style="color: rgb(0, 102, 102);">null</span>
      <span class="hljs-comment" style="color: rgb(136, 0, 0);">//"object"</span>
      <span class="hljs-literal" style="color: rgb(0, 102, 102);">null</span> : 是一个对象(空对象, 没有任何属性和方法);
      例如作为函数的参数,表示该函数的参数不是对象;
    <span class="hljs-comment" style="color: rgb(136, 0, 0);">//</span>
    注意:
      在验证<span class="hljs-literal" style="color: rgb(0, 102, 102);">null</span>时,一定要使用 === ,因为 == 无法分别 <span class="hljs-literal" style="color: rgb(0, 102, 102);">null</span> 和 <span class="hljs-literal" style="color: rgb(0, 102, 102);">undefined</span></code>
  • 能否写一个通用的事件侦听器函数?
    <code class="javascript"><span class="hljs-comment" style="color: rgb(136, 0, 0);">//Event工具集,from:github.com/markyun</span>
    markyun.Event = {
      <span class="hljs-comment" style="color: rgb(136, 0, 0);">//页面加载完成后</span>
      readyEvent: <span class="hljs-function"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">function</span>(<span class="hljs-params" style="color: rgb(102, 0, 102);">fn</span>) </span>{
          <span class="hljs-keyword" style="color: rgb(0, 0, 136);">if</span> (fn == <span class="hljs-literal" style="color: rgb(0, 102, 102);">null</span>) {
              fn = <span class="hljs-built_in" style="color: rgb(102, 0, 102);">document</span>;
          }
          <span class="hljs-keyword" style="color: rgb(0, 0, 136);">var</span> oldonload = <span class="hljs-built_in" style="color: rgb(102, 0, 102);">window</span>.onload;
          <span class="hljs-keyword" style="color: rgb(0, 0, 136);">if</span> (<span class="hljs-keyword" style="color: rgb(0, 0, 136);">typeof</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102);">window</span>.onload != <span class="hljs-string" style="color: rgb(0, 136, 0);">'function'</span>) {
              <span class="hljs-built_in" style="color: rgb(102, 0, 102);">window</span>.onload = fn;
          }<span class="hljs-keyword" style="color: rgb(0, 0, 136);">else</span>{
              <span class="hljs-built_in" style="color: rgb(102, 0, 102);">window</span>.onload = <span class="hljs-function"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">function</span>(<span class="hljs-params" style="color: rgb(102, 0, 102);"></span>) </span>{
                  oldonload();
                  fn();
              };
          }
      },
      <span class="hljs-comment" style="color: rgb(136, 0, 0);">//视能力分别使用 demo0 || demo1 || IE 方式来绑定事件</span>
      <span class="hljs-comment" style="color: rgb(136, 0, 0);">//参数:操作的元素,事件名称,事件处理程序</span>
      addEvent: <span class="hljs-function"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">function</span>(<span class="hljs-params" style="color: rgb(102, 0, 102);">element,type,handler</span>) </span>{
          <span class="hljs-keyword" style="color: rgb(0, 0, 136);">if</span> (element.addEventListener) {
              <span class="hljs-comment" style="color: rgb(136, 0, 0);">//事件类型、需要执行的函数、是否捕捉</span>
              element.addEventListener(type,handler,<span class="hljs-literal" style="color: rgb(0, 102, 102);">false</span>);
          }<span class="hljs-keyword" style="color: rgb(0, 0, 136);">else</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">if</span> (element.attachEvent) {
              element.attachEvent(<span class="hljs-string" style="color: rgb(0, 136, 0);">'on'</span> + type, <span class="hljs-function"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">function</span>(<span class="hljs-params" style="color: rgb(102, 0, 102);"></span>) </span>{
                  handler.call(element);
              });
          }<span class="hljs-keyword" style="color: rgb(0, 0, 136);">else</span> {
              element[<span class="hljs-string" style="color: rgb(0, 136, 0);">'on'</span> + type] = handler;
          }
      },
      <span class="hljs-comment" style="color: rgb(136, 0, 0);">//移除事件</span>
      removeEvent: <span class="hljs-function"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">function</span>(<span class="hljs-params" style="color: rgb(102, 0, 102);">element,type,handler</span>) </span>{
          <span class="hljs-keyword" style="color: rgb(0, 0, 136);">if</span> (element.removeEventListener) {
              element.removeEventListener(type,handler,<span class="hljs-literal" style="color: rgb(0, 102, 102);">false</span>);
          }<span class="hljs-keyword" style="color: rgb(0, 0, 136);">else</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">if</span> (element.datachEvent) {
              element.datachEvent(<span class="hljs-string" style="color: rgb(0, 136, 0);">'on'</span> + type,handler);
          }<span class="hljs-keyword" style="color: rgb(0, 0, 136);">else</span>{
              element[<span class="hljs-string" style="color: rgb(0, 136, 0);">'on'</span> + type] = <span class="hljs-literal" style="color: rgb(0, 102, 102);">null</span>;
          }
      },
      <span class="hljs-comment" style="color: rgb(136, 0, 0);">//阻止事件(主要是事件冒泡,因为IE不支持事件捕获)</span>
      stopPropagation: <span class="hljs-function"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">function</span>(<span class="hljs-params" style="color: rgb(102, 0, 102);">ev</span>) </span>{
          <span class="hljs-keyword" style="color: rgb(0, 0, 136);">if</span> (ev.stopPropagation) {
              ev.stopPropagation();
          }<span class="hljs-keyword" style="color: rgb(0, 0, 136);">else</span> {
              ev.cancelBubble = <span class="hljs-literal" style="color: rgb(0, 102, 102);">true</span>;
          }
      },
      <span class="hljs-comment" style="color: rgb(136, 0, 0);">//取消事件的默认行为</span>
      preventDefault: <span class="hljs-function"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">function</span>(<span class="hljs-params" style="color: rgb(102, 0, 102);">event</span>) </span>{
          <span class="hljs-keyword" style="color: rgb(0, 0, 136);">if</span> (event.preventDefault) {
              event.preventDefault();
          }<span class="hljs-keyword" style="color: rgb(0, 0, 136);">else</span>{
              event.returnValue = <span class="hljs-literal" style="color: rgb(0, 102, 102);">false</span>;
          }
      },
      <span class="hljs-comment" style="color: rgb(136, 0, 0);">//获取事件目标</span>
      getTarget: <span class="hljs-function"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">function</span>(<span class="hljs-params" style="color: rgb(102, 0, 102);">event</span>) </span>{
          <span class="hljs-keyword" style="color: rgb(0, 0, 136);">return</span> event.target || event.srcElemnt;
      },
      <span class="hljs-comment" style="color: rgb(136, 0, 0);">//获取event对象的引用,取到事件的所有信息,确保随时能使用event;</span>
      getEvent: <span class="hljs-function"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">function</span>(<span class="hljs-params" style="color: rgb(102, 0, 102);">e</span>) </span>{
          <span class="hljs-keyword" style="color: rgb(0, 0, 136);">var</span> ev = e || <span class="hljs-built_in" style="color: rgb(102, 0, 102);">window</span>.event;
          <span class="hljs-keyword" style="color: rgb(0, 0, 136);">if</span> (!ev) {
              <span class="hljs-keyword" style="color: rgb(0, 0, 136);">var</span> c = <span class="hljs-keyword" style="color: rgb(0, 0, 136);">this</span>.getEvent.caller;
              <span class="hljs-keyword" style="color: rgb(0, 0, 136);">while</span>(c) {
                  ev = c.argument[<span class="hljs-number" style="color: rgb(0, 102, 102);">0</span>];
                  <span class="hljs-keyword" style="color: rgb(0, 0, 136);">if</span> (ev && Event == ev.constructor) {
                      <span class="hljs-keyword" style="color: rgb(0, 0, 136);">break</span>;
                  }
                  c = c.caller;
              }
          }
          retrun ev;
      }
    };</code>
  • ["1","2","3"].map(parseInt) 的答案是多少?
    <code class="scheme">[<span class="hljs-keyword" style="color: rgb(0, 0, 136);">1</span>,NaN,NaN]
    因为 parseInt 需要两个参数<span class="hljs-list">(<span class="hljs-keyword" style="color: rgb(0, 0, 136);">val</span>,radix)</span>,其中 radix 表示解析时用的基数。
    map 传了3个<span class="hljs-list">(<span class="hljs-keyword" style="color: rgb(0, 0, 136);">element</span>,index,array)</span>,对应的 radix 不合法导致解析失败。</code>
  • 事件是什么?IE与火狐的事件机制有何区别?如何阻止冒泡?
    <code class="cpp">(<span class="hljs-number" style="color: rgb(0, 102, 102);">1</span>)我们在网页中的某个操作(有的操作对应多个事件)。例如:当我们点击一个按钮就会产生一个事件。是可以被 JavaScript 侦测到的行为。
    (<span class="hljs-number" style="color: rgb(0, 102, 102);">2</span>)事件处理机制:IE是事件冒泡、Firefox同时支持两种事件模型,也就是:捕获型事件和冒泡型事件;
    (<span class="hljs-number" style="color: rgb(0, 102, 102);">3</span>)ev.stopPropagation();(旧ie的方法 ev.cancelBubble = <span class="hljs-literal" style="color: rgb(0, 102, 102);">true</span>;)</code>
  • 什么是闭包(closure),为什么要用它?
    <code class="xml">闭包是指有权访问另一个函数作用域中变量的函数,创建闭包的最常见的方式就是在一个函数内创建另一个函数,通过另一个函数访问这个函数的局部变量,利用闭包可以突破作用链域,将函数内部的变量和方法传递到外部。
    //
    闭包特性:
    (1)函数内再嵌套函数
    (2)内部函数可以引用外层的参数和变量
    (3)参数和变量不会被垃圾回收机制回收
    //li节点的onclick事件都能正确的弹出当前被点击的li索引
    <span class="hljs-tag" style="color: rgb(0, 102, 102);"><<span class="hljs-title" style="color: rgb(0, 0, 136);">ul</span>></span>
      <span class="hljs-tag" style="color: rgb(0, 102, 102);"><<span class="hljs-title" style="color: rgb(0, 0, 136);">li</span>></span> index = 0 <span class="hljs-tag" style="color: rgb(0, 102, 102);"></<span class="hljs-title" style="color: rgb(0, 0, 136);">li</span>></span>
      <span class="hljs-tag" style="color: rgb(0, 102, 102);"><<span class="hljs-title" style="color: rgb(0, 0, 136);">li</span>></span> index = 1 <span class="hljs-tag" style="color: rgb(0, 102, 102);"></<span class="hljs-title" style="color: rgb(0, 0, 136);">li</span>></span>
      <span class="hljs-tag" style="color: rgb(0, 102, 102);"><<span class="hljs-title" style="color: rgb(0, 0, 136);">li</span>></span> index = 2 <span class="hljs-tag" style="color: rgb(0, 102, 102);"></<span class="hljs-title" style="color: rgb(0, 0, 136);">li</span>></span>
      <span class="hljs-tag" style="color: rgb(0, 102, 102);"><<span class="hljs-title" style="color: rgb(0, 0, 136);">li</span>></span> index = 3 <span class="hljs-tag" style="color: rgb(0, 102, 102);"></<span class="hljs-title" style="color: rgb(0, 0, 136);">li</span>></span>
    <span class="hljs-tag" style="color: rgb(0, 102, 102);"></<span class="hljs-title" style="color: rgb(0, 0, 136);">ul</span>></span>
    <span class="hljs-tag" style="color: rgb(0, 102, 102);"><<span class="hljs-title" style="color: rgb(0, 0, 136);">script</span> <span class="hljs-attribute" style="color: rgb(102, 0, 102);">type</span>=<span class="hljs-value" style="color: rgb(0, 136, 0);">"text/javascript"</span>></span><span class="javascript">
      <span class="hljs-keyword" style="color: rgb(0, 0, 136);">var</span> nodes = <span class="hljs-built_in" style="color: rgb(102, 0, 102);">document</span>.getElementsByTagName(<span class="hljs-string" style="color: rgb(0, 136, 0);">'li'</span>);
      <span class="hljs-keyword" style="color: rgb(0, 0, 136);">for</span>(i = <span class="hljs-number" style="color: rgb(0, 102, 102);">0</span>;i<nodes.length;i+=<span class="hljs-number" style="color: rgb(0, 102, 102);">1</span>) {
          nodes[i].onclick = <span class="hljs-function"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">function</span>(<span class="hljs-params" style="color: rgb(102, 0, 102);"></span>) </span>{
              <span class="hljs-built_in" style="color: rgb(102, 0, 102);">console</span>.log(i+<span class="hljs-number" style="color: rgb(0, 102, 102);">1</span>); <span class="hljs-comment" style="color: rgb(136, 0, 0);">//不使用闭包的话,值每次都是4</span>
          }(<span class="hljs-number" style="color: rgb(0, 102, 102);">4</span>);
      }
    </span><span class="hljs-tag" style="color: rgb(0, 102, 102);"></<span class="hljs-title" style="color: rgb(0, 0, 136);">script</span>></span></code>
  • JavaScript 代码中的 "use strict"; 是什么意思?使用它的区别是什么?
    <code class="sql"><span class="hljs-operator"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">use</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">strict</span>是一种ECMAscript <span class="hljs-number" style="color: rgb(0, 102, 102);">5</span> 添加的(严格)运行模式,这种模式使得 Javascript 在更严格的条件下运行,使JS编码更加规范化的模式,消除Javascript语法的一些不合理、不严谨之处,减少一些怪异行为。
    默认支持的糟糕特性都会被禁用,比如不能用<span class="hljs-keyword" style="color: rgb(0, 0, 136);">with</span>,也不能在意外的情况下给全局变量赋值;</span>
    全局变量的显示声明,函数必须声明在顶层,不允许在非函数代码块内声明函数,arguments.callee也不允许使用;
    消除代码运行的一些不安全之处,保证代码运行的安全,限制函数中的arguments修改,严格模式下的eval函数的行为和非严格模式的也不相同;
    提高编译器效率,增加运行速度;
    为未来新版本的Javascript标准化做铺垫。</code>
  • new 操作符具体干了什么呢?
    <code class="cpp">(<span class="hljs-number" style="color: rgb(0, 102, 102);">1</span>)创建一个空对象,并且 <span class="hljs-keyword" style="color: rgb(0, 0, 136);">this</span> 变量引用该对象,同时还继承了该函数的原型。
    (<span class="hljs-number" style="color: rgb(0, 102, 102);">2</span>)属性和方法被加入到 <span class="hljs-keyword" style="color: rgb(0, 0, 136);">this</span> 引用的对象中。
    (<span class="hljs-number" style="color: rgb(0, 102, 102);">3</span>)新创建的对象由 <span class="hljs-keyword" style="color: rgb(0, 0, 136);">this</span> 所引用,并且最后隐式的返回 <span class="hljs-keyword" style="color: rgb(0, 0, 136);">this</span> 。
    <span class="hljs-comment" style="color: rgb(136, 0, 0);">//</span>
    var obj = {};
    obj.__proto__ = Base.prototype;
    Base.call(obj);</code>
  • 用原生的 JavaScript 实现过什么功能吗?
  • JavaScript 中,有一个函数,执行对象查找时,永远不会去查找原型,这个函数是哪个?
    <code class="scala">hasOwnProperty
    <span class="hljs-comment" style="color: rgb(136, 0, 0);">//</span>
    <span class="hljs-type">JavaScript</span> 中 hasOwnProperty 函数方法是返回一个布尔值,指出一个对象是否具有指定名称的属性。此方法无法检查该对象的原型链中是否具有该属性;该属性必须是对象本身的一个成员。
    <span class="hljs-comment" style="color: rgb(136, 0, 0);">//</span>
    使用方法:
    <span class="hljs-class"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">object</span>.<span class="hljs-title" style="color: rgb(102, 0, 102);">hasOwnProperty</span>(</span>proName)
    其中参数<span class="hljs-class"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">object</span><span class="hljs-title" style="color: rgb(102, 0, 102);">是必选项,一个对象的实例。</span>
    </span>proName是必选项,一个属性名称的字符串值。
    <span class="hljs-comment" style="color: rgb(136, 0, 0);">//</span>
    如果 <span class="hljs-class"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">object</span> <span class="hljs-title" style="color: rgb(102, 0, 102);">具有指定名称的属性,那么JavaScript中hasOwnProperty函数方法返回</span> <span class="hljs-title" style="color: rgb(102, 0, 102);">true,反之则返回</span> <span class="hljs-title" style="color: rgb(102, 0, 102);">false。</span></span></code>
  • 你对 JSON 了解吗?
    <code class="javascript"><span class="hljs-built_in" style="color: rgb(102, 0, 102);">JSON</span>(JavaScript <span class="hljs-built_in" style="color: rgb(102, 0, 102);">Object</span> Notation)是一种轻量级的数据交换格式。
    它是基于JavaScript的一个子集。数据格式简单,易于读写,占用带宽小。
    如:
    {<span class="hljs-string" style="color: rgb(0, 136, 0);">"age"</span>:<span class="hljs-string" style="color: rgb(0, 136, 0);">"12"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0);">"name"</span>:<span class="hljs-string" style="color: rgb(0, 136, 0);">"back"</span>}</code>
  • js延迟加载的方式有哪些?
    <code class="cs">defer和<span class="hljs-keyword" style="color: rgb(0, 0, 136);">async</span>、动态创建DOM方式(用得最多)、按需异步载入js</code>
  • Ajax 是什么?如何创建一个 Ajax ?
    <code class="cpp">ajax的全称:Asynchronous Javascript And XML。
    异步传输+js+xml。
    所谓异步,在这里简单地解释就是:向服务器发送请求的时候,我们不必等待结果,而是可以同时做其他的事情,等到有了结果它自己会根据设定进行后续操作,与此同时,页面是不会发生整页刷新的,提高了用户体验。
    <span class="hljs-comment" style="color: rgb(136, 0, 0);">//</span>
    (<span class="hljs-number" style="color: rgb(0, 102, 102);">1</span>)创建XMLHttpRequest对象,也就是创建一个异步调用对象
    (<span class="hljs-number" style="color: rgb(0, 102, 102);">2</span>)创建一个新的HTTP请求,并指定该HTTP请求的方法、URL及验证信息
    (<span class="hljs-number" style="color: rgb(0, 102, 102);">3</span>)设置响应HTTP请求状态变化的函数
    (<span class="hljs-number" style="color: rgb(0, 102, 102);">4</span>)发送HTTP请求
    (<span class="hljs-number" style="color: rgb(0, 102, 102);">5</span>)获取异步调用返回的数据
    (<span class="hljs-number" style="color: rgb(0, 102, 102);">6</span>)使用JavaScript和DOM实现局部刷新</code>
  • 同步和异步的区别?
    <code class="objectivec">同步的概念应该是来自于操作系统中关于同步的概念:
    不同进程为协同完成某项工作而在先后次序上调整(通过阻塞,唤醒等方式)。同步强调的是顺序性,谁先谁后;异步则不存在这种顺序性。
    <span class="hljs-comment" style="color: rgb(136, 0, 0);">//</span>
    同步:浏览器访问服务器请求,用户看得到页面刷新,重新发请求,等请求完,页面刷新,新内容出现,用户看到新内容,进行下一步操作。
    <span class="hljs-comment" style="color: rgb(136, 0, 0);">//</span>
    异步:浏览器访问服务器请求,用户正常操作,浏览器后端进行请求。等请求完,页面不刷新,新内容也会出现,用户看到新内容。</code>
  • 如何解决跨域问题?
    <code class="javascript">jsonp、iframe、<span class="hljs-built_in" style="color: rgb(102, 0, 102);">window</span>.name、<span class="hljs-built_in" style="color: rgb(102, 0, 102);">window</span>.postMessage、服务器上设置代理页面</code>
  • 页面编码和被请求的资源编码如果不一致如何处理?
  • 谈一谈你对 ECMAScript6 的了解?
    <code class="cpp">ECMAScript <span class="hljs-number" style="color: rgb(0, 102, 102);">6</span> 是JavaScript语言的下一代标准,已经在<span class="hljs-number" style="color: rgb(0, 102, 102);">2015</span>年<span class="hljs-number" style="color: rgb(0, 102, 102);">6</span>月正式发布了。它的目标,是使得JavaScript语言可以用来编写复杂的大型应用程序,成为企业级开发语言。
    标准的制定者有计划,以后每年发布一次标准,使用年份作为标准的版本。因为当前版本的ES6是在<span class="hljs-number" style="color: rgb(0, 102, 102);">2015</span>年发布的,所以又称ECMAScript <span class="hljs-number" style="color: rgb(0, 102, 102);">2015</span>。也就是说,ES6就是ES2015</code>
  • ECMAScript 6 怎么写 class ,为何会出现 class?
    <code class="cpp">ES6的<span class="hljs-keyword" style="color: rgb(0, 0, 136);">class</span>可以看作只是一个语法糖,它的绝大部分功能,ES5都可以做到,新的<span class="hljs-keyword" style="color: rgb(0, 0, 136);">class</span>写法只是让对象原型的写法更加清晰、更像面向对象编程的语法而已。
    <span class="hljs-comment" style="color: rgb(136, 0, 0);">//定义类</span>
    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">class</span> Point {
      constructor(x,y) {  <span class="hljs-comment" style="color: rgb(136, 0, 0);">//构造方法</span>
          <span class="hljs-keyword" style="color: rgb(0, 0, 136);">this</span>.x = x;  <span class="hljs-comment" style="color: rgb(136, 0, 0);">//this关键字代表实例对象</span>
          <span class="hljs-keyword" style="color: rgb(0, 0, 136);">this</span>.y = y;
      }
      toString() {
          <span class="hljs-keyword" style="color: rgb(0, 0, 136);">return</span> <span class="hljs-string" style="color: rgb(0, 136, 0);">'('</span> + <span class="hljs-keyword" style="color: rgb(0, 0, 136);">this</span>.x + <span class="hljs-string" style="color: rgb(0, 136, 0);">','</span> + <span class="hljs-keyword" style="color: rgb(0, 0, 136);">this</span>.y + <span class="hljs-string" style="color: rgb(0, 136, 0);">')'</span>;
      }
    }</code>
  • 异步加载 JS 的方式有哪些?
    <code class="cpp">(<span class="hljs-number" style="color: rgb(0, 102, 102);">1</span>)defer,只支持 IE
    (<span class="hljs-number" style="color: rgb(0, 102, 102);">2</span>)async:
    (<span class="hljs-number" style="color: rgb(0, 102, 102);">3</span>)创建 script,插入到 DOM 中,加载完毕后 callBack</code>
  • document.write 和 innerHTML 有何区别?
    <code class="perl">document.<span class="hljs-keyword" style="color: rgb(0, 0, 136);">write</span> 只能重绘整个页面
    innerHTML 可以重绘页面的一部分</code>
  • DOM 操作——怎样添加、移除、移动、复制、创建和查找节点?
    <code class="cpp">(<span class="hljs-number" style="color: rgb(0, 102, 102);">1</span>)创建新节点
    createDocumentFragment()    <span class="hljs-comment" style="color: rgb(136, 0, 0);">//创建一个DOM片段</span>
    createElement()   <span class="hljs-comment" style="color: rgb(136, 0, 0);">//创建一个具体的元素</span>
    createTextNode()   <span class="hljs-comment" style="color: rgb(136, 0, 0);">//创建一个文本节点</span>
    (<span class="hljs-number" style="color: rgb(0, 102, 102);">2</span>)添加、移除、替换、插入
    appendChild()
    removeChild()
    replaceChild()
    insertBefore() <span class="hljs-comment" style="color: rgb(136, 0, 0);">//在已有的子节点前插入一个新的子节点</span>
    (<span class="hljs-number" style="color: rgb(0, 102, 102);">3</span>)查找
    getElementsByTagName()    <span class="hljs-comment" style="color: rgb(136, 0, 0);">//通过标签名称</span>
    getElementsByName()    <span class="hljs-comment" style="color: rgb(136, 0, 0);">//通过元素的Name属性的值(IE容错能力较强,会得到一个数组,其中包括id等于name值的)</span>
    getElementById()    <span class="hljs-comment" style="color: rgb(136, 0, 0);">//通过元素Id,唯一性</span></code>
  • 数组和对象有哪些原生方法?能否列举一下?
  • 哪些操作会造成内存泄漏?
    <code class="cpp">内存泄漏是指任何对象在您不再拥有或需要它之后任然存在。
    垃圾回收器定期扫描对象,并计算引用了每个对象的其他对象的数量,如果一个对象的引用数量为<span class="hljs-number" style="color: rgb(0, 102, 102);">0</span>(没有其他对象引用过该对象),或对该对象的惟一引用是循环的,那么该对象的内存即可回收。
    <span class="hljs-comment" style="color: rgb(136, 0, 0);">//</span>
    setTimeout 的第一个参数使用字符串而非函数的话,会引发内存泄漏。
    闭包、控制台日志、循环(在两个对象彼此引用且彼此保留时,就会产生一个循环)</code>
  • 是否看过 jQuery 的源码?能否简单概括一下它的实现原理?
  • jQuery.fn 的 init 方法返回的 this 指的是什么对象?为什么要返回 this ?
  • jQuery 中如何将数组转化为 json 字符串,然后再转化回来?
    <code class="javascript">jQuery 中没有提供这个功能,所以需要先编写两个 jQuery 的扩展:
    $.fn.stringifyArray = <span class="hljs-function"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">function</span>(<span class="hljs-params" style="color: rgb(102, 0, 102);">array</span>) </span>{
      <span class="hljs-keyword" style="color: rgb(0, 0, 136);">return</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102);">JSON</span>.stringify(array)
    }
    $.fn.parseArray = <span class="hljs-function"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">function</span>(<span class="hljs-params" style="color: rgb(102, 0, 102);">array</span>) </span>{
      <span class="hljs-keyword" style="color: rgb(0, 0, 136);">return</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102);">JSON</span>.parse(array)
    }
    <span class="hljs-comment" style="color: rgb(136, 0, 0);">//</span>
    然后调用:
    $(<span class="hljs-string" style="color: rgb(0, 136, 0);">""</span>).stringifyArray(array)</code>
  • jQuery 的属性拷贝(extend)的实现原理是什么?如何实现深拷贝?
  • jQuery.extend 与 jQuery.fn.extend 有何区别?
  • jQuery 的队列是如何实现的?队列可以用在哪些地方?
  • jQuery 中一个对象可以同时绑定多个事件,这是如何实现的?
  • 是否了解针对 jQuery 性能的优化方法?
    <code class="javascript">基于Class的选择性的性能相对于Id选择器开销很大,因为需遍历所有DOM元素。
    <span class="hljs-comment" style="color: rgb(136, 0, 0);">//</span>
    频繁操作的DOM,先缓存起来再操作。用Jquery的链式调用更好。
    比如:<span class="hljs-keyword" style="color: rgb(0, 0, 136);">var</span> str=$(<span class="hljs-string" style="color: rgb(0, 136, 0);">"a"</span>).attr(<span class="hljs-string" style="color: rgb(0, 136, 0);">"href"</span>);
    <span class="hljs-comment" style="color: rgb(136, 0, 0);">//</span>
    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">for</span> (<span class="hljs-keyword" style="color: rgb(0, 0, 136);">var</span> i = size; i < arr.length; i++) {}
    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">for</span> 循环每一次循环都查找了数组 (arr) 的.length 属性,在开始循环的时候设置一个变量来存储这个数字,可以让循环跑得更快:
    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">for</span> (<span class="hljs-keyword" style="color: rgb(0, 0, 136);">var</span> i = size, length = arr.length; i < length; i++) {}</code>
  • jQuery 与 jQuery UI 有何区别?
    <code class="perl"><span class="hljs-string" style="color: rgb(0, 136, 0);">`jQuery`</span>是一个js库,主要提供的功能是选择器,属性修改和事件绑定等等。
    <span class="hljs-string" style="color: rgb(0, 136, 0);">`jQuery UI`</span>则是在jQuery的基础上,利用jQuery的扩展性,设计的插件。提供了一些常用的界面元素,诸如对话框、拖动行为、改变大小行为等等</code>
  • jQuery UI 如何自定义组件?
  • 如何判断当前脚本运行在浏览器还是 node 环境中?(阿里)
    <code class="javascript">通过判断 Global 对象是否为 <span class="hljs-built_in" style="color: rgb(102, 0, 102);">window</span> ,如果不为 <span class="hljs-built_in" style="color: rgb(102, 0, 102);">window</span> ,当前脚本没有运行在浏览器中</code>
  • 什么是“前端路由”?什么时候适合使用“前端路由”?“前端路由”有哪些优点和缺点?
  • 怎样用js实现千位分隔符?
    <code class="javascript">正则 + replace
    <span class="hljs-function"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">function</span> <span class="hljs-title">commafy</span>(<span class="hljs-params" style="color: rgb(102, 0, 102);">num</span>) </span>{
      num = num + <span class="hljs-string" style="color: rgb(0, 136, 0);">''</span>;
      <span class="hljs-keyword" style="color: rgb(0, 0, 136);">var</span> reg = <span class="hljs-regexp" style="color: rgb(0, 136, 0);">/(-?d+)(d{3})/</span>;
      <span class="hljs-keyword" style="color: rgb(0, 0, 136);">if</span> (reg.test(num)) {
          num = num.replace(reg, <span class="hljs-string" style="color: rgb(0, 136, 0);">'$1,$2'</span>);
      }
      <span class="hljs-keyword" style="color: rgb(0, 0, 136);">return</span> num;
    }</code>
  • 检测浏览器版本有哪些方式?
    <code class="objectivec">功能检测、userAgent 特征检测
    比如:navigator<span class="hljs-variable" style="color: rgb(102, 0, 102);">.userAgent</span>
    <span class="hljs-comment" style="color: rgb(136, 0, 0);">//"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.101 Safari/537.36"</span></code>
  • 谈谈你对 JavaScript 中的模块规范 CommonJS、AMD、CMD 的了解?
    <code class="objectivec"><span class="hljs-comment" style="color: rgb(136, 0, 0);">//个人拙见</span>
    |   CommonJS   |   AMD   |   CMD   |
    |--------------|---------|---------|
    |    Node<span class="hljs-variable" style="color: rgb(102, 0, 102);">.js</span>   |RequireJS|  SeaJS  |</code>
    详细文章:浅析JS中的模块规范(CommonJS,AMD,CMD)关于 CommonJS AMD CMD UMD
  • 前端 MVC、MVVM
    1、MVC

    MVC
    <code class="cpp">模型(Model):数据保存
    视图(View):用户界面
    控制器(Controller):业务逻辑
    (<span class="hljs-number" style="color: rgb(0, 102, 102);">1</span>)View 传送指令到 Controller
    (<span class="hljs-number" style="color: rgb(0, 102, 102);">2</span>)Controller 完成业务逻辑后,要求 Model 改变状态
    (<span class="hljs-number" style="color: rgb(0, 102, 102);">3</span>)Model 将新的数据发送到 View ,用户得到反馈
    所有通信都是单向的。</code>
    2、MVVM

    MVVM
    <code class="cpp">模型(Model)
    视图(View)
    视图模型(ViewModel)
    (<span class="hljs-number" style="color: rgb(0, 102, 102);">1</span>)各部分间都是双向通信
    (<span class="hljs-number" style="color: rgb(0, 102, 102);">2</span>)View 与 Model 不发生联系,都通过 ViewModel 传递
    (<span class="hljs-number" style="color: rgb(0, 102, 102);">3</span>)View 非常薄,不部署任何业务逻辑,称为“被动视图”(Passive View),即没有任何主动性;而 ViewModel 非常厚,所有逻辑都部署在那里
    采用双向绑定(data-binding):View 的变动,自动反映在 ViewModel ,反之亦然。</code>

系列:
前端开发面试题之 HTML
前端开发面试题之综合篇
前端开发面试题之 CSS


资料搜集整理自 网络
同时发布在 GitHub-前端开发面试题之 JavaScriptGitHub-《WEB-DE》前端开发面试题之 JavaScript

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值