Giter VIP home page Giter VIP logo

Comments (8)

Panda-HJN avatar Panda-HJN commented on July 21, 2024 11

逗号运算符,的作用是对每个表达式求值,并返回最后一个表达式的值.

(a,b)//b

所以 (object2, object1, object3) + object1 +-- object1;.
可以简化为object3 + object1 +--object1
object3 + object1 + (-- object1)

从右向左计算.第一个运算符是--自减运算符.
需要讲其转换成数值.

如果运算子是对象,先执行该对象的valueOf方法.

式子变为
30+10+9

结果是49

from frontend-interview.

Jemair avatar Jemair commented on July 21, 2024

(object2, object1, object3) + object1 +-- object1;
=> obj3 + obj1 + --obj1后面就简单了
这题可以改成
(obj2, obj1, obj3) + --obj1 + obj1
比较有意思

from frontend-interview.

Panda-HJN avatar Panda-HJN commented on July 21, 2024

这题改成

var result = (object3, object2, object1) + object1 +-- object1 + object1 ; 

还能再绕绕弯.

我已经绕进去了,为啥结果是 38,求解惑

@Jemair

from frontend-interview.

xcatliu avatar xcatliu commented on July 21, 2024

@Panda-HJN

var result = (object2, object1, object3) + -- object1 + object1;
// 38

因为 --object1 先执行,得到 9 同时 object1 的值也变成了 9,具体过程如下:

var result = (object2, object1, object3) + -- object1 + object1;

// 等价于

var a = (object2, object1, object3) + 0;
var b = object1 - 1;
object1 = object1 - 1;
var result = a + b + object1;

from frontend-interview.

Panda-HJN avatar Panda-HJN commented on July 21, 2024

@xcatliu 非常感谢您的帮助.
一楼的题目我已做出来了,我的疑惑是
当原题的result 改为

var result = (object3, object2, object1) + object1 +-- object1 + object1 ; 
//38

对于这个结果我没想明白.
我的思路是
圆括号有最高优先级.
所以

result = 10  +object1 +-- object1 +object1;

剩下的式子中--拥有最高的优先级所以
计算--object1 得到9.
同时object1的值也变为了9

result = 10 + 9 +9 +9  
result = 37

这与答案 38 不符.
请问我哪里出错了呢?

经过几次实验我猜测,似乎只有 --后面的 object1 减一了 ,--前面的似乎还是10

from frontend-interview.

xcatliu avatar xcatliu commented on July 21, 2024

@Panda-HJN

-- 并没有拥有最高优先级,测试如下:

var a = {
  valueOf: function() {
    console.log('a.valueOf');
    return 10;
  }
};
var b = {
  valueOf: function() {
    console.log('b.valueOf');
    return 20;
  }
};
10 + b + --a

// b.valueOf
// a.valueOf
// 39

from frontend-interview.

hileix avatar hileix commented on July 21, 2024

@Panda-HJN

var result = (object3, object2, object1) + object1 +-- object1 + object1
// 38

我觉得应该是这样的:
1.首先,“+” 运算符和 “--” 运算符的优先级是一样的:
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/Operator_Precedence
这里查看运算符的优先等级。
整个式子会按照从左往右的运算顺序进行运算
2.就相当于:

(1) var result = (object3, object2, object1) + object1 +-- object1 + object1 
(2) var result = object1 + object1 + --object1 + object 
(3) var result = 20 + --object1 + object 
(4) var result = 20 + 9 + 9
(5) var result = 38

3.也就是只有运算到 --object1 的时候,object1 的值才会变为 9,所以最后那个 object1 的值就是9

from frontend-interview.

Indomi avatar Indomi commented on July 21, 2024

from frontend-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.