不是函数错误 - JavaScript es6类结构

时间:2017-09-13 16:52:25

标签: javascript ecmascript-6

有问题 - 不是功能错误,我的代码在这里是:

import $ from 'jquery';
import gsap from 'greensock';

class IntroAnimations{
 constructor(){
    this.ctaEnter         = $('#cta-enter');
    this.loadContent      = $('.center-h-v');
    this.tlLoading        = new TimelineLite();
    this.animationTime    = .5;

    this.ctaEnter.click(function(){
        this.removeLoadContent(); //<- error is here
    })
 }
removeLoadContent(){
    TweenLite.to(this.loadContent, this.animationTime, {autoAlpha: 0})
 }
}

export default IntroAnimations;

错误是我调用this.removeLoadContent(); 有人可以帮我解释为什么会导致错误吗?

谢谢。

2 个答案:

答案 0 :(得分:1)

<div class="projects">
  <div class="desktop-image">
    <a class="desktop-image" href="https://www.stellamccartney.com/gb" target="_blank">
      <img src="https://www.thesun.co.uk/wp-content/uploads/2017/08/nintchdbpict000342472436.jpg?strip=all&w=960&quality=100">
      <p class="img_description">
        An event directory that I<br> founded. Development, design &<br> content by me.
      </p>
    </a>
  </div>
</div>

答案 1 :(得分:0)

这是因为“this”有不同的背景。

使用胖箭头编写你的函数,它应该可以工作。

removeLoadContent = ()=>{
    TweenLite.to(this.loadContent, this.animationTime, {autoAlpha: 0})
 }