简体   繁体   English

OO JavaScript规避.call

[英]OO JavaScript circumventing .call

Take a look at the fiddle here 看看这里的小提琴

In the show function the JavaScript call method is used to make this refer to the container variable in my contactForm object. 在播放功能,使用JavaScript调用方法,使thiscontainer的变量在我的联系形式对象。 I think, I'm not too sure about the magic that makes this work. 我认为,我不太确定这项功能的魔力。 Can someone elucidate why this does work, and what a good alternative might be? 有人可以阐明为什么这样做有效,还有什么好的替代方法?

JS JS

 $(document).ready(function () {
            var contactForm = {
                container: $('#contact'),
                config: {
                    effect: 'slideToggle',
                    speed: 400
                },
                /*******************/
                init: function (config) {
                    $.extend(this.config, config);
                    $('<button>').text('Contact me')
                                 .attr('type', 'button')
                                 .insertAfter('#firstArticle')
                                 .on('click', this.show);
                   //currently only logic on the close button
                },
                /*******************/
                show: function () {
                    //using variable names to shorten up
                    var cf = contactForm,
                        container = cf.container,
                        config = cf.config;
                    if (container.is(':hidden')) {
                        cf.close.call(container);
                        container[config.effect](config.speed);
                    }
                },
                /*******************/
                close: function () {
                    var self = $(this);
                    if (self.find('span.close').length) {
                        return;
                    }
                    $('<span>').addClass('close')
                               .text('close')
                               .prependTo(this)
                               .on('click', function () {
                                   //self= span
                                   self[contactForm.config.effect](500)
                               });
                }
            };


            contactForm.init();

        });

There's no magic at all; 根本没有魔术; that's just how call works. 这就是call工作方式。 call lets you call a JavaScript function and manually specify the this value therein, followed by all of the parameters, listed out individually. call ,您可以调用JavaScript函数并在其中手动指定this值,后跟所有参数(分别列出)。

So 所以

cf.close.call(container);

calls cf.close with the this value set to container . this值设置为container调用cf.close Hypothetically, this 假设地,这

cf.close.call(container, 1, 'b');

would do the same thing, except also pass in 1 and 'b' as parameters. 会做同样的事情,但通过在1和“b”作为参数。

Call is very, very similar to apply , with the difference being that apply takes all parameters as an array, rather than being listed out individually. 调用与apply非常非常相似,不同之处在于apply会将所有参数作为一个数组,而不是单独列出。 So the (hypothetical) second example would be the same as 因此,(假设的)第二个示例将与

cf.close.apply(container, [1, 'b']);

This can be incredibly useful when you want to call another function, set the this value, and wholesale pass all of the current function's arguments along for the ride. 当您要调用另一个函数,设置此值并批量传递当前函数的所有参数进行骑行时,这将非常有用。 Ie

someFunction.apply(thisValue, arguments);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM