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

时间:2022-10-30 13:30:12

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.

4 个解决方案

#1


For lists you can use JSON notation:

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

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

#2


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. Note: spidermonkey (firefox) does support using a getter (get method for a property).

如果您控制列表,则可以使用RoBorg建议的JSON表示法...但是,没有跨浏览器方式将属性视为方法。注意:spidermonkey(firefox)确实支持使用getter(属性的get方法)。

#3


Whats the best hack to remove the ()

什么是最好的黑客删除()

Property getters/setters in JavaScript. 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中的属性getter / setter。不幸的是,它是一个相对较新的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. But it's really not worth it.

您可以通过创建一个包装String并覆盖所有String方法的JavaScript对象来执行此特定示例,然后在初始化时将String的第一个字符添加静态“first_char”属性。但它真的不值得。

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

to something like :)(suggestions needed)

类似于:)(需要建议)

_("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. (It predates JSON by many, many years.)

自JavaScript 1.1-1.2时代开始提供,今天在每个浏览器中都可用。 (它比许多年前的JSON早。)

#4


I'll try to answer one by one: 1) Why would you want to remove parenthesis from a functon call? 2) If the "naive" approach is failing it's probably because you are calling the maxFn and assigning the results to Array.prototype.max. It should be like this:

我将尝试逐个回答:1)为什么要从功能调用中删除括号? 2)如果“天真”方法失败,可能是因为您正在调用maxFn并将结果分配给Array.prototype.max。它应该是这样的:

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. 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 }));

#1


For lists you can use JSON notation:

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

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

#2


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. Note: spidermonkey (firefox) does support using a getter (get method for a property).

如果您控制列表,则可以使用RoBorg建议的JSON表示法...但是,没有跨浏览器方式将属性视为方法。注意:spidermonkey(firefox)确实支持使用getter(属性的get方法)。

#3


Whats the best hack to remove the ()

什么是最好的黑客删除()

Property getters/setters in JavaScript. 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中的属性getter / setter。不幸的是,它是一个相对较新的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. But it's really not worth it.

您可以通过创建一个包装String并覆盖所有String方法的JavaScript对象来执行此特定示例,然后在初始化时将String的第一个字符添加静态“first_char”属性。但它真的不值得。

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

to something like :)(suggestions needed)

类似于:)(需要建议)

_("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. (It predates JSON by many, many years.)

自JavaScript 1.1-1.2时代开始提供,今天在每个浏览器中都可用。 (它比许多年前的JSON早。)

#4


I'll try to answer one by one: 1) Why would you want to remove parenthesis from a functon call? 2) If the "naive" approach is failing it's probably because you are calling the maxFn and assigning the results to Array.prototype.max. It should be like this:

我将尝试逐个回答:1)为什么要从功能调用中删除括号? 2)如果“天真”方法失败,可能是因为您正在调用maxFn并将结果分配给Array.prototype.max。它应该是这样的:

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. 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 }));