GithubHelp home page GithubHelp logo

Comments (9)

dinjufen avatar dinjufen commented on May 1, 2024 24

说实话,感觉考这些真的很无聊。。学js学得都陷在语法的泥潭里了,像极了大学时期末考试C语言i++、++i之类的。。

from fe-interview.

chenzesam avatar chenzesam commented on May 1, 2024 22

加了默认值,则转为严格模式(其实也可以使用 "use strict" 转),这时候参数(a、b、c)与 arguments 没有绑定关系,所以修改 arguments 不会影响到参数(a、b、c)的值,修改参数(a、b、c)也不会影响到 arguments。
不加默认值,则为非严格模式,结果和上面的相反。

from fe-interview.

Genzhen avatar Genzhen commented on May 1, 2024 15

c会变成10啊,解析的什么东东

@zhengcaifeng

是arguments 中 c 的值,function a(a, b, c = 3) 这里的c,因为 a 函数加了默认值,所以就按 ES 的方式解析,函数中的参数就不会变了

function side(arr) {
  arr[0] = arr[2];
}
function a(a, b, c = 3) {
  c = 10;
  console.log(arguments);
  side(arguments);  // 这里 a,c的值不管怎么改变都是不会改变的
  return a + b + c;
}
a(1, 1, 1);  //12

但是,如果是

function side(arr) {
  arr[0] = arr[2];
}
function a(a, b, c) {
  c = 10;
  console.log(arguments);
  side(arguments);  // 这里 a,c的值不管怎么改变都是不会改变的
  return a + b + c;
}
a(1, 1, 1);  // 21

from fe-interview.

1uckyneo avatar 1uckyneo commented on May 1, 2024 6

说实话,感觉考这些真的很无聊。。学js学得都陷在语法的泥潭里了,像极了大学时期末考试C语言i++、++i之类的。。

JavaScript 因为一些历史原因,它的设计很糟糕

但是现在制定标准委员会确实在创造新的特性和语法,来弥补过去糟糕设计带来的糟糕的使用

也鼓励大家使用新特性和语法去替代旧的糟糕的特性和语法

有了 good parts 就用新东西就好了,老的糟糕设计要保留是为了兼容历史包袱,大家也不该继续用了

但是有些考题还考这些「历史包袱」就很没有意义的,一点价值都没有,挺不能理解的

这些东西不该成为,常驻于我们 JavaScript 编程思维模型中的常客,因为这些东西挺扰乱思路的。如果真的在开发中遇到这种,超低频率出现的问题,也应该当时找资料去解决它

from fe-interview.

HowieWH avatar HowieWH commented on May 1, 2024 1

c会变成10啊,解析的什么东东

@zhengcaifeng

是arguments 中 c 的值,function a(a, b, c = 3) 这里的c,因为 a 函数加了默认值,所以就按 ES 的方式解析,函数中的参数就不会变了

function side(arr) {
  arr[0] = arr[2];
}
function a(a, b, c = 3) {
  c = 10;
  console.log(arguments);
  side(arguments);  // 这里 a,c的值不管怎么改变都是不会改变的
  return a + b + c;
}
a(1, 1, 1);  //12

但是,如果是

function side(arr) {
  arr[0] = arr[2];
}
function a(a, b, c) {
  c = 10;
  console.log(arguments);
  side(arguments);  // 这里 a,c的值不管怎么改变都是不会改变的
  return a + b + c;
}
a(1, 1, 1);  // 21

在es5的非严格模式下,因为数组索引对应了数据属性,所以会互相联动的,也就是c和arguments[2]绑定了;而在严格模式下则是简单拷贝。es6是简单拷贝,所以c的值不会因为argument的变化而改变

from fe-interview.

Genzhen avatar Genzhen commented on May 1, 2024

答案
12

解析
arguments 中 c 的值还是 1 不会变成 10,
因为 a 函数加了默认值,就按 ES 的方式解析,ES6 是有块级作用域的,所以 c 的值是不会改变的

from fe-interview.

zhengcaifeng avatar zhengcaifeng commented on May 1, 2024

c会变成10啊,解析的什么东东

from fe-interview.

zsasjy avatar zsasjy commented on May 1, 2024

当设置了解构赋值、默认值、剩余运算符 arguments的值不会变

from fe-interview.

woshinibaba-hue avatar woshinibaba-hue commented on May 1, 2024

c会变成10啊,解析的什么东东

首先,c会有一个默认值3,调用a函数传递了三个1,此时a函数三个变量都为1,此时执行a函数第一行代码,将形参c赋值为10,调用side将当前函数args数组传递过去,改变该数组的第一项,并不会对args会有影响,所以c还是10,所以最终是12

from fe-interview.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.