Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

What is the reasoning behind the MDN syntax using 'prototype'?

In all of the examples I have seen about syntax in every language, the variable is represented by one word or a combination of words combined as one as in "samplewebsite.com" or "firstName.length". Although not super critical to understanding this video, why does the MDN say "String.prototype.toLocaleLowerCase()" instead of just "String.toLocaleLowerCase()"? What is the MDN trying to communicate by keeping 'prototype' as part of the syntax?

For reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toLocaleLowerCase

4 Answers

Hi Matthew,

This is a feature of object oriented programming and is not unique to javascript. There are methods that can be called on a class itself (Class Methods), and then there are methods that can be called on instances of a class (Instance Methods). Because toLocaleLowerCase() is an instance method, you cannot call it directly on String. Think about it - what would it mean to change the String class to lowercase? It doesn't make sense. However, if you have an instance of String stored in a variable (var greeting = "Hello World"), we can convert that to lowercase (greeting.toLocaleLowerCase() // returns "hello world").

The prototype object is just what it sounds like - it's a prototype instance of a class. Essentially, it gives you the ability to use an instance method without really creating your own instance. Thomas gives a good example of how this can be useful in his answer.

For the documentation, any time you see Class.prototype.methodName(), you're looking at an instance method. You will also see Class.differentMethodName(), which would be a class method. For example String.fromCharCode(), which takes a number and returns an instance of String, is a class method.

Hope this helps!

Cheers :beers:

-Greg

the prototype object is a key concept in javascript. This link explains it fairly well

For example, If I wanted to make my own filter function that can be called on Arrays, I would write something like this:

Array.prototype.customFilter = function(callback) {
    var arr = [];
    for (var i = 0; i < this.length; i++) {
        if(callback(this[i])) {
            arr.push(this[i]);
        }
    }

    return arr;
} 


var arr = [1, 2, 3, 4];
var filteredArr = arr.customFilter(n => n % 2 == 0);


//Outputs [2, 4]
console.log(filteredArr);

Since posting this question, I've come across this video which also gives a good explanation of prototype: https://teamtreehouse.com/library/objectoriented-javascript/constructor-functions-and-prototypes/methods-with-prototypes

From what I see theyre using prototype to represent an object of properties. According to them: You can use the constructor's prototype object to add properties or methods to all instances in that prototype. I understand what you mean, why couldnt they just leave off prototype? Maybe there are other functions that are not considered objects & they want to differentiate that? Hmmmm