自己动手实现jQuery Callbacks完整功能代码详解(3)

if ( typeof ( fn ) === 'function'
                    && fn.name !== ''
                    && ar.length > 0 )
                {
                    for ( var i = 0; i < ar.length; i++ )
                    {
                        if ( ar[i].name === fn.name )
                        {
                            f = true;
                            break;
                        }
                    }
                }

return f;
            };

// invoke funtions it includes one by one
            this.fire = function ( args )
            {
                if ( ops.once && firedTimes > 0 )
                {
                    return this;
                }

if ( ar.length > 0 )
                {
                    var r;

for ( var i = 0; i < ar.length; i++ )
                    {
                        r = ar[i].call( this, args );

if ( ops.stopOnFalse && r === false )
                        {
                            break;
                        }
                    }
                }

firedTimes++;

if ( ops.memory )
                {
                    lastArgs = args;
                }

return this;
            };
        };
 

测试函数如下:(注意fn1 fn2是匿名函数, fn2返回false , fn3是有“名”函数)

复制代码 代码如下:


 var fn1 = function ( v )
        {
            console.log( 'fn1 ' + ( v || '' ) );
        };

var fn2 = function ( v )
        {
            console.log( 'fn2 ' + ( v || '' ) );
            return false;
        };

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wdwxwp.html