Giter VIP home page Giter VIP logo

Comments (4)

stuodte avatar stuodte commented on August 23, 2024 13

(object2 > object1 +-- object1) + true)

object1和object2 都是对象,所以运算的时候会调用valueOf方法,所以得到:
(2 > 1 + --1) + true)

--1为前递减所以为0,2 > 1为true 得到:
(true + 0) + true)

布尔值在运算时会自动转为数字 true -> 1 false -> 0
得到:
(1+ 0) + 1)

所以结果是2

from frontend-interview.

Jemair avatar Jemair commented on August 23, 2024

(object2 > object1 +-- object1) + true);
先算大于号
obj2 > obj1 == 2 > 1 == true; =>
true + -- obj1 == true + --1 == true + 0 == 1 + 0 == 1; =>
1 + true == 1 + 1 == 2
故输出2

from frontend-interview.

WangXiaoyugg avatar WangXiaoyugg commented on August 23, 2024

参考MDN的运算符优先级
() 19级 -- 15级 + 13级 >11 级
(object2 > object1 +-- object1) + true)
object1和object2 都是对象,所以运算的时候会调用valueOf方法,所以得到:
(2 > 1 + --1) + true) => ((2>1 + 0)+true) => ((2>1)+true) >=(true+true) >=(1+1) >=2

from frontend-interview.

hungeroxc avatar hungeroxc commented on August 23, 2024

结果输出是2;


原因:
1.对对象进行的计算会先调用对象的valueOf方法,无结果再调用toString方法,这里的两个对象都有valueOf方法,所以调用成功,所以式子(object2 > object1 +-- object1),转化为
(2 > 1 + --1) + true

2.在2 > 1 + --1中,自减符号优先级高,先计算--1,得出结果0,2 > 1结果为true,最后式子变成
(true + 0) + true

3.因为加法运算符,true转化为1,所以结果为2

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.