Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript: Class.method vs. Class.prototype.method

What is the difference between the following two declarations?

Class.method = function () { /* code */ } Class.prototype.method = function () { /* code using this.values */ } 

Is it okay to think of the first statement as a declaration of a static method, and the second statement as a declaration of an instance method?

like image 329
postrational Avatar asked Oct 28 '09 03:10

postrational


People also ask

What is the difference between class and prototype in JavaScript?

Classes. The most important difference between class- and prototype-based inheritance is that a class defines a type which can be instantiated at runtime, whereas a prototype is itself an object instance.

What is class methods in JavaScript?

Class methods are created with the same syntax as object methods. Use the keyword class to create a class. Always add a constructor() method. Then add any number of methods.

What is prototype method in JavaScript?

Every object in JavaScript has a built-in property, which is called its prototype. The prototype is itself an object, so the prototype will have its own prototype, making what's called a prototype chain. The chain ends when we reach a prototype that has null for its own prototype.


2 Answers

Yes, the first function has no relationship with an object instance of that constructor function, you can consider it like a 'static method'.

In JavaScript functions are first-class objects, that means you can treat them just like any object, in this case, you are only adding a property to the function object.

The second function, as you are extending the constructor function prototype, it will be available to all the object instances created with the new keyword, and the context within that function (the this keyword) will refer to the actual object instance where you call it.

Consider this example:

// constructor function function MyClass () {   var privateVariable; // private member only available within the constructor fn    this.privilegedMethod = function () { // it can access private members     //..   }; }  // A 'static method', it's just like a normal function  // it has no relation with any 'MyClass' object instance MyClass.staticMethod = function () {};  MyClass.prototype.publicMethod = function () {   // the 'this' keyword refers to the object instance   // you can access only 'privileged' and 'public' members };  var myObj = new MyClass(); // new object instance  myObj.publicMethod(); MyClass.staticMethod(); 
like image 53
Christian C. Salvadó Avatar answered Sep 21 '22 07:09

Christian C. Salvadó


Yes, the first one is a static method also called class method, while the second one is an instance method.

Consider the following examples, to understand it in more detail.

In ES5

function Person(firstName, lastName) {    this.firstName = firstName;    this.lastName = lastName; }  Person.isPerson = function(obj) {    return obj.constructor === Person; }  Person.prototype.sayHi = function() {    return "Hi " + this.firstName; } 

In the above code, isPerson is a static method, while sayHi is an instance method of Person.

Below, is how to create an object from Person constructor.

var aminu = new Person("Aminu", "Abubakar");

Using the static method isPerson.

Person.isPerson(aminu); // will return true

Using the instance method sayHi.

aminu.sayHi(); // will return "Hi Aminu"

In ES6

class Person {    constructor(firstName, lastName) {       this.firstName = firstName;       this.lastName = lastName;    }     static isPerson(obj) {       return obj.constructor === Person;    }     sayHi() {       return `Hi ${this.firstName}`;    } } 

Look at how static keyword was used to declare the static method isPerson.

To create an object of Person class.

const aminu = new Person("Aminu", "Abubakar");

Using the static method isPerson.

Person.isPerson(aminu); // will return true

Using the instance method sayHi.

aminu.sayHi(); // will return "Hi Aminu"

NOTE: Both examples are essentially the same, JavaScript remains a classless language. The class introduced in ES6 is primarily a syntactical sugar over the existing prototype-based inheritance model.

like image 40
Aminu Kano Avatar answered Sep 20 '22 07:09

Aminu Kano