简体   繁体   English

如何在JavaScript中添加语法糖(或您应该)

[英]How to add syntactic sugar in javascript (or should you)

So I was setting out to build a simple library that would swap "screens" in and out of a viewport for a full screen web app. 因此,我开始着手建立一个简单的库,该库可以将视口内外的“屏幕”交换为全屏Web应用程序。

I created a basic interface to support this: 我创建了一个基本的界面来支持这一点:

function Swap(element_in, element_out, animation_in, animation_out)

When I finished, I thought, hey this is javascript -- I wonder if I can create some crazy syntax that reads like a sentence. 完成后,我想,嘿,这是javascript -我想知道是否可以创建一些听起来像句子的疯狂语法。 My goal was this: 我的目标是:

Swap(element_in).InBy(animation_in).AndSwap(element_out).OutBy(animation_out)

ie

Swap('screen1').InBy('slidingitoutleft').AndSwap('screen2').OutBy('slidingitoutright');

After some reading I found a post here on SO (which I can't seem to find now) which allowed me to achieve this (with some pitfalls, namely, you can call those methods in any order and you have to store some variables). 经过一番阅读后,我在这里找到了一篇关于SO的文章(现在似乎找不到),这使我得以实现(有一些陷阱,即,您可以按任何顺序调用这些方法,并且必须存储一些变量) 。 This interface looks like this: 该界面如下所示:

function Swap2(element_in) {
  var _element_in, _element_out, _animation_in, _animation_out;

  // assign param
  _element_in = element_in;

  this.InBy = function(animation_in) {
    _animation_in = animation_in;
    return this;
  }

  this.AndSwap = function(element_out) {
    _element_out = element_out;
    return this;
  }

  this.OutBy = function(animation_out) {
    _animation_out = animation_out;

    // Do the swap!
    return Swap(_element_in, _element_out, _animation_in, _animation_out);
  }

  return this;
}

After this, I thought pretty cool but I bet I can do better. 在此之后,我觉得很酷,但我敢打赌,我可以做得更好。 I came up with this (try not to throw up): 我想出了这个(尽量不要抛出):

function Swap3(element_in) {
  return {
        InBy: function (animation_in) {
            return {
                AndSwap: function (element_out) {
                    return {
                        OutBy: function (animation_out) {
                            return Swap(element_in, element_out, animation_in, animation_out);
                        }
                    };
                }
            };
        }
    };
}

This last one forces the methods to be called in the right order, and doesn't hang on to any parameters - it just passes them along. 这最后一个强制以正确的顺序调用方法,并且不会挂在任何参数上,它只是传递它们。

I made a codepen just so you can see it actually works (although admittedly I've only been playing with this in Chrome): http://codepen.io/andrewleith/pen/emltv 我制作了一个Codepen只是为了让您可以看到它确实有效(尽管坦白地说我只是在Chrome中玩过这个): http : //codepen.io/andrewleith/pen/emltv

So, my question: am I going to javascript hell for this? 所以,我的问题是:我要为此去JavaScript地狱吗? Is this A Good Thing? 这是一件好事吗? Or A Very Bad Thing? 还是很糟糕的事情? Or is it just preference? 还是只是偏爱?

Andrew 安德鲁

am I going to javascript hell for this? 我要去javascript地狱吗?

No, you're going to JavaScript hell for capitalizing non-constructor functions ;) 不,您将因非构造函数的大写而进入JavaScript地狱;)

Anyways, both your implementations work, but consider using something more standard-looking: 无论如何,您的两个实现都可以使用,但是请考虑使用外观更标准的东西:

swap(element_in, {
    inBy: animation_in,
    andSwap: element_out,
    outBy: animation_out
});

I think I don't see the value. 我认为我看不到价值。 It looks like an english sentence, sure, but how important is that? 看起来好像是英语句子,但这有多重要? There are more characters to type and more data to send to the client (and I doubt this minifies very well). 有更多的字符可以键入,并且有更多的数据可以发送给客户端(我怀疑这样做会减少很多)。 If it were possible to do different things for each step, I could see it being more beneficial, but I don't know that I prefer it to a normal function call with the four steps being necessary in the same order. 如果可以对每个步骤执行不同的操作,那么我会发现它更有益,但是我不知道我更喜欢普通的函数调用,因为四个步骤必须以相同的顺序进行。 The code, with the pyramid hell you've got, is definitely harder to read. 带有金字塔地狱的代码绝对很难阅读。

Otherwise, there's nothing technically wrong with the approach. 否则,该方法在技术上没有任何错误。

================== Edit ==================编辑

To avoid the pyramid, you can do the following. 为了避免金字塔,您可以执行以下操作。 this would also give you control over what following steps are possible at each step. 这也使您可以控制每个步骤中可以执行哪些后续步骤。

function Swap3(element_in) {
    var _element_in, _element_out, _animation_in, _animation_out;

    _element_in = element_in;

    return {
        inBy: inBy
    };


    function inBy(animation_in) {
        _animation_in = animation_in;

        return {
            andSwap: andSwap
        };
    }

    function andSwap(element_out) {
        _element_out = element_out;

        return {
            outBy: outBy
        };
    }

    function outBy(animation_out) {
        _animation_out = animation_out

        return Swap(_element_in, _element_out, _animation_in, _animation_out);
    }

}

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

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