2

Besides all the other problems with js, I have to ask the experts:

What is the error or error classes commonly caused in javascript by conceptually treating prototypes or dictionaries as objects proper?

How or in what way does this hurt development as opposed to languages that make distinctions between prototypes and objects?

Comments
  • 0
    Could you elaborate? Do you mean the usage of the `.` operator to access object fields?
  • 1
    Answering as a Javascript enthusiast rather than an expert which I'm not. But before that let's rephrase the question a bit for better understanding...

    Q1) What exception is thrown in Javascript when you treat prototypes or dictionaries as objects?

    Q2) With regards to Question 1, how does this affect development in Javascript compared to other languages that makes a clear distinction between a prototype and an object?
  • 1
    Short Answer:

    A1) No exception will be thrown because they're both objects unless assumed otherwise.

    A2) Similar to Javascript, prototypes are also objects in languages like C# and Java. Since JS is loosely typed, development challenges may vary.
  • 0
    Long Answer:

    A1) Both Prototype and Dictionary (known as Map in Javascript) are basically objects. Every parent object in javascript has a property called "prototype" while their instances has hidden property called "__proto__". Calling either properties will return all the available methods and properties contained in its constructor which is a function.

    Let's go a little deeper so we can understand possible exceptions based on your assumption that may occur when a prototype object is mistaked for an "object instance"...

    Here is a short example;

    -- Let's say we have an object "Person" with a constructor like below:

    function Person(firstname, lastname){

    this.firstname = firstname;

    this.lastname = lastname;

    }
  • 0
    -- Pay attention here...

    // Firstly, this is an instance object of Person

    let personA = new Person("David", "Fox");

    // Secondly, below is a prototype object of Person

    // we derive using the property "__proto__"

    // which gives access to hidden property "Person.prototype"

    let personB = personA.__proto__;

    // Echoing firstname from instance object

    // The below will echo out "David"

    console.log(personA.firstname);

    // Echoing firstname from prototype object

    // The below will throw a TypeError because we try

    // to perform a trim action on property which has no

    // default value. --Important-Line

    console.log(personB.firstname.trim()); //--TypeError
  • 1
    // What if we want their fullname?

    // The below will echo "undefined"

    // because object Person has no property named "fullname"

    console.log(personA.fullname);

    // Can we add the fullname property via instance?

    // No, we can't because the constructor is closed for

    // the day and probably must have gone home.

    personA.fullname = "David Foxer"; // Error WTF is fullname

    // But we can add extra props using the prototype object

    personB.fullname = "David Foxer"; // Yeah! Smart guy.

    // Now because Person object has been manipulated via personA,

    // hence all references and new instances will have

    // the property "fullname" default value "David Foxer";

    let personC = new Person("Tim", "Trogus");

    console.log(personC.fullname); // David Foxer
  • 0
    -- With the example above most especially the line tagged "--Important-Line"

    You could see that prototype object (personB) is just a template with an interface to manipulate the properties of Person object while an instance object gives access to the object property values but no means of constructor manipulation.

    When you mistake a "prototype object" for an "instance object" you can get a "TypeError" exception when trying to make use of or perform actions on uninitialized properties.
  • 1
    A2) Similar to Javascript, Prototype in most languages like C# and Java are also object instances used as template for creating other objects.

    You cannot explicitly distinguish a prototype from an object because that is what a prototype is.

    Nevertheless in Javascript, since prototypes are inherited by default on every object, there are challenges understanding whose object properties a prototype derives from (i.e if either it's from the current instance, parent object or parent's parent object). Such challenges are rarely encountered on other languages since you'd have to create the prototypes yourself.
Add Comment