Skip to content
Advertisement

JavaScript – Confusion between global classes and inheritance

Total begginer who learns JS here. I don’t understand why when you declare a variable it doesn’t TOTALLY inherit of it’s parent class methods, for e.g.:

// I initiate an array (my question is the same for all type of vars)
var myArr = ["foo", "bar"]

// Let's say I call a random function of the parent class Array
console.log(Array.isArray(myArr)); // true

// Since I assume that myArr inherited of the COMPLETE LIST of Array's methods, I should be able to do this:
console.log(myArr.isArray()); // Uncaught TypeError

Why don’t the variables inherit of all of the methods of it’s parent classes? Instead of that you need to mix between the fonctions of Array and myArr. They should be identitical on the two sides, no?

Advertisement

Answer

When you declare a variable it is an instance of a Class, there is not inheritance.

When you declare a class that extends another class is where inheritance occurs.

Array.isArray() is a static property of the JavaScript Array object.

Usually, static methods are used to implement functions that belong to the class, but not to any particular object of it.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement