text: (window.webpackJsonp=window.webpackJsonp||[]).push([[120],{458:function(t,s,n){"use strict";n.r(s);var a=n(2),e=Object(a.a)({},(function(){var t=this,s=t.$createElement,n=t._self._c||s;return n("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[n("h1",{attrs:{id:"题目"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#题目"}},[t._v("#")]),t._v(" 题目")]),t._v(" "),n("div",{staticClass:"language-js line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-js"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("function")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Foo")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n    "),n("span",{pre:!0,attrs:{class:"token function-variable function"}},[t._v("getName")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("function")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("alert")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token number"}},[t._v("1")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),t._v("\n    "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("this")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\nFoo"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function-variable function"}},[t._v("getName")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("function")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("alert")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token number"}},[t._v("2")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token class-name"}},[t._v("Foo")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("prototype"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function-variable function"}},[t._v("getName")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("function")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("alert")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token number"}},[t._v("3")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("var")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function-variable function"}},[t._v("getName")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("function")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("alert")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token number"}},[t._v("4")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("function")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("getName")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("alert")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token number"}},[t._v("5")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n \n"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//请写出以下输出结果：")]),t._v("\nFoo"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("getName")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("getName")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Foo")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("getName")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("getName")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("new")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token class-name"}},[t._v("Foo"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("getName")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("new")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token class-name"}},[t._v("Foo")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("getName")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("new")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token class-name"}},[t._v("new")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Foo")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("getName")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),t._v("\n")])]),t._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[t._v("1")]),n("br"),n("span",{staticClass:"line-number"},[t._v("2")]),n("br"),n("span",{staticClass:"line-number"},[t._v("3")]),n("br"),n("span",{staticClass:"line-number"},[t._v("4")]),n("br"),n("span",{staticClass:"line-number"},[t._v("5")]),n("br"),n("span",{staticClass:"line-number"},[t._v("6")]),n("br"),n("span",{staticClass:"line-number"},[t._v("7")]),n("br"),n("span",{staticClass:"line-number"},[t._v("8")]),n("br"),n("span",{staticClass:"line-number"},[t._v("9")]),n("br"),n("span",{staticClass:"line-number"},[t._v("10")]),n("br"),n("span",{staticClass:"line-number"},[t._v("11")]),n("br"),n("span",{staticClass:"line-number"},[t._v("12")]),n("br"),n("span",{staticClass:"line-number"},[t._v("13")]),n("br"),n("span",{staticClass:"line-number"},[t._v("14")]),n("br"),n("span",{staticClass:"line-number"},[t._v("15")]),n("br"),n("span",{staticClass:"line-number"},[t._v("16")]),n("br"),n("span",{staticClass:"line-number"},[t._v("17")]),n("br")])]),n("p",[t._v("这几天面试上几次碰上这道经典的题目，特地从头到尾来分析一次答案，这道题的经典之处在于它综合考察了面试者的JavaScript的综合能力，包含了变量定义提升、this指针指向、运算符优先级、原型、继承、全局变量污染、对象属性及原型属性优先级等知识，此题在网上也有部分相关的解释，当然我觉得有部分解释还欠妥，不够清晰，特地重头到尾来分析一次，当然我们会把最终答案放在后面，并把此题再改高一点点难度，改进版也放在最后，方便面试官在出题的时候有个参考，更多详情可关注本文作者"),n("a",{attrs:{href:"https://github.com/Wscats",target:"_blank",rel:"noopener noreferrer"}},[t._v("@Wscats"),n("OutboundLink")],1),t._v(" "),n("a",{attrs:{href:"https://cloud.githubusercontent.com/assets/17243165/24434776/377f6f14-1464-11e7-9dcc-75c1000040d7.png",target:"_blank",rel:"noopener noreferrer"}},[n("img",{attrs:{src:"https://cloud.githubusercontent.com/assets/17243165/24434776/377f6f14-1464-11e7-9dcc-75c1000040d7.png",alt:"image"}}),n("OutboundLink")],1)]),t._v(" "),n("h1",{attrs:{id:"第一问"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#第一问"}},[t._v("#")]),t._v(" 第一问")]),t._v(" "),n("p",[t._v("先看此题的上半部分做了什么，首先定义了一个叫Foo的函数，之后为Foo创建了一个叫getName的静态属性存储了一个匿名函数，之后为Foo的原型对象新创建了一个叫getName的匿名函数。之后又通过函数变量表达式创建了一个getName的函数，最后再声明一个叫getName函数。")]),t._v(" "),n("p",[t._v("第一问的"),n("strong",[t._v("Foo.getName")]),t._v("自然是访问Foo函数上存储的静态属性，答案自然是2，这里就不需要解释太多的，一般来说第一问对于稍微懂JS基础的同学来说应该是没问题的,当然我们可以用下面的代码来回顾一下基础，先加深一下了解")]),t._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[t._v("function User(name) {\n\tvar name = name; //私有属性\n\tthis.name = name; //公有属性\n\tfunction getName() { //私有方法\n\t\treturn name;\n\t}\n}\nUser.prototype.getName = function() { //公有方法\n\treturn this.name;\n}\nUser.name = 'Wscats'; //静态属性\nUser.getName = function() { //静态方法\n\treturn this.name;\n}\nvar Wscat = new User('Wscats'); //实例化\n")])]),t._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[t._v("1")]),n("br"),n("span",{staticClass:"line-number"},[t._v("2")]),n("br"),n("span",{staticClass:"line-number"},[t._v("3")]),n("br"),n("span",{staticClass:"line-number"},[t._v("4")]),n("br"),n("span",{staticClass:"line-number"},[t._v("5")]),n("br"),n("span",{staticClass:"line-number"},[t._v("6")]),n("br"),n("span",{staticClass:"line-number"},[t._v("7")]),n("br"),n("span",{staticClass:"line-number"},[t._v("8")]),n("br"),n("span",{staticClass:"line-number"},[t._v("9")]),n("br"),n("span",{staticClass:"line-number"},[t._v("10")]),n("br"),n("span",{staticClass:"line-number"},[t._v("11")]),n("br"),n("span",{staticClass:"line-number"},[t._v("12")]),n("br"),n("span",{staticClass:"line-number"},[t._v("13")]),n("br"),n("span",{staticClass:"line-number"},[t._v("14")]),n("br"),n("span",{staticClass:"line-number"},[t._v("15")]),n("br")])]),n("p",[t._v("注意下面这几点：")]),t._v(" "),n("ul",[n("li",[t._v("调用公有方法，公有属性，我们必需先实例化对象，也就是用new操作符实化对象，就可构造函数实例化对象的方法和属性，并且公有方法是不能调用私有方法和静态方法的")]),t._v(" "),n("li",[t._v("静态方法和静态属性就是我们无需实例化就可以调用")]),t._v(" "),n("li",[t._v("而对象的私有方法和属性,外部是不可以访问的")])]),t._v(" "),n("h1",{attrs:{id:"第二问"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#第二问"}},[t._v("#")]),t._v(" 第二问")]),t._v(" "),n("p",[t._v("第二问，直接调用getName函数。既然是直接调用那么就是访问当前上文作用域内的叫getName的函数，所以这里应该直接把关注点放在4和5上，跟1 2 3都没什么关系。当然后来我问了我的几个同事他们大多数回答了5。此处其实有两个坑，一是变量声明提升，二是函数表达式和函数声明的区别。\n我们来看看为什么，可参考"),n("a",{attrs:{href:"https://github.com/Wscats/Good-text-Share/issues/73",target:"_blank",rel:"noopener noreferrer"}},[t._v("(1)关于Javascript的函数声明和函数表达式"),n("OutboundLink")],1),t._v(" "),n("a",{attrs:{href:"https://github.com/Wscats/Good-text-Share/issues/86",target:"_blank",rel:"noopener noreferrer"}},[t._v("(2)关于JavaScript的变量提升"),n("OutboundLink")],1),t._v("\n在Javascript中，定义函数有两种类型")]),t._v(" "),n("h3",{attrs:{id:"函数声明"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#函数声明"}},[t._v("#")]),t._v(" 函数声明")]),t._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[t._v('// 函数声明\nfunction wscat(type) {\n\treturn type === "wscat";\n}\n')])]),t._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[t._v("1")]),n("br"),n("span",{staticClass:"line-number"},[t._v("2")]),n("br"),n("span",{staticClass:"line-number"},[t._v("3")]),n("br"),n("span",{staticClass:"line-number"},[t._v("4")]),n("br")])]),n("h3",{attrs:{id:"函数表达式"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#函数表达式"}},[t._v("#")]),t._v(" 函数表达式")]),t._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[t._v('// 函数表达式\nvar oaoafly = function(type) {\n\treturn type === "oaoafly";\n}\n')])]),t._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[t._v("1")]),n("br"),n("span",{staticClass:"line-number"},[t._v("2")]),n("br"),n("span",{staticClass:"line-number"},[t._v("3")]),n("br"),n("span",{staticClass:"line-number"},[t._v("4")]),n("br")])]),n("p",[t._v("先看下面这个经典问题，在一个程序里面同时用函数声明和函数表达式定义一个名为getName的函数")]),t._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[t._v("getName() //oaoafly\nvar getName = function() {\n\tconsole.log('wscat')\n}\ngetName() //wscat\nfunction getName() {\n\tconsole.log('oaoafly')\n}\ngetName() //wscat\n")])]),t._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[t._v("1")]),n("br"),n("span",{staticClass:"line-number"},[t._v("2")]),n("br"),n("span",{staticClass:"line-number"},[t._v("3")]),n("br"),n("span",{staticClass:"line-number"},[t._v("4")]),n("br"),n("span",{staticClass:"line-number"},[t._v("5")]),n("br"),n("span",{staticClass:"line-number"},[t._v("6")]),n("br"),n("span",{staticClass:"line-number"},[t._v("7")]),n("br"),n("span",{staticClass:"line-number"},[t._v("8")]),n("br"),n("span",{staticClass:"line-number"},[t._v("9")]),n("br")])]),n("p",[t._v("上面的代码看起来很类似，感觉也没什么太大差别。但实际上，Javascript函数上的一个“陷阱”就体现在Javascript两种类型的函数定义上。")]),t._v(" "),n("ul",[n("li",[n("strong",[t._v("JavaScript 解释器中存在一种变量声明被提升的机制，也就是说函数声明会被提升到作用域的最前面，即使写代码的时候是写在最后面，也还是会被提升至最前面。")])]),t._v(" "),n("li",[n("strong",[t._v("而用函数表达式创建的函数是在运行时进行赋值，且要等到表达式赋值完成后才能调用")])])]),t._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[t._v("var getName //变量被提升，此时为undefined\n\ngetName() //oaoafly 函数被提升 这里受函数声明的影响，虽然函数声明在最后可以被提升到最前面了\nvar getName = function() {\n\tconsole.log('wscat')\n} //函数表达式此时才开始覆盖函数声明的定义\ngetName() //wscat\nfunction getName() {\n\tconsole.log('oaoafly')\n}\ngetName() //wscat 这里就执行了函数表达式的值\n")])]),t._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[t._v("1")]),n("br"),n("span",{staticClass:"line-number"},[t._v("2")]),n("br"),n("span",{staticClass:"line-number"},[t._v("3")]),n("br"),n("span",{staticClass:"line-number"},[t._v("4")]),n("br"),n("span",{staticClass:"line-number"},[t._v("5")]),n("br"),n("span",{staticClass:"line-number"},[t._v("6")]),n("br"),n("span",{staticClass:"line-number"},[t._v("7")]),n("br"),n("span",{staticClass:"line-number"},[t._v("8")]),n("br"),n("span",{staticClass:"line-number"},[t._v("9")]),n("br"),n("span",{staticClass:"line-number"},[t._v("10")]),n("br"),n("span",{staticClass:"line-number"},[t._v("11")]),n("br")])]),n("p",[t._v("所以可以分解为这两个简单的问题来看清楚区别的本质")]),t._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[t._v("var getName;\nconsole.log(getName) //undefined\ngetName() //Uncaught TypeError: getName is not a function\nvar getName = function() {\n\tconsole.log('wscat')\n}            \n")])]),t._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[t._v("1")]),n("br"),n("span",{staticClass:"line-number"},[t._v("2")]),n("br"),n("span",{staticClass:"line-number"},[t._v("3")]),n("br"),n("span",{staticClass:"line-number"},[t._v("4")]),n("br"),n("span",{staticClass:"line-number"},[t._v("5")]),n("br"),n("span",{staticClass:"line-number"},[t._v("6")]),n("br")])]),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[t._v("var getName;\nconsole.log(getName) //function getName() {console.log('oaoafly')}\ngetName() //oaoafly\nfunction getName() {\n\tconsole.log('oaoafly')\n}\n")])]),t._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[t._v("1")]),n("br"),n("span",{staticClass:"line-number"},[t._v("2")]),n("br"),n("span",{staticClass:"line-number"},[t._v("3")]),n("br"),n("span",{staticClass:"line-number"},[t._v("4")]),n("br"),n("span",{staticClass:"line-number"},[t._v("5")]),n("br"),n("span",{staticClass:"line-number"},[t._v("6")]),n("br")])]),n("p",[t._v("这个区别看似微不足道，但在某些情况下确实是一个难以察觉并且“致命“的陷阱。出现这个陷阱的本质原因体现在这两种类型在函数提升和运行时机（解析时/运行时）上的差异。\n当然我们给一个总结：Javascript中"),n("strong",[t._v("函数声明")]),t._v("和"),n("strong",[t._v("函数表达式")]),t._v("是存在区别的，"),n("strong",[t._v("函数声明")]),t._v("在JS"),n("strong",[t._v("解析时")]),t._v("进行函数提升，因此在同一个作用域内，不管函数声明在哪里定义，该函数都可以进行调用。而"),n("strong",[t._v("函数表达式")]),t._v("的值是在JS"),n("strong",[t._v("运行时")]),t._v("确定，并且在表达式赋值完成后，该函数才能调用。\n所以第二问的答案就是4，5的函数声明被4的函数表达式覆盖了")]),t._v(" "),n("h1",{attrs:{id:"第三问"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#第三问"}},[t._v("#")]),t._v(" 第三问")]),t._v(" "),n("p",[n("code",[t._v("Foo().getName();")]),t._v("先执行了Foo函数，然后调用Foo函数的返回值对象的getName属性函数。\nFoo函数的第一句"),n("code",[t._v("getName = function () { alert (1); };")]),t._v("是一句函数赋值语句，注意它没有var声明，所以先向当前Foo函数作用域内寻找getName变量，没有。再向当前函数作用域上层，即外层作用域内寻找是否含有getName变量，找到了，也就是第二问中的alert(4)函数，将此变量的值赋值为"),n("code",[t._v("function(){alert(1)}")]),t._v("。\n此处实际上是将外层作用域内的getName函数修改了。")]),t._v(" "),n("blockquote",[n("p",[t._v("注意：此处若依然没有找到会一直向上查找到window对象，若window对象中也没有getName属性，就在window对象中创建一个getName变量。")])]),t._v(" "),n("p",[t._v("之后Foo函数的返回值是this，而JS的this问题已经有非常多的文章介绍，这里不再多说。\n简单的讲，this的指向是由所在函数的调用方式决定的。而此处的直接调用方式，this指向window对象。\n遂Foo函数返回的是window对象，相当于执行"),n("code",[t._v("window.getName()")]),t._v("，而window中的getName已经被修改为alert(1)，所以最终会输出1\n此处考察了两个知识点，一个是变量作用域问题，一个是this指向问题\n我们可以利用下面代码来回顾下这两个知识点")]),t._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[t._v('var name = "Wscats"; //全局变量\nwindow.name = "Wscats"; //全局变量\nfunction getName() {\n\tname = "Oaoafly"; //去掉var变成了全局变量\n\tvar privateName = "Stacsw";\n\treturn function() {\n\t\tconsole.log(this); //window\n\t\treturn privateName\n\t}\n}\nvar getPrivate = getName("Hello"); //当然传参是局部变量，但函数里面我没有接受这个参数\nconsole.log(name) //Oaoafly\nconsole.log(getPrivate()) //Stacsw\n')])]),t._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[t._v("1")]),n("br"),n("span",{staticClass:"line-number"},[t._v("2")]),n("br"),n("span",{staticClass:"line-number"},[t._v("3")]),n("br"),n("span",{staticClass:"line-number"},[t._v("4")]),n("br"),n("span",{staticClass:"line-number"},[t._v("5")]),n("br"),n("span",{staticClass:"line-number"},[t._v("6")]),n("br"),n("span",{staticClass:"line-number"},[t._v("7")]),n("br"),n("span",{staticClass:"line-number"},[t._v("8")]),n("br"),n("span",{staticClass:"line-number"},[t._v("9")]),n("br"),n("span",{staticClass:"line-number"},[t._v("10")]),n("br"),n("span",{staticClass:"line-number"},[t._v("11")]),n("br"),n("span",{staticClass:"line-number"},[t._v("12")]),n("br"),n("span",{staticClass:"line-number"},[t._v("13")]),n("br")])]),n("p",[t._v("因为JS没有块级作用域，但是函数是能产生一个作用域的，函数内部不同定义值的方法会直接或者间接影响到全局或者局部变量，函数内部的私有变量可以用闭包获取，函数还真的是第一公民呀~\n而关于this，this的指向在函数定义的时候是确定不了的，只有函数执行的时候才能确定this到底指向谁，实际上this的最终指向的是那个调用它的对象\n所以第三问中实际上就是window在调用**Foo()**函数，所以this的指向是window")]),t._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[t._v("window.Foo().getName();\n//->window.getName();\n")])]),t._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[t._v("1")]),n("br"),n("span",{staticClass:"line-number"},[t._v("2")]),n("br")])]),n("h1",{attrs:{id:"第四问"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#第四问"}},[t._v("#")]),t._v(" 第四问")]),t._v(" "),n("p",[t._v("直接调用getName函数，相当于"),n("code",[t._v("window.getName()")]),t._v("，因为这个变量已经被Foo函数执行时修改了，遂结果与第三问相同，为1，也就是说Foo执行后把全局的getName函数给重写了一次，所以结果就是Foo()执行重写的那个getName函数")]),t._v(" "),n("h1",{attrs:{id:"第五问"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#第五问"}},[t._v("#")]),t._v(" 第五问")]),t._v(" "),n("p",[t._v("第五问"),n("code",[t._v("new Foo.getName();")]),t._v("此处考察的是JS的运算符优先级问题，我觉得这是这题灵魂的所在，也是难度比较大的一题\n下面是JS运算符的优先级表格，从高到低排列。可参考"),n("a",{attrs:{href:"https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/Operator_Precedence",target:"_blank",rel:"noopener noreferrer"}},[t._v("MDN运算符优先级"),n("OutboundLink")],1)]),t._v(" "),n("table",[n("thead",[n("tr",[n("th",[t._v("优先级")]),t._v(" "),n("th",[t._v("运算类型")]),t._v(" "),n("th",[t._v("关联性")]),t._v(" "),n("th",[t._v("运算符")])])]),t._v(" "),n("tbody",[n("tr",[n("td",[t._v("19")]),t._v(" "),n("td",[t._v("圆括号")]),t._v(" "),n("td",[t._v("n/a")]),t._v(" "),n("td",[t._v("( … )")])]),t._v(" "),n("tr",[n("td",[t._v("18")]),t._v(" "),n("td",[t._v("成员访问")]),t._v(" "),n("td",[t._v("从左到右")]),t._v(" "),n("td",[t._v("… . …")])]),t._v(" "),n("tr",[n("td"),t._v(" "),n("td",[t._v("需计算的成员访问")]),t._v(" "),n("td",[t._v("从左到右")]),t._v(" "),n("td",[t._v("… [ … ]")])]),t._v(" "),n("tr",[n("td"),t._v(" "),n("td",[t._v("new (带参数列表)")]),t._v(" "),n("td",[t._v("n/a new")]),t._v(" "),n("td",[t._v("… ( … )")])]),t._v(" "),n("tr",[n("td",[t._v("17")]),t._v(" "),n("td",[t._v("函数调用")]),t._v(" "),n("td",[t._v("从左到右")]),t._v(" "),n("td",[t._v("… ( … )")])]),t._v(" "),n("tr",[n("td"),t._v(" "),n("td",[t._v("new (无参数列表)")]),t._v(" "),n("td",[t._v("从右到左")]),t._v(" "),n("td",[t._v("new …")])]),t._v(" "),n("tr",[n("td",[t._v("16")]),t._v(" "),n("td",[t._v("后置递增(运算符在后)")]),t._v(" "),n("td",[t._v("n/a")]),t._v(" "),n("td",[t._v("… ++")])]),t._v(" "),n("tr",[n("td"),t._v(" "),n("td",[t._v("后置递减(运算符在后)")]),t._v(" "),n("td",[t._v("n/a")]),t._v(" "),n("td",[t._v("… --")])]),t._v(" "),n("tr",[n("td",[t._v("15")]),t._v(" "),n("td",[t._v("逻辑非")]),t._v(" "),n("td",[t._v("从右到左")]),t._v(" "),n("td",[t._v("! …")])]),t._v(" "),n("tr",[n("td"),t._v(" "),n("td",[t._v("按位非")]),t._v(" "),n("td",[t._v("从右到左")]),t._v(" "),n("td",[t._v("~ …")])]),t._v(" "),n("tr",[n("td"),t._v(" "),n("td",[t._v("一元加法")]),t._v(" "),n("td",[t._v("从右到左")]),t._v(" "),n("td",[t._v("+ …")])]),t._v(" "),n("tr",[n("td"),t._v(" "),n("td",[t._v("一元减法")]),t._v(" "),n("td",[t._v("从右到左")]),t._v(" "),n("td",[t._v("- …")])]),t._v(" "),n("tr",[n("td"),t._v(" "),n("td",[t._v("前置递增")]),t._v(" "),n("td",[t._v("从右到左")]),t._v(" "),n("td",[t._v("++ …")])]),t._v(" "),n("tr",[n("td"),t._v(" "),n("td",[t._v("前置递减")]),t._v(" "),n("td",[t._v("从右到左")]),t._v(" "),n("td",[t._v("-- …")])]),t._v(" "),n("tr",[n("td"),t._v(" "),n("td",[t._v("typeof")]),t._v(" "),n("td",[t._v("从右到左")]),t._v(" "),n("td",[t._v("typeof …")])]),t._v(" "),n("tr",[n("td"),t._v(" "),n("td",[t._v("void")]),t._v(" "),n("td",[t._v("从右到左")]),t._v(" "),n("td",[t._v("void …")])]),t._v(" "),n("tr",[n("td"),t._v(" "),n("td",[t._v("delete")]),t._v(" "),n("td",[t._v("从右到左")]),t._v(" "),n("td",[t._v("delete …")])]),t._v(" "),n("tr",[n("td",[t._v("14")]),t._v(" "),n("td",[t._v("乘法")]),t._v(" "),n("td",[t._v("从左到右")]),t._v(" "),n("td",[t._v("… * …")])]),t._v(" "),n("tr",[n("td"),t._v(" "),n("td",[t._v("除法")]),t._v(" "),n("td",[t._v("从左到右")]),t._v(" "),n("td",[t._v("… / …")])]),t._v(" "),n("tr",[n("td"),t._v(" "),n("td",[t._v("取模")]),t._v(" "),n("td",[t._v("从左到右")]),t._v(" "),n("td",[t._v("… % …")])]),t._v(" "),n("tr",[n("td",[t._v("13")]),t._v(" "),n("td",[t._v("加法")]),t._v(" "),n("td",[t._v("从左到右")]),t._v(" "),n("td",[t._v("… + …")])]),t._v(" "),n("tr",[n("td"),t._v(" "),n("td",[t._v("减法")]),t._v(" "),n("td",[t._v("从左到右")]),t._v(" "),n("td",[t._v("… - …")])]),t._v(" "),n("tr",[n("td",[t._v("12")]),t._v(" "),n("td",[t._v("按位左移")]),t._v(" "),n("td",[t._v("从左到右")]),t._v(" "),n("td",[t._v("… << …")])]),t._v(" "),n("tr",[n("td"),t._v(" "),n("td",[t._v("按位右移")]),t._v(" "),n("td",[t._v("从左到右")]),t._v(" "),n("td",[t._v("… >> …")])]),t._v(" "),n("tr",[n("td"),t._v(" "),n("td",[t._v("无符号右移")]),t._v(" "),n("td",[t._v("从左到右")]),t._v(" "),n("td",[t._v("… >>> …")])]),t._v(" "),n("tr",[n("td",[t._v("11")]),t._v(" "),n("td",[t._v("小于")]),t._v(" "),n("td",[t._v("从左到右")]),t._v(" "),n("td",[t._v("… < …")])]),t._v(" "),n("tr",[n("td"),t._v(" "),n("td",[t._v("小于等于")]),t._v(" "),n("td",[t._v("从左到右")]),t._v(" "),n("td",[t._v("… <= …")])]),t._v(" "),n("tr",[n("td"),t._v(" "),n("td",[t._v("大于")]),t._v(" "),n("td",[t._v("从左到右")]),t._v(" "),n("td",[t._v("… > …")])]),t._v(" "),n("tr",[n("td"),t._v(" "),n("td",[t._v("大于等于")]),t._v(" "),n("td",[t._v("从左到右")]),t._v(" "),n("td",[t._v("… >= …")])]),t._v(" "),n("tr",[n("td"),t._v(" "),n("td",[t._v("in")]),t._v(" "),n("td",[t._v("从左到右")]),t._v(" "),n("td",[t._v("… in …")])]),t._v(" "),n("tr",[n("td"),t._v(" "),n("td",[t._v("instanceof")]),t._v(" "),n("td",[t._v("从左到右")]),t._v(" "),n("td",[t._v("… instanceof …")])]),t._v(" "),n("tr",[n("td",[t._v("10")]),t._v(" "),n("td",[t._v("等号")]),t._v(" "),n("td",[t._v("从左到右")]),t._v(" "),n("td",[t._v("… == …")])]),t._v(" "),n("tr",[n("td"),t._v(" "),n("td",[t._v("非等号")]),t._v(" "),n("td",[t._v("从左到右")]),t._v(" "),n("td",[t._v("… != …")])]),t._v(" "),n("tr",[n("td"),t._v(" "),n("td",[t._v("全等号")]),t._v(" "),n("td",[t._v("从左到右")]),t._v(" "),n("td",[t._v("… === …")])]),t._v(" "),n("tr",[n("td"),t._v(" "),n("td",[t._v("非全等号")]),t._v(" "),n("td",[t._v("从左到右")]),t._v(" "),n("td",[t._v("… !== …")])]),t._v(" "),n("tr",[n("td",[t._v("9")]),t._v(" "),n("td",[t._v("按位与")]),t._v(" "),n("td",[t._v("从左到右")]),t._v(" "),n("td",[t._v("… & …")])]),t._v(" "),n("tr",[n("td",[t._v("8")]),t._v(" "),n("td",[t._v("按位异或")]),t._v(" "),n("td",[t._v("从左到右")]),t._v(" "),n("td",[t._v("… ^ …")])]),t._v(" "),n("tr",[n("td",[t._v("7")]),t._v(" "),n("td",[t._v("按位或")]),t._v(" "),n("td",[t._v("从左到右")]),t._v(" "),n("td",[t._v("… 按位或 …")])]),t._v(" "),n("tr",[n("td",[t._v("6")]),t._v(" "),n("td",[t._v("逻辑与")]),t._v(" "),n("td",[t._v("从左到右")]),t._v(" "),n("td",[t._v("… && …")])]),t._v(" "),n("tr",[n("td",[t._v("5")]),t._v(" "),n("td",[t._v("逻辑或")]),t._v(" "),n("td",[t._v("从左到右")]),t._v(" "),n("td",[t._v("… 逻辑或 …")])]),t._v(" "),n("tr",[n("td",[t._v("4")]),t._v(" "),n("td",[t._v("条件运算符")]),t._v(" "),n("td",[t._v("从右到左")]),t._v(" "),n("td",[t._v("… ? … : …")])]),t._v(" "),n("tr",[n("td",[t._v("3")]),t._v(" "),n("td",[t._v("赋值")]),t._v(" "),n("td",[t._v("从右到左")]),t._v(" "),n("td",[t._v("… = …")])]),t._v(" "),n("tr",[n("td"),t._v(" "),n("td"),t._v(" "),n("td"),t._v(" "),n("td",[t._v("… += …")])]),t._v(" "),n("tr",[n("td"),t._v(" "),n("td"),t._v(" "),n("td"),t._v(" "),n("td",[t._v("… -= …")])]),t._v(" "),n("tr",[n("td"),t._v(" "),n("td"),t._v(" "),n("td"),t._v(" "),n("td",[t._v("… *= …")])]),t._v(" "),n("tr",[n("td"),t._v(" "),n("td"),t._v(" "),n("td"),t._v(" "),n("td",[t._v("… /= …")])]),t._v(" "),n("tr",[n("td"),t._v(" "),n("td"),t._v(" "),n("td"),t._v(" "),n("td",[t._v("… %= …")])]),t._v(" "),n("tr",[n("td"),t._v(" "),n("td"),t._v(" "),n("td"),t._v(" "),n("td",[t._v("… <<= …")])]),t._v(" "),n("tr",[n("td"),t._v(" "),n("td"),t._v(" "),n("td"),t._v(" "),n("td",[t._v("… >>= …")])]),t._v(" "),n("tr",[n("td"),t._v(" "),n("td"),t._v(" "),n("td"),t._v(" "),n("td",[t._v("… >>>= …")])]),t._v(" "),n("tr",[n("td"),t._v(" "),n("td"),t._v(" "),n("td"),t._v(" "),n("td",[t._v("… &= …")])]),t._v(" "),n("tr",[n("td"),t._v(" "),n("td"),t._v(" "),n("td"),t._v(" "),n("td",[t._v("… ^= …")])]),t._v(" "),n("tr",[n("td"),t._v(" "),n("td"),t._v(" "),n("td"),t._v(" "),n("td",[t._v("… 或= …")])]),t._v(" "),n("tr",[n("td",[t._v("2")]),t._v(" "),n("td",[t._v("yield")]),t._v(" "),n("td",[t._v("从右到左")]),t._v(" "),n("td",[t._v("yield …")])]),t._v(" "),n("tr",[n("td"),t._v(" "),n("td",[t._v("yield*")]),t._v(" "),n("td",[t._v("从右到左")]),t._v(" "),n("td",[t._v("yield* …")])]),t._v(" "),n("tr",[n("td",[t._v("1")]),t._v(" "),n("td",[t._v("展开运算符")]),t._v(" "),n("td",[t._v("n/a")]),t._v(" "),n("td",[t._v("... …")])]),t._v(" "),n("tr",[n("td",[t._v("0")]),t._v(" "),n("td",[t._v("逗号")]),t._v(" "),n("td",[t._v("从左到右")]),t._v(" "),n("td",[t._v("… , …")])])])]),t._v(" "),n("p",[t._v("这题首先看优先级的第18和第17都出现关于new的优先级，new (带参数列表)比new (无参数列表)高比函数调用高，跟成员访问同级")]),t._v(" "),n("p",[n("code",[t._v("new Foo.getName();")]),t._v("的优先级是这样的")]),t._v(" "),n("p",[t._v("相当于是:")]),t._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[t._v("new (Foo.getName)();\n")])]),t._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[t._v("1")]),n("br")])]),n("ul",[n("li",[t._v("点的优先级(18)比new无参数列表(17)优先级高")]),t._v(" "),n("li",[t._v("当点运算完后又因为有个括号"),n("code",[t._v("()")]),t._v("，此时就是变成new有参数列表(18)，所以直接执行new，当然也可能有朋友会有疑问为什么遇到()不函数调用再new呢，那是因为函数调用(17)比new有参数列表(18)优先级低")])]),t._v(" "),n("blockquote",[n("p",[t._v(".成员访问(18)->new有参数列表(18)")])]),t._v(" "),n("p",[t._v("所以这里实际上将getName函数作为了构造函数来执行，遂弹出2。")]),t._v(" "),n("h1",{attrs:{id:"第六问"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#第六问"}},[t._v("#")]),t._v(" 第六问")]),t._v(" "),n("p",[t._v("这一题比上一题的唯一区别就是在Foo那里多出了一个括号，这个有括号跟没括号我们在第五问的时候也看出来优先级是有区别的")]),t._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[t._v("(new Foo()).getName()\n")])]),t._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[t._v("1")]),n("br")])]),n("p",[t._v("那这里又是怎么判断的呢？首先new有参数列表(18)跟点的优先级(18)是同级，同级的话按照从左向右的执行顺序，所以先执行new有参数列表(18)再执行点的优先级(18)，最后再函数调用(17)")]),t._v(" "),n("blockquote",[n("p",[t._v("new有参数列表(18)->.成员访问(18)->()函数调用(17)")])]),t._v(" "),n("p",[t._v("这里还有一个小知识点，Foo作为构造函数有返回值，所以这里需要说明下JS中的构造函数返回值问题。")]),t._v(" "),n("h3",{attrs:{id:"构造函数的返回值"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#构造函数的返回值"}},[t._v("#")]),t._v(" 构造函数的返回值")]),t._v(" "),n("p",[t._v("在传统语言中，构造函数不应该有返回值，实际执行的返回值就是此构造函数的实例化对象。\n而在JS中构造函数可以有返回值也可以没有。")]),t._v(" "),n("ol",[n("li",[t._v("没有返回值则按照其他语言一样返回实例化对象。")])]),t._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[t._v("function Foo(name) {\n\tthis.name = name\n}\nconsole.log(new Foo('wscats'))\n")])]),t._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[t._v("1")]),n("br"),n("span",{staticClass:"line-number"},[t._v("2")]),n("br"),n("span",{staticClass:"line-number"},[t._v("3")]),n("br"),n("span",{staticClass:"line-number"},[t._v("4")]),n("br")])]),n("p",[n("a",{attrs:{href:"https://cloud.githubusercontent.com/assets/17243165/24442903/cf6d6bf4-1492-11e7-822e-85ca44b01364.png",target:"_blank",rel:"noopener noreferrer"}},[n("img",{attrs:{src:"https://cloud.githubusercontent.com/assets/17243165/24442903/cf6d6bf4-1492-11e7-822e-85ca44b01364.png",alt:"image"}}),n("OutboundLink")],1)]),t._v(" "),n("ol",[n("li",[t._v("若有返回值则检查其返回值是否为引用类型。如果是非引用类型，如基本类型（String,Number,Boolean,Null,Undefined）则与无返回值相同，实际返回其实例化对象。")])]),t._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[t._v("function Foo(name) {\n\tthis.name = name\n\treturn 520\n}\nconsole.log(new Foo('wscats'))\n")])]),t._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[t._v("1")]),n("br"),n("span",{staticClass:"line-number"},[t._v("2")]),n("br"),n("span",{staticClass:"line-number"},[t._v("3")]),n("br"),n("span",{staticClass:"line-number"},[t._v("4")]),n("br"),n("span",{staticClass:"line-number"},[t._v("5")]),n("br")])]),n("p",[n("a",{attrs:{href:"https://cloud.githubusercontent.com/assets/17243165/24442973/38c304c4-1493-11e7-935f-b6c60e1bcb8d.png",target:"_blank",rel:"noopener noreferrer"}},[n("img",{attrs:{src:"https://cloud.githubusercontent.com/assets/17243165/24442973/38c304c4-1493-11e7-935f-b6c60e1bcb8d.png",alt:"image"}}),n("OutboundLink")],1)]),t._v(" "),n("ol",[n("li",[t._v("若返回值是引用类型，则实际返回值为这个引用类型。")])]),t._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[t._v("function Foo(name) {\n\tthis.name = name\n\treturn {\n\t\tage: 16\n\t}\n}\nconsole.log(new Foo('wscats'))\n")])]),t._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[t._v("1")]),n("br"),n("span",{staticClass:"line-number"},[t._v("2")]),n("br"),n("span",{staticClass:"line-number"},[t._v("3")]),n("br"),n("span",{staticClass:"line-number"},[t._v("4")]),n("br"),n("span",{staticClass:"line-number"},[t._v("5")]),n("br"),n("span",{staticClass:"line-number"},[t._v("6")]),n("br"),n("span",{staticClass:"line-number"},[t._v("7")]),n("br")])]),n("p",[n("a",{attrs:{href:"https://cloud.githubusercontent.com/assets/17243165/24443023/843a4cc8-1493-11e7-9a57-c91835622d7b.png",target:"_blank",rel:"noopener noreferrer"}},[n("img",{attrs:{src:"https://cloud.githubusercontent.com/assets/17243165/24443023/843a4cc8-1493-11e7-9a57-c91835622d7b.png",alt:"image"}}),n("OutboundLink")],1),t._v("\n原题中，由于返回的是this，而this在构造函数中本来就代表当前实例化对象，最终Foo函数返回实例化对象。\n之后调用实例化对象的getName函数，因为在Foo构造函数中没有为实例化对象添加任何属性，当前对象的原型对象(prototype)中寻找getName函数。\n当然这里再拓展个题外话，如果构造函数和原型链都有相同的方法，如下面的代码，那么默认会拿构造函数的公有方法而不是原型链，这个知识点在原题中没有表现出来，后面改进版我已经加上。")]),t._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[t._v("function Foo(name) {\n\tthis.name = name\n\tthis.getName = function() {\n\t\treturn this.name\n\t}\n}\nFoo.prototype.name = 'Oaoafly';\nFoo.prototype.getName = function() {\n\treturn 'Oaoafly'\n}\nconsole.log((new Foo('Wscats')).name) //Wscats\nconsole.log((new Foo('Wscats')).getName()) //Wscats\n")])]),t._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[t._v("1")]),n("br"),n("span",{staticClass:"line-number"},[t._v("2")]),n("br"),n("span",{staticClass:"line-number"},[t._v("3")]),n("br"),n("span",{staticClass:"line-number"},[t._v("4")]),n("br"),n("span",{staticClass:"line-number"},[t._v("5")]),n("br"),n("span",{staticClass:"line-number"},[t._v("6")]),n("br"),n("span",{staticClass:"line-number"},[t._v("7")]),n("br"),n("span",{staticClass:"line-number"},[t._v("8")]),n("br"),n("span",{staticClass:"line-number"},[t._v("9")]),n("br"),n("span",{staticClass:"line-number"},[t._v("10")]),n("br"),n("span",{staticClass:"line-number"},[t._v("11")]),n("br"),n("span",{staticClass:"line-number"},[t._v("12")]),n("br")])]),n("h1",{attrs:{id:"第七问"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#第七问"}},[t._v("#")]),t._v(" 第七问")]),t._v(" "),n("p",[n("code",[t._v("new new Foo().getName();")]),t._v("同样是运算符优先级问题。做到这一题其实我已经觉得答案没那么重要了，关键只是考察面试者是否真的知道面试官在考察我们什么。\n最终实际执行为:")]),t._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[t._v("new ((new Foo()).getName)();\n")])]),t._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[t._v("1")]),n("br")])]),n("blockquote",[n("p",[t._v("new有参数列表(18)->new有参数列表(18)")])]),t._v(" "),n("p",[t._v("先初始化Foo的实例化对象，然后将其原型上的getName函数作为构造函数再次new，所以最终结果为3")]),t._v(" "),n("h1",{attrs:{id:"答案"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#答案"}},[t._v("#")]),t._v(" 答案")]),t._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[t._v("function Foo() {\n    getName = function () { alert (1); };\n    return this;\n}\nFoo.getName = function () { alert (2);};\nFoo.prototype.getName = function () { alert (3);};\nvar getName = function () { alert (4);};\nfunction getName() { alert (5);}\n\n//答案：\nFoo.getName();//2\ngetName();//4\nFoo().getName();//1\ngetName();//1\nnew Foo.getName();//2\nnew Foo().getName();//3\nnew new Foo().getName();//3\n")])]),t._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[t._v("1")]),n("br"),n("span",{staticClass:"line-number"},[t._v("2")]),n("br"),n("span",{staticClass:"line-number"},[t._v("3")]),n("br"),n("span",{staticClass:"line-number"},[t._v("4")]),n("br"),n("span",{staticClass:"line-number"},[t._v("5")]),n("br"),n("span",{staticClass:"line-number"},[t._v("6")]),n("br"),n("span",{staticClass:"line-number"},[t._v("7")]),n("br"),n("span",{staticClass:"line-number"},[t._v("8")]),n("br"),n("span",{staticClass:"line-number"},[t._v("9")]),n("br"),n("span",{staticClass:"line-number"},[t._v("10")]),n("br"),n("span",{staticClass:"line-number"},[t._v("11")]),n("br"),n("span",{staticClass:"line-number"},[t._v("12")]),n("br"),n("span",{staticClass:"line-number"},[t._v("13")]),n("br"),n("span",{staticClass:"line-number"},[t._v("14")]),n("br"),n("span",{staticClass:"line-number"},[t._v("15")]),n("br"),n("span",{staticClass:"line-number"},[t._v("16")]),n("br"),n("span",{staticClass:"line-number"},[t._v("17")]),n("br")])]),n("h1",{attrs:{id:"后续"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#后续"}},[t._v("#")]),t._v(" 后续")]),t._v(" "),n("p",[t._v("后续我把这题的难度再稍微加大一点点(附上答案)，在Foo函数里面加多一个公有方法getName，对于下面这题如果用在面试题上那通过率可能就更低了，因为难度又大了一点，又多了两个坑，但是明白了这题的原理就等同于明白了上面所有的知识点了")]),t._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[t._v("function Foo() {\n\tthis.getName = function() {\n\t\tconsole.log(3);\n\t\treturn {\n\t\t\tgetName: getName //这个就是第六问中涉及的构造函数的返回值问题\n\t\t}\n\t}; //这个就是第六问中涉及到的，JS构造函数公有方法和原型链方法的优先级\n\tgetName = function() {\n\t\tconsole.log(1);\n\t};\n\treturn this\n}\nFoo.getName = function() {\n\tconsole.log(2);\n};\nFoo.prototype.getName = function() {\n\tconsole.log(6);\n};\nvar getName = function() {\n\tconsole.log(4);\n};\n\nfunction getName() {\n\tconsole.log(5);\n} //答案：\nFoo.getName(); //2\ngetName(); //4\nconsole.log(Foo())\nFoo().getName(); //1\ngetName(); //1\nnew Foo.getName(); //2\nnew Foo().getName(); //3\n//多了一问\nnew Foo().getName().getName(); //3 1\nnew new Foo().getName(); //3             \n")])]),t._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[t._v("1")]),n("br"),n("span",{staticClass:"line-number"},[t._v("2")]),n("br"),n("span",{staticClass:"line-number"},[t._v("3")]),n("br"),n("span",{staticClass:"line-number"},[t._v("4")]),n("br"),n("span",{staticClass:"line-number"},[t._v("5")]),n("br"),n("span",{staticClass:"line-number"},[t._v("6")]),n("br"),n("span",{staticClass:"line-number"},[t._v("7")]),n("br"),n("span",{staticClass:"line-number"},[t._v("8")]),n("br"),n("span",{staticClass:"line-number"},[t._v("9")]),n("br"),n("span",{staticClass:"line-number"},[t._v("10")]),n("br"),n("span",{staticClass:"line-number"},[t._v("11")]),n("br"),n("span",{staticClass:"line-number"},[t._v("12")]),n("br"),n("span",{staticClass:"line-number"},[t._v("13")]),n("br"),n("span",{staticClass:"line-number"},[t._v("14")]),n("br"),n("span",{staticClass:"line-number"},[t._v("15")]),n("br"),n("span",{staticClass:"line-number"},[t._v("16")]),n("br"),n("span",{staticClass:"line-number"},[t._v("17")]),n("br"),n("span",{staticClass:"line-number"},[t._v("18")]),n("br"),n("span",{staticClass:"line-number"},[t._v("19")]),n("br"),n("span",{staticClass:"line-number"},[t._v("20")]),n("br"),n("span",{staticClass:"line-number"},[t._v("21")]),n("br"),n("span",{staticClass:"line-number"},[t._v("22")]),n("br"),n("span",{staticClass:"line-number"},[t._v("23")]),n("br"),n("span",{staticClass:"line-number"},[t._v("24")]),n("br"),n("span",{staticClass:"line-number"},[t._v("25")]),n("br"),n("span",{staticClass:"line-number"},[t._v("26")]),n("br"),n("span",{staticClass:"line-number"},[t._v("27")]),n("br"),n("span",{staticClass:"line-number"},[t._v("28")]),n("br"),n("span",{staticClass:"line-number"},[t._v("29")]),n("br"),n("span",{staticClass:"line-number"},[t._v("30")]),n("br"),n("span",{staticClass:"line-number"},[t._v("31")]),n("br"),n("span",{staticClass:"line-number"},[t._v("32")]),n("br"),n("span",{staticClass:"line-number"},[t._v("33")]),n("br"),n("span",{staticClass:"line-number"},[t._v("34")]),n("br"),n("span",{staticClass:"line-number"},[t._v("35")]),n("br")])]),n("p",[t._v("最后，其实我是不建议把这些题作为考察面试者的唯一评判，但是作为一名合格的前端工程师我们不应该因为浮躁忽略了我们的一些最基本的基础知识，当然我也祝愿所有面试者找到一份理想的工作，祝愿所有面试官找到心中那匹千里马～")]),t._v(" "),n("ul",[n("li",[n("a",{attrs:{href:"https://github.com/Wscats/Good-text-Share/issues/85",target:"_blank",rel:"noopener noreferrer"}},[t._v("Github"),n("OutboundLink")],1)]),t._v(" "),n("li",[n("a",{attrs:{href:"https://www.talkingcoder.com/article/6421659505561698459",target:"_blank",rel:"noopener noreferrer"}},[t._v("Talkingcoder"),n("OutboundLink")],1)]),t._v(" "),n("li",[n("a",{attrs:{href:"http://www.codeceo.com/article/one-javascript-interview.html",target:"_blank",rel:"noopener noreferrer"}},[t._v("码农网"),n("OutboundLink")],1)]),t._v(" "),n("li",[n("a",{attrs:{href:"http://www.cnblogs.com/xxcanghai/p/5189353.html",target:"_blank",rel:"noopener noreferrer"}},[t._v("cnblogs"),n("OutboundLink")],1)])])])}),[],!1,null,null,null);s.default=e.exports}}]);