18

In JavaScript, values of objects and arrays can be indexed like the following: objOrArray[index]. Is there an identity "index" value?

In other words:

Is there a value of x that makes the following always true?

let a = [1, 2, 3, 4];
/* Is this true? */ a[x] == a

let b = { a: 1, b: 2, c: 3 };
/* Is this true? */ b[x] == b

Definition of an identity in this context: https://en.wikipedia.org/wiki/Identity_function

9
  • 2
    Unless there is an element (keyed or index) that itself refers to the container, the expression will never be true (as in the shown code). Oct 5, 2021 at 1:34
  • 8
    I’m voting to close this question because all mathematical terminologies does not necessarily have an application in computer science Oct 5, 2021 at 1:35
  • 4
    One way to create this hypothetical “identity” index: a = [1,2,3]; a.myself = a; x = "myself"; a[x] === a .. (this is true for, and only for, the shown value of x) Oct 5, 2021 at 1:37
  • 23
    This question is being discussed on meta.
    – cigien
    Oct 5, 2021 at 1:50
  • 3
    First of all, substr is deprecated. Don’t use this method. Secondly, s.description.substring(0, 50) doesn’t need this length check. I don’t see how an “identity” would even help here. This is no longer property access, it’s the conditional operator — two completely different operators. Oct 5, 2021 at 2:06

2 Answers 2

31

There is no such thing built-in, as there is rarely a need for it (and sometimes even a need against it).0 It is nevertheless possible to roll your own ‘identity’ key:

const self = Symbol('self');

Object.defineProperty(Object.prototype, self, {
    enumerable: false,
    get() { "use strict"; return this; }
});

This will work on all primitives (other that null and undefined) and most JavaScript objects: that is, other than proxies or those that bypass the usual prototype chain by means of e.g. Object.create(null). Any object later in the prototype chain will also be able to disable the functionality, e.g. by doing { [self]: void 0 }; all these caveats mean that x[self] === x is by no means a universal law. But this is probably the best you can do.

Modifying Object.prototype is usually considered a bad idea, but the above manages to avoid most of its badness: adding the property at a symbol key (and making it explicitly non-enumerable as well) prevents it from unexpectedly showing up in iterations and lookups that walk the prototype chain, helping ensure no code should be impacted that does not specifically look for this property.


0 Even if such a feature existed, it would not be a good solution for the asker’s original use case: a ‘cut to 50 characters or take the whole string if shorter’ operation can be expressed as s.description.substring(0, s.description.length > 50 ? 50 : void 0) (or in fact just s.description.substring(0, 50)). It wouldn’t be any easier to express even with such a feature: depending on the condition, you still need to invoke the substring method, not just look it up, but not invoke the ‘self’ non-method. And given that you need to append an ellipsis at the end in the former case, you would still have to perform the condition check outside the substring call, making any shorthand rather ineffective. All that said, tricks like described in this answer do find some real use.

1
  • 1
    but the above manages to avoid most of its badness For posterity, it's worth mentioning the case where this would "break". If, for whatever reason, someone used Object.getOwnPropertySymbols(obj.constructor.prototype) they would see the self symbol. I wouldn't put it past people to have written this code. Oct 7, 2021 at 13:30
19

The indexing operation doesn't have an identity element. The domain and range of indexing is not necessarily the same -- the domain is arrays and objects, but the range is any type of object, since array elements and object properties can hold any type. If you have an array of integers, the domain is Array, while the range is Integer, so it's not possible for there to be an identity. a[x] will always be an integer, which can never be equal to the array itself.

And even if you have an array of arrays, there's no reason to expect any of the elements to be a reference to the array itself. It's possible to create self-referential arrays like this, but most are not. And even if it is, the self-reference could be in any index, so there's no unique identity value.

6
  • 1
    Well, there is the Symbol value which is technically an index of an object but doesn't give any of the actual values
    – Shivam
    Oct 5, 2021 at 1:34
  • 6
    @Shivam Not sure how that relates.
    – Barmar
    Oct 5, 2021 at 1:36
  • 1
    @Shivam “the range is Integer” refers to a[x], not to x. x is always either a symbol or coerced to a string. Oct 5, 2021 at 1:45
  • 2
    This needs more clarification… 1. Your a is an Array (i.e. an Object), your b an Object (this is the domain). 2. x is either a Symbol or a String. 3. a[x] and b[x] could be anything — this is the range, not x. If x is one of "0", "1", "2", "3", or "length", then a[x] is a Number. If x is one of the remaining strings from Object.getOwnPropertyNames(Array.prototype) or Object.getOwnPropertyNames(Object.prototype), or a symbol from Object.getOwnPropertySymbols(Array.prototype), then a[x] is always an Object. If x is anything else, a[x] is Undefined. Oct 5, 2021 at 2:19
  • 5
    ‘The domain and range of indexing is not necessarily the same’ – this is a silly non-obstacle in a dynamically-typed, dynamically-dispatched language that allows adding arbitrary properties to objects. And the prototype chain renders the second obstacle irrelevant as well. Oct 6, 2021 at 9:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.