How does the browser traverse the prototype chain with string and array?

Asked

Viewed 73 times

1

To illustrate my question and my question I will use the following simple example:

function Car(name, age) {
    this.name = name;
    this.age = age;

    this.phrase = function() {
      alert(`${this.name} ${this.age} a fast car`);
    };
}

let car1 = new Car('Ferrari', 2019);

In the above example I have two properties and a method that are defined in the constructor function Car() these are name, age, phrase() what happens is that the builder Car() has some other methods already included (by default) when constructor is created Car() such as, for example, toString(), valueOf(), toLocaleString() etc, if I call these methods will work:

car1.toString()
car1.valueOf()
car1.toLocaleString()

// etc

These methods are not defined in the constructor Car() and yes in its own prototype which is Object() and for the browser to execute these methods that are not defined in the constructor Car() it climbs up the prototyping chain checking if each prototype object has the specified method, if it finds it will perform:

Checking whether the prototype of car1 has the specified method

car1 > Car() > Não possui o método

Checking whether the prototype of Car() has the specified method

car1 > Car() > Object() > Possui o método

In the case of Javascript (at least one language I know) there is no way you can call a method using the syntax from above (dot notation) in a number like, for example:

19.toFixed(3);
19.toExponential();
19.toPrecision(3);

But in strings and arrays you can use the syntax from above:

let a = ['a', 'b', 'c'].length;
let b = 'abc'.indexOf('g');

console.log(a);
console.log(b);

What I’d like to understand is how the browser executes the methods and properties in an array and string or how it traverses the prototyping chain when I use methods of Array and String (as in the first example).

  • 2

    In fact it has how to call the methods in numbers yes: 1 .toString() (note the space between the number is the point) or (1).toString(), for example. This is because the point right after the number is used to denote its fractional part.

  • 2

    Complementing the above comment, see here. Your question has not been answered here? "When invoking the method, the interpreter checks whether the object in question has a method with that name... So the interpreter checks to see if the prototype of each object has the method... If the method was not found, the interpreter would continue the verification by climbing the prototype chain until it found the desired method or property, or until it arrived at an object that has null as a prototype."

  • This answers your question? How prototypes work in Javascript?

  • Really interesting did not know these little details when calling a method in a number, vlw.

  • @Augustovasques, do not! I even understand how prototypes work, but with string and array leaves me confused as, for example, when a string is set as the browser will go through the prototype chain to assign the method to string? by the time I define the string it will already have its proper methods even though it is not an object?

  • String and tb arrays are objects, the difference being that they are native to the language, with all methods already "predefined". But the operation is the same, we look for the method in the prototype of the object, if not find going up the prototypes chain, etc

  • Example: https://ideone.com/h88jyn

  • @hkotsubo atá understood I thought I had a specific way of climbing the chain with strings and arrays :), but thank you.

  • I may even draw up an answer, but that argument "...there is no way you can call a method using the syntax from above (point notation) on a number like..." I will refute it. My fear is to waste time gathering material and produce a theoretical response and end up being offended. The question, there is some problem in confronting parts of your question?

  • 1

    @Augustovasques I still think it’s duplicate. It is that he thought that native objects - as string and array - had different treatment, but it has been clarified that it is the same as the one already explained here. And about calling method in number, has been answered here. I do not know if you have more to add. And the way the question is, I understand that it is duplicated

  • @Augustovasques, not without a problem.

Show 6 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.