This question already has answers here:
Example of Properties vs. Methods in JS
(3 answers)
Closed 3 years ago.
In chapter 4 of Eloquent JavaScript, it reads: "[Data types, namely, strings] have built in properties. Every string value has a number of methods. Some very useful ones are slice and index which resemble the array methods of the same name.
For me, the excerpt that I've cited from Eloquent JavaScript appears to use the terms "property" and "method" interchangeably.
From MDN Web Docs, I understand that "A JavaScript property is a characteristic of an object, often describing attributes associated with a data structure.
Additionally, MDN's glossary defines a method as a function which is a property of an object.
Can anyone assist in distinguishing the terms "property" and "method" (as used in JavaScript) from one another?
Short answer
Property is something "about" the string e.g. its length.
Method is something that can be done to the string e.g. slice
Related
This question already has answers here:
Complexity of accessing data in an object
(2 answers)
Closed 2 months ago.
I was watching a youtube tutorial in DS&A (using JS) and he mentioned that the time complexity of accessing or removing property in an object is constant?
how is that possible shouldn't we search for this property first and then delete it or access it. so, it should be linear!
JavaScript objects are actually hash tables under the hood. (See https://en.wikipedia.org/wiki/Hash_table for more info on how hash tables work.)
In simple terms, in a hash table, the key itself directly correlates to a number when put through the hash table's hashing function, which number is (or correlates to) an address. Thus, JavaScript can (typically) directly use the key name to access that number and/or address. This has runtime complexity O(1) because the key's string (if it's a string) can be decoded into an address without any iteration over the other keys in the object.
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]
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.
This question already has answers here:
What is the difference between iterator and iterable and how to use them?
(15 answers)
In JavaScript ES6, what is the difference between an iterable and iterator?
(3 answers)
Closed 6 years ago.
What is the difference between Iterator and Iterable? Is one sub-type of the other or not? What are the actual differences in some real applications?
I am trying to read some tutorials and specifications, but they are all very complicated.
(I am using ES6 and Babel, if that helps.)
From Exploring ES6 by Dr. Axel Rauschmayer:
An iterable is a data structure that wants to make its elements accessible to the public. It does so by implementing a method whose key is Symbol.iterator. That method is a factory for iterators.
An iterator is a pointer for traversing the elements of a data structure (think cursors in databases).
This question already has answers here:
Map vs Object in JavaScript
(15 answers)
Closed 7 years ago.
I've been looking at the new ES6 docs on MDN and I can't find a real world use for the Map object where a normal object wouldn't work. Does anyone have any use cases for Maps and explain why an object wouldn't work in that scenario?
MDN lists a number of important differences:
An Object has a prototype, so there are default keys in the map. [editor: the good old hasOwnProperty issue]
The keys of an Object are Strings, where they can be any value for a Map.
You can get the size of a Map easily while you have to manually keep track of size for an Object.
A Map iterates its elements in insertion order, whereas iteration order is not specified for Objects.
So a Map is finally an insert-ordered key-value store for Javascript, which additionally allows mapping any value to any value, instead of restricting keys to be strings. This can greatly simplify some code where ordering is important, or where objects or other complex data types need to be associated with other data.