本文介绍了在javascript中,我可以覆盖括号来访问字符串中的字符吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以定义 String [int] 以避免使用 String.CharAt(int)

Is there some way I can define String[int] to avoid using String.CharAt(int)?

推荐答案

不,没有办法做到这一点。

No, there isn't a way to do this.

这是来自其他语言的JavaScript开发人员的常见问题,其中运算符可以为某种类型定义或覆盖

This is a common question from developers who are coming to JavaScript from another language, where operators can be defined or overridden for a certain type.

在C ++中,在 MyType 上重载 operator * 并不是完全不可能的,最后是一个独特的涉及 MyType 类型的对象的操作的星号运算符。这种做法的可读性可能仍会受到质疑,但语言仍然提供了这种做法。

In C++, it's not entirely out of the question to overload operator* on MyType, ending up with a unique asterisk operator for operations involving objects of type MyType. The readability of this practice might still be called into question, but the language affords for it, nevertheless.

在JavaScript中,这根本不可能。您将无法定义一种允许您使用括号从 String 索引字符的方法。

In JavaScript, this is simply not possible. You will not be able to define a method which allows you to index chars from a String using brackets.

@Lee Kowalkowski 提出了一个很好的观点,即在某种程度上,可能使用括号访问字符,因为括号可用于访问a的成员JavaScript 数组。这将涉及创建一个新的 Array ,使用该字符串的每个字符作为其成员,然后访问 Array

@Lee Kowalkowski brings up a good point, namely that it is, in a way, possible to access characters using the brackets, because the brackets can be used to access members of a JavaScript Array. This would involve creating a new Array, using each of the characters of the string as its members, and then accessing the Array.

这可能是一种令人困惑的方法。 JavaScript的某些实现将通过括号提供对字符串的访问,而有些则不会,因此它不是标准做法。该对象可能会混淆一个字符串,并且由于JavaScript是一种松散类型的语言,因此存在歪曲类型的风险。定义一个数组仅仅是为了使用与语言已经提供的语法不同的语法,只是为了促进这种类型的混淆。这引起了 @Andrew Hedges 的问题:为什么要对抗语言?..

This is probably a confusing approach. Some implementations of JavaScript will provide access to a string via the brackets and some will not, so it's not standard practice. The object may be confused for a string, and as JavaScript is a loosely typed language, there is already a risk of misrepresenting a type. Defining an array solely for the purposes of using a different syntax from what the language already affords is only gong to promote this type of confusion. This gives rise to @Andrew Hedges's question: "Why fight the language?"..

有用的模式JavaScript 用于合法函数重载多态继承。这不是一个例子。

There are useful patterns in JavaScript for legitimate function overloading and polymorphic inheritance. This isn't an example of either.

除了所有语义之外,运营商仍然没有被覆盖。

All semantics aside, the operators still haven't been overridden.

旁注:习惯于强类型检查和经典继承惯例的开发人员有时会被JavaScript的C系列语法所困惑。在引擎盖下,它以一种不熟悉的方式工作。最好以干净,明确的方式编写JavaScript,以防止混淆。

Side note: Developers who are accustomed to the conventions of strong type checking and classical inheritance are sometimes confused by JavaScript's C-family syntax. Under the hood, it is working in an unfamiliar way. It's best to write JavaScript in clean and unambiguous ways, in order to prevent confusion.

这篇关于在javascript中,我可以覆盖括号来访问字符串中的字符吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 14:51