Can you override what == does in Javascript? [duplicate] - javascript

This question already has answers here:
Override the Equivalence Comparison in Javascript
(5 answers)
Overloading Arithmetic Operators in JavaScript?
(11 answers)
Closed 6 years ago.
In Python, it's possible to override what == does by defining a __eq__ method for your class.
Is it possible to do something similar in Javascript? If so, how do you do it?
My understanding is that, by default (and maybe always, if you can't override it), Javascript just checks to see if two objects reside at the same address when you do a ==. How can I get the address directly?
I'm asking all of this because I'm trying to debug some code someone who's long gone wrote. As far as I can tell, two objects are identical, and yet == is returning false. I'm trying to find why... I'm wondering if maybe == was overridden someplace (but it's a big codebase and I don't know what to search for) or if or if maybe the object was duplicated, so they exist at different addresses despite being identical as far as I can tell. Knowing how to get the address would help with confirming that theory and give me a lead to hunt down the problem.

Not typically. JS does not allow operator overloading or operators-as-methods.
There are certain operators that call toString or valueOf internally, which you can override on your own classes, thus influencing the behavior.

No, it is not possible to override any operators directly in JavaScript.

Related

Is an array in JavaScript a pointers array? [duplicate]

This question already has answers here:
Are JavaScript Arrays actually implemented as arrays?
(2 answers)
How are JavaScript arrays implemented?
(8 answers)
Closed 2 years ago.
I am new to JavaScript and lately, I found out that arrays in JavaScript are like lists in Java and that they can contain different types of variables.
My question is if in JavaScript an array are made of pointers? How is it possible to have different types in the same array, because we must define the array size before we assign the variables?
I have tried to find some information on Google, but all I have found are examples on arrays ):
You do not have to define the array size before you assign the variables. You can go like:
let array = [];
array.push(12);
array.push("asd");
array.push({data:5});
array.forEach(element => {
console.log(element);
});
Also I think you should not think about pointers with such a high level language. The better way is to look at variables like 'primitives' and 'objects'. Here is a good read about it:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures
High level languages, and in particular scripting languages, tend to reference most things with pointers, and they make pointer access transparent. Javascript does this also. Most everything, even primitives like numbers and strings, are objects. Objects in javascript have properties that store things. Those properties are essentially pointers, in that they are references to other objects. Arrays are implemented in the same way, and are in fact objects with numeric properties (and a few utility methods a standard object doesn't have, such as .length, .push(), .map(), etc.). Arrays don't hav a fixed size anymore than objects do. So everything in javascript is stored in these object "buckets" that can store anything in their properties (although you can seal objects, like numbers and strings, so that they don't accidentally change).
Languages with fixed data types (C like languages for instance) implement things with fixed data structures, and the exact size is easily calculable and known. When you declare a variable, the compiler uses the type of that variable to reserve some space in memory. Javascript handles all that for you and doesn't assume anything is a fixed size, because it can't. The size of javascript objects can change at any time.
In C-Like languages, when you ask for an array, you are asking for a block of a specific size. The compiler needs to know how big that is so that it can determine where in memory to put everything, and it can use the type of objects in the array to easily calculate that. Interpreted languages use pointers behind the scenes to keep track of where everything is stored, because they can't assume it will always be in the same place, like a compiled program can. (This is somewhat of a simplification and there are caveats to this of course).
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
JavaScript is a loosely typed language, Therefore there is noting stoping you from having different types in javascript array. but I would strongly avoid structuring your data that way without static type-checking (Typescript)
const test = ['test', {test:'test'}, 1, true]

Is there any method to check memory address of Javascript variable? [duplicate]

This question already has answers here:
How can I get the memory address of a JavaScript variable?
(4 answers)
Closed 10 months ago.
Is it possible to find the memory address of a JavaScript variable in modern Javascript? Or address of Typescript variable.
I am confused about the pass by value and pass by reference, using memory address it is easy to understand the reality. Now we have now ES8 and Typescript but still unable to get memory address of variable and Object.
From this post:
It's more or less impossible - Javascript's evaluation strategy is to always use call by value, but in the case of Objects (including arrays) the value passed is a reference to the Object, which is not copied or cloned. If you reassign the Object itself in the function, the original won't be changed, but if you reassign one of the Object's properties, that will affect the original Object.
All JavaScript runtimes that I know of hide this from you.
However, you can simulate the behavior of a memory pointer by using Typed Arrays and an appropriate buffer view based on the type of data you wish to store. You can simulate a pointer by saving your data at a particular offset, and then passing that offset around to different functions that manipulate the data directly in the typed array: aka the offset acts sort of like a low-level pointer.
JavaScript itself is meant to be implementation-agnostic, so concepts like memory addresses are intentionally absent from the language itself. Outside of the language, you can use the browser's debugging tools to take a memory snapshot, and that might contain the information. Note, however, that there is no real guarantee that an object will retain its address.

Trying to learn JavaScript Map() object for keyword-value pairs

In the early 1990s working in C developing real time music software, I wanted to code a keyword lookup algorithm which I finally did in JavaScript years later. It was accepted to SourceForge as HashCompactor, and someone asked me where to find out more about it. Then the HTML5 Map object obfuscated it, and my buggy forgotten code fell to the wayside.
I've recently rewritten it as MyMap to better match the JavaScript Map object for porting to other languages as originally intended. (Most of the demo action is in console.log.) I seems to match the specifications as best I can, but there's quirks specific to JavaScript I can't duplicate, so I'm curious as to whether I can use defineProperty to do so, say, by toggling a size property size to be read-only to the client instead of a function call, and I saw something about enumeration that might fix issues with the missing MyMap functionality?
I don't understand the defineProperty function whatsoever.
Otherwise, my biggest concerns are with garbage collection. Should every parameter be explicitly deleted or just those allocated with new? Didn't I see an algorithm that deep cleans objects?
Otherwise, the premise of the code is to read each character in the keyword string or number creating children for those that come after it so that everything beginning with the same character or digit only searches that call tree.
https://SkewsMe.com/code/HashCompactor/MyMap.html

Does the length of an object's property names impact memory usage? [duplicate]

This question already has an answer here:
Do browser engines compress keynames in large arrays of reoccurring objects?
(1 answer)
Closed 5 years ago.
I have array of objects containing around 200000 records. The objects have long key names. Will shorter property names consume less memory? Will it be enough to make a difference in performance?
If I prepare my data like:
[{"dTN":"datatype value","dTR":"dataType range"},....]
instead of:
[{"dataTypeName":"datatype value","dataTypeRange":"dataType range"},....]
how it will effect memory usage in browsers?
Can anyone please explain how javascript handles object property names?
You can check using window.performance.memory in Chrome.
If you have many repeating strings I believe they will be optimized, this completely depends on the engine of course. If the keys are all different you might see an increase.

Get exact syntax of javascript base functions [duplicate]

This question already has answers here:
Where can I find javascript native functions source code? [duplicate]
(3 answers)
Javascript native sort method code
(3 answers)
Closed 8 years ago.
Is there a way to know how test (for regex) or cos (for math) or substring (for string) or splice (for array) have been coded in the exact syntax either by a way in Javascript (like alert(function)) or by seeking in the files that come with Firefox?
Well, the source code of javascript likely isn't written in javascript itself, so you would be unable to just print it to the browser console or alert.
You can browse the source code of google's V8 engine, and see if it helps you ;)
http://code.google.com/p/v8/
These functions are actually implemented differently by browsers in one of the OS native languages.
So these algorithms are written in c, c++, c#, java etc. but not in JavaScript.

Categories

Resources