简体   繁体   English

如何在我的Javascript库中添加语法糖?

[英]How do I add syntactic sugar in my Javascript library?

Right now the library can translate this operation 现在,图书馆可以翻译此操作

Select * from List where name = k% order by desc  

to

List.filter(function(x) { return x.first_char() == 'k' }).sort().reverse()); 

Whats the best hack to remove the () so that the developer can write statements like: 什么是最好的黑客删除(),以便开发人员可以编写如下语句:

List.filter(fn(x) { return x.first_char == 'k' }).sort.reverse;

Naive approach: 天真的方法:

maxfn = function() {this[0]..};  Array.prototype.max = maxfn();

But with this approach I can't access 'this'. 但是通过这种方法,我无法访问“这个”。

I wanted to add a syntactic sugar for 我想为它添加一个语法糖

new Array("1","2","3")

to something like :)(suggestions needed) 类似于:)(需要建议)

_("1","2" ,"3")

like we have in scheme where list -> ' 就像我们在计划中的列表 - >'

I tried to clone the arguments but failed. 我试图克隆参数但失败了。

Thanks. 谢谢。

对于列表,您可以使用JSON表示法:

["1", "2", "3"]

You can use JSON notation as suggested by RoBorg, if you control the list... However, there's no cross-browser way to treat a property as a method. 如果您控制列表,则可以使用RoBorg建议的JSON表示法...但是,没有跨浏览器方式将属性视为方法。 Note: spidermonkey (firefox) does support using a getter (get method for a property). 注意:spidermonkey(firefox)支持使用getter(属性的get方法)。

Whats the best hack to remove the () 什么是最好的黑客删除()

Property getters/setters in JavaScript. JavaScript中的属性getter / setter Unfortunately it's a relatively new JavaScript feature that won't work on IE6/7 (as well as various other older browsers), so it's not really ready for prime-time yet (despite the intro of the linked article). 不幸的是,它是一个相对较新的JavaScript功能,无法在IE6 / 7(以及其他各种旧版浏览器)上运行,所以它尚未准备好进入黄金时段(尽管链接文章的介绍)。

You could do this particular example by making a JavaScript object that wrapped a String and shadowed all String's methods, then add a static 'first_char' property set to the String's first character on initialisation. 您可以通过创建一个包装String并覆盖所有String方法的JavaScript对象来执行此特定示例,然后在初始化时将String的第一个字符添加静态“first_char”属性。 But it's really not worth it. 但这真的不值得。

new Array("1","2","3") 新数组(“1”,“2”,“3”)

to something like :)(suggestions needed) 类似于:)(需要建议)

_("1","2" ,"3") _(“1”,“2”,“3”)

Well that's simple enough: 那很简单:

function _(/* items */) {
    var a= new Array();
    for (var i= 0; i<arguments.length; i++)
        a[i]= arguments[i];
    return a;
}

There's no point in doing it nowadays, though, since the array literal syntax: 但是,现在这样做没有意义,因为数组文字语法:

['1', '2', '3']

has been available since JavaScript 1.1-1.2 era and is available in every browser today. 自JavaScript 1.1-1.2时代开始提供,今天在每个浏览器中都可用。 (It predates JSON by many, many years.) (它比许多年前的JSON早。)

I'll try to answer one by one: 1) Why would you want to remove parenthesis from a functon call? 我将尝试逐个回答:1)为什么要从功能调用中删除括号? 2) If the "naive" approach is failing it's probably because you are calling the maxFn and assigning the results to Array.prototype.max. 2)如果“天真”方法失败,可能是因为您正在调用maxFn并将结果分配给Array.prototype.max。 It should be like this: 它应该是这样的:

maxfn = function() {this[0]..};  Array.prototype.max = maxfn;

3) RoBorg is correct, just use literal notation to construct arrays on the fly. 3)RoBorg是正确的,只需使用文字表示法即时构建数组。


Edit: 编辑:
Here's one way of implementing a max function on an array object. 这是在数组对象上实现max函数的一种方法。 The optional evaluator argument is a function that takes two parameters, the current max value and current value in array. 可选的赋值器参数是一个函数,它接受两个参数,即数组中的当前最大值和当前值。 It should return the object that is "greater". 它应该返回“更大”的对象。 Useful for non-primitives. 对非基元有用。

 Array.prototype.max = function(evaluator) { var max, i = 1; len = this.length; if (len > 0) max = this[0]; for (; i < len; i++) { if (evaluator) { max = evaluator(max, this[i]); } else if(max < this[i]) { max = this[i]; } } return max; }; var a = [1, 3, 4, 5, 6]; alert(a.max()); var b = ["Arnold", "Billy", "Caesar"]; alert(b.max()); var c = ["Arnold", new Date(), 99, true]; alert(c.max()); var d = [1, 3, 4, 5, 6]; alert(d.max(function (max, val) { return max < val ? val : max })); 

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

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