"this" 在 module.exports 中的范围

scope of "this" in module.exports

我正在使用 module.exports 将 React.js 组件转换为 Common.js 模块,并且在组件元素的上下文中从以下之一访问 "this" 时遇到问题这是方法。

下面是整个组件。我在出现问题的行上方放置了一条评论。一开始我确实尝试了一个不太详细的例子,但我认为这不足以解释这个问题。

    var React = require('react');
    var GSAP  = require('gsap');

    var Psychedelicon = React.createClass({  

        cycleColors: function() {
            var touchPlatforms = ['iPhone', 'iPad', 'iPod', 'Android', 'Linux armv7l', 'WinCE'];
                       isTouch  = false;
                       iDevice  = false;
                       isDroid  = false;
                       plat = navigator.platform;

            if(plat === 'iPhone' || plat === 'iPad' || plat === 'iPod') {
                isTouch  = true;
                iDevice  = true;
            }
            else if (plat === 'Linux armv7l' || plat === 'Android') {
                isTouch  = true;
                isDroid  = true;
            }
            else {
                for (var i = 0; i < touchPlatforms.length; i++) {
                    if (plat === touchPlatforms[i]) {
                        isTouch = true;
                        break;
                    }
                    else {
                        isTouch = false;
                    }
                }
            }

            var isIE = false
                if (navigator.userAgent.toLowerCase().indexOf('msie') > -1 || navigator.userAgent.toLowerCase().indexOf('trident') > -1) {
                isIE = true
            }

            var isFF = false
                if (navigator.userAgent.toLowerCase().indexOf('firefox') != -1) {
                isFF = true
            }

            if(!isTouch) {
                var ColorSwirl = function(colorSet,defaultColor,time) {
                    var storedResult;
                    var randomColor = function(theArray) {
                        var result = theArray[Math.floor(Math.random() * (theArray.length))];
                        if(result === storedResult){
                            return(defaultColor)                        
                        }
                        else {
                            storedResult = result;
                            return(result);
                        }
                    }
                    var theLuckyColors = {top:randomColor(colorSet),bottom:randomColor(colorSet)};
                    var swirl = function(){
    //!!!!On this line the problem occurs onUpdateParams must reference the element accepting the execution event (onMouseEneter)
                        TweenLite.to(theLuckyColors, time, {colorProps:{top:randomColor(colorSet), bottom:randomColor(colorSet)}, onUpdate:colorize, onUpdateParams:[this],onComplete:swirl});
                    }
 gradients
                    var colorize = function(el) {
                        if(isIE) {
                            TweenLite.set(el, {
                                backgroundImage:'-ms-radial-gradient(center,circle cover,' + theLuckyColors.top + ' 0%, ' + theLuckyColors.bottom + ' 100%)'
                            });
                        }
                        else if(isFF) {
                            TweenLite.set(el, {
                                backgroundImage:'-moz-radial-gradient(center,circle cover,' + theLuckyColors.top + ' 0%, ' + theLuckyColors.bottom + ' 100%)'
                            });
                        }
                        else {
                            TweenLite.set(el, {
                                backgroundImage:'radial-gradient(circle,' + theLuckyColors.top + ', ' + theLuckyColors.bottom + ')',
                                backgroundImage:'-webkit-radial-gradient(circle,' + theLuckyColors.top + ', ' + theLuckyColors.bottom + ')'
                            });
                        }
                    }
                    swirl();
                }
                ColorSwirl(['red','green','#4B0082','#9F00FF','yellow','orange'],'blue',.15);
            }

        },
        stopTheCycle: function() {

        },

        render: function() {
            return (
                <a className="psychedelicon" href={this.props.href} target={this.props.target} onMouseEnter={this.cycleColors} onMouseLeave={this.stopTheCycle}>
                    <i className={"fa fa-" + this.props.icon}></i>
                </a>
            )
        }

    });
    module.exports = Psychedelicon;

到目前为止,我已尝试将 "this" 绑定到接收事件的元素:

onMouseEnter={this.cycleColors.bind(this)}

我得到:`'You are binding a component method to the component. React does this for you automatically in a high-performance way, so you can safely remove this call.'

我也试过:

onMouseEnter={this.cycleColors.call(Psychedelicon)}

和 onMouseEnter={this.cycleColors.bind(迷幻)}

这两个都没有产生错误但没有工作

我知道这个函数在其他方面是有效的,因为当我改变

onUpdateParams:[this]

onUpdateParams:['.psychedelicon']

该组件会产生所需的行为,除了它会同时影响所有组件(我需要避免这种情况,因此需要使用 "this")。

我一定是漏掉了什么。感谢任何帮助。

更新:这个答案不适用于 React,而是对问题的更一般的先前版本的回应。


这看起来像是不使用 onclick 属性的另一个参数,但您可以使用 callapply 方法,并将 this 作为第一个参数传递.

<div id="foo" onClick="Module.addClass.call(this)"></div>

但是您可能要考虑使用 addEventListener 或 jQuery 的事件委托。

所以我能够解决我自己的问题。这是成功的代码:

var React = require('react');
var GSAP  = require('gsap');
var $     = require('jquery')

var Psychedelicon = React.createClass({

    componentDidMount: function() {

        var that = React.findDOMNode(this.refs.psicon);
        $(that).hover(function() {
    //detect device type for Psychedelicon
            var touchPlatforms = ['iPhone', 'iPad', 'iPod', 'Android', 'Linux armv7l', 'WinCE'];
                       isTouch  = false;
                       iDevice  = false;
                       isDroid  = false;
                       plat = navigator.platform;

            if(plat === 'iPhone' || plat === 'iPad' || plat === 'iPod') {
                isTouch  = true;
                iDevice  = true;
            }
            else if (plat === 'Linux armv7l' || plat === 'Android') {
                isTouch  = true;
                isDroid  = true;
            }
            else {
                for (var i = 0; i < touchPlatforms.length; i++) {
                    if (plat === touchPlatforms[i]) {
                        isTouch = true;
                        break;
                    }
                    else {
                        isTouch = false;
                    }
                }
            }

    //sniff the for ie
            var isIE = false
                if (navigator.userAgent.toLowerCase().indexOf('msie') > -1 || navigator.userAgent.toLowerCase().indexOf('trident') > -1) {
                isIE = true
            }

    //sniff for firefox
            var isFF = false
                if (navigator.userAgent.toLowerCase().indexOf('firefox') != -1) {
                isFF = true
            }


    //Begin ColorSwirl on non-touch devices
            if(!isTouch) {
    //Define the Color Sets
                var ColorSwirl = function(colorSet,defaultColor,time) {
    //Pick random color. If the color is the same as the previous one pick blue instead.
                    var storedResult;
                    var randomColor = function(theArray) {
                        var result = theArray[Math.floor(Math.random() * (theArray.length))];
                        if(result === storedResult){
                            return(defaultColor)                        
                        }
                        else {
                            storedResult = result;
                            return(result)
                        }
                    }
    //Pick our colors for the initial state
                    var theLuckyColors = {top:randomColor(colorSet),bottom:randomColor(colorSet)};
    //Start swirling
                    $(that).addClass('swirling');
                    var swirl = function(){
                        if($(that).hasClass('swirling')) {
                            TweenLite.to(theLuckyColors, time, {colorProps:{top:randomColor(colorSet), bottom:randomColor(colorSet)}, onUpdate:colorize, onUpdateParams:[that],onComplete:swirl});
                        }
                    }
    //Detect Browser and Pass Psychedelicon the appropriate radial gradients
                    var colorize = function(el) {
                        if(isIE) {
                            TweenLite.set(el, {
                                backgroundImage:'-ms-radial-gradient(center,circle cover,' + theLuckyColors.top + ' 0%, ' + theLuckyColors.bottom + ' 100%)'
                            });
                        }
                        else if(isFF) {
                            TweenLite.set(el, {
                                backgroundImage:'-moz-radial-gradient(center,circle cover,' + theLuckyColors.top + ' 0%, ' + theLuckyColors.bottom + ' 100%)'
                            });
                        }
                        else {
                            TweenLite.set(el, {
                                backgroundImage:'radial-gradient(circle,' + theLuckyColors.top + ', ' + theLuckyColors.bottom + ')',
                                backgroundImage:'-webkit-radial-gradient(circle,' + theLuckyColors.top + ', ' + theLuckyColors.bottom + ')'
                            });
                        }
                    }
                    swirl();
                }
                ColorSwirl(['red','green','#4B0082','#9F00FF','yellow','orange'],'blue',.15);
            }

        },function() {
            var theLuckyColors = {top:'#FFFFFF',bottom:'#FFFFFF'};
            var stopNow = function(time){
                $(that).removeClass('swirling');
                TweenLite.to(theLuckyColors, time, {colorProps:{top:'#FFFFFF', bottom:'#FFFFFF'}, onUpdate:whiteWash, onUpdateParams:[that]});
            }
            var whiteWash = function(el) {
                    TweenLite.set(el, {
                        backgroundImage:'-ms-radial-gradient(center,circle cover,#FFFFFF 0%, #FFFFFF 100%)',
                        backgroundImage:'-moz-radial-gradient(center,circle cover,#FFFFFF 0%, #FFFFFF 100%)',
                        backgroundImage:'radial-gradient(circle,#FFFFFF,#FFFFFF)',
                        backgroundImage:'-webkit-radial-gradient(circle,#FFFFFF,#FFFFFF)'
                    });
            }
            stopNow(.15);       
        });
    },
    render: function() {
        return (
            <a className="psychedelicon" ref="psicon" href={this.props.href} target={this.props.target} onMouseEnter={this.cycleColors} onMouseLeave={this.stopTheCycle}>
                <i className={"fa fa-" + this.props.icon}></i>
            </a>
        )
    }

})

module.exports = Psychedelicon;

以下是我如何从问题到解决方案:

当我无法按照@Alexander O'Mara 的建议使用 "call" 生成结果时,我需要 jQuery 来加速测试并添加变量

var that = $(this)

到组件的最外层范围,这样我就可以从内部函数的范围访问组件本身,如下所示:

//Note that onUpdateParams now references "that" which is equal to "this" in the scope of the actual component.
TweenLite.to(theLuckyColors, time, {colorProps:{top:randomColor(colorSet), bottom:randomColor(colorSet)}, onUpdate:colorize, onUpdateParams:[that],onComplete:swirl});

这又失败了所以我将 "this" 的值记录到控制台并看到我实际上是在引用组件的构造函数而不是呈现的输出!

我再次查看了 docs,发现我可以通过使用名为 "refs" 的 reactjs 属性在每个渲染实例上引用渲染输出。我只需要给呈现的元素一个 "ref" 属性:

render: function() {
        return (
            <a className="psychedelicon" ref="psicon" href={this.props.href} target={this.props.target} onMouseEnter={this.cycleColors} onMouseLeave={this.stopTheCycle}>
                <i className={"fa fa-" + this.props.icon}></i>
            </a>
        )
    }

并在我的方法中引用 ref,我决定将 运行 改为 "componentDidMount"。

var that = React.findDOMNode(this.refs.psicon);

现在,每次我引用 "that" 时,我都是在引用渲染的元素本身(考虑到它在鼠标悬停时每 .15 秒重新渲染一次,这非常令人印象深刻)而且一切都很完美!