Javascript 中的索引 getter

发布于 2025-01-12 07:24:24 字数 2422 浏览 0 评论 0原文

我直接使用 Javascript(请不要使用 JQuery 或类似的东西)。我已经实现了一个包装数组的类,因此:

class Ctrls
{
    _items = new Array();
        
    constructor()
    {
        this._items = new Array();
    }   
    
    Add(oCtrl)
    {
        this._items.push( { key:oCtrl.Name, value:oCtrl } );
    }   
    
    Clear()
    {
        this._items = new Array();
    }   
    
    get Count()
    {
        return this._items.length;
    }   
    
    get Item(index)
    {
        // get the index'th item. 
        // If item is numeric, this is an index.
        // If item is a string, this is a control name
        if (Number.isInteger(index))
        {
            return this._items(index).value;
        }
        else
        {
            item = this._items.find(element => (element.value.Name == index));
            return item;
        }
    }   
    
    get Items()
    {
        return this._items; // in case we desperately need to
    }
}

get Item(index) 处,我在页面加载时收到错误,这是 Uncaught SyntaxError: Getter 不得有任何形式参数。我来自 C# 世界,正在寻找相当于:

public Ctrl Item(iIndex)
{
    get
    {
        return _items[iIndex];
    }
}

How do I index a getter in Javascript?


编辑(1):我建议将 get Item 转换为函数,但如果我将定义更改为:

    function GetItem(index) // returns Ctrl
    {
        // get the index'th item. 
        // If item is numeric, this is an index.
        // If item is a string, this is a control name
        if (Number.isInteger(index))
        {
            return this._items(index).value;
        }
        else
        {
            item = this._items.find(element => (element.value.Name == index));
            return item;
        }
    }   

我在页面加载时收到此错误:Uncaught SyntaxError:意外的标识符< /code> 在 function GetItem...


编辑(2):将上面的内容修改为:

    GetItem(index) // returns Ctrl
    {
        // get the index'th item. 
        // If item is numeric, this is an index.
        // If item is a string, this is a control name
        if (Number.isInteger(index))
        {
            return this._items(index).value;
        }
        else
        {
            item = this._items.find(element => (element.value.Name == index));
            return item;
        }
    }   

因为类内的函数不使用 function 关键字,这很奇怪。现在这有效了。谢谢大家。

I'm using straight Javascript (no JQuery or anything like that, please). I've implemented a class which wraps an array, thus:

class Ctrls
{
    _items = new Array();
        
    constructor()
    {
        this._items = new Array();
    }   
    
    Add(oCtrl)
    {
        this._items.push( { key:oCtrl.Name, value:oCtrl } );
    }   
    
    Clear()
    {
        this._items = new Array();
    }   
    
    get Count()
    {
        return this._items.length;
    }   
    
    get Item(index)
    {
        // get the index'th item. 
        // If item is numeric, this is an index.
        // If item is a string, this is a control name
        if (Number.isInteger(index))
        {
            return this._items(index).value;
        }
        else
        {
            item = this._items.find(element => (element.value.Name == index));
            return item;
        }
    }   
    
    get Items()
    {
        return this._items; // in case we desperately need to
    }
}

I get an error on page load, at get Item(index), which is Uncaught SyntaxError: Getter must not have any formal parameters. I come from C# world and am looking for an equivalent of:

public Ctrl Item(iIndex)
{
    get
    {
        return _items[iIndex];
    }
}

How do I index a getter in Javascript?


Edit(1): I've had suggestions to turn get Item into a function, but if I change the definition to this:

    function GetItem(index) // returns Ctrl
    {
        // get the index'th item. 
        // If item is numeric, this is an index.
        // If item is a string, this is a control name
        if (Number.isInteger(index))
        {
            return this._items(index).value;
        }
        else
        {
            item = this._items.find(element => (element.value.Name == index));
            return item;
        }
    }   

I get this error on pageload: Uncaught SyntaxError: Unexpected identifier at the line function GetItem...


Edit(2): Modified the above to read:

    GetItem(index) // returns Ctrl
    {
        // get the index'th item. 
        // If item is numeric, this is an index.
        // If item is a string, this is a control name
        if (Number.isInteger(index))
        {
            return this._items(index).value;
        }
        else
        {
            item = this._items.find(element => (element.value.Name == index));
            return item;
        }
    }   

as functions within classes do not use the function keyword, oddly. This now works. Thank all.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不必你懂 2025-01-19 07:24:24

“你不能将参数传递给 JS 中的 getter”。理论上:是的,你不能那样做。但实际上:函数是 JS 中的一等公民,因此它们可以是函数的参数或返回值。你可以这样做:

class GetterWithParameter {
  constructor() {
    this.array = ["index 0", "index 1", "index 2"]
  }
  get itemAtIndex() {
    return (idx) => this.array[idx]
  }
}

const getterWithParameter = new GetterWithParameter()

const idx0 = getterWithParameter.itemAtIndex(0)
const idx1 = getterWithParameter.itemAtIndex(1)
const idx2 = getterWithParameter.itemAtIndex(2)

console.log("item at index 0:", idx0)
console.log("item at index 1:", idx1)
console.log("item at index 2:", idx2)

因此,虽然 getter 不能有参数,但您可以返回一个可以接收参数的函数 - 并使用它。

当然,其用法似乎与在需要相同参数的类上定义函数相同 - 但您仍然使用 getter

"you can't pass parameters to getters in JS". Theoretically: yes, you cannot do that. But practically: functions are first-class citizens in JS, so they can be arguments or return values of a function. You can do it like this:

class GetterWithParameter {
  constructor() {
    this.array = ["index 0", "index 1", "index 2"]
  }
  get itemAtIndex() {
    return (idx) => this.array[idx]
  }
}

const getterWithParameter = new GetterWithParameter()

const idx0 = getterWithParameter.itemAtIndex(0)
const idx1 = getterWithParameter.itemAtIndex(1)
const idx2 = getterWithParameter.itemAtIndex(2)

console.log("item at index 0:", idx0)
console.log("item at index 1:", idx1)
console.log("item at index 2:", idx2)

So, while the getter cannot have arguments, you can return a function that can receive an argument - and use that.

Of course, the usage seems identical to defining a function on the class that requires the same argument - but still, you are using a getter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文