Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

部分问题纠正 #128

Open
yeild opened this issue Jan 11, 2019 · 0 comments
Open

部分问题纠正 #128

yeild opened this issue Jan 11, 2019 · 0 comments

Comments

@yeild
Copy link

yeild commented Jan 11, 2019

  • 说几条写JavaScript的基本规范?
    7.For循环必须使用大括号
    8.If语句必须使用大括号

单行的for和if可以不使用大括号,使代码更简洁(这个其实看团队代码风格)

  for (let k in obj) arr.push(obj[k])
  ...
  if (expression) foo()
  else bar()
  • 什么是闭包(closure),为什么要用它?

关于闭包的文章几乎都是抄来抄去,人云亦云,然而自己根本就不理解。这里的回答也是, 强调“函数里嵌套函数”,或者“有权访问”另一个函数的作用域,可以说并没有理解到闭包的本质。

可以说,一切函数都是闭包, 看下面这个例子:

const count = 0

function getCount () {
  console.log(count)
}

function run () {
  const count  = 1
  getCount()
}

run()

执行run()的时候,打印的值是0,正是因为getCount函数是"闭包",包含了函数本身和其外部变量(或者说作用域链)。
详细内容可以看我的博客
或者MDN上的内容(闭包|MDN

  • JSON 的了解?
    var obj = str.parseJSON()
    var last = obj.toJSONString()

str并没有parseJSON方法,obj也没有toJSONString方法。

  • .call() 和 .apply() 的区别?
    例子中用 add 来替换 sub,add.call(sub,3,1) == add(3,1) ,所以运行结果为:alert(4);

这个例子并没有指出call/apply的关键,即修改this指向

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant