GithubHelp home page GithubHelp logo

Comments (5)

Genzhen avatar Genzhen commented on May 1, 2024 6

答案
12 NaN

解析
注意,add是普通函数,而reduce是箭头函数。对于箭头函数,this关键字指向是它所在上下文(定义时的位置)的环境,与普通函数不同! 这意味着当我们调用reduce时,它不是指向num对象,而是指其定义时的环境(window)。没有值a属性,返回undefined

from fe-interview.

userkang avatar userkang commented on May 1, 2024 4
reduce: () => this.a -2;

这行代码最后一行的 ; 多余了

from fe-interview.

a123456789B avatar a123456789B commented on May 1, 2024

答案
12 NaN
reduce: () => this.a -2; 箭头函数中的this 指向外层正常函数,因为外层没有函数所以指向window ,window中没有a ,返回undefind 加上number 所以为NaN

from fe-interview.

wind8866 avatar wind8866 commented on May 1, 2024

我本来以为严格模式下 this 不能指向全局对象,没想到箭头函数是可以的。

function f(){
  console.log(this);
}
f();// undefined
const f2 = () => {
  console.log(this);
}
f2();// window

from fe-interview.

jasonjiang123 avatar jasonjiang123 commented on May 1, 2024

测试为-1

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.