Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ES6 - declare a prototype method on a class with an import statement

I am using ES6 classes. I want to be able to do this:

function Car(color) {   this.color = color; };  Car.prototype.getColor = require('./getColor'); 

Where get color is an exported function. i.e. I want to be able to import a function from an outside file and set is as a prototype method on the ES6 Class. This is the kind of syntax I am talking about:

class Car {   constructor(color) {     this.color = color;   }    getColor() {} // I want to import this function from './getColor', as above } 

Is this doable?

like image 877
SheedySheedySheedy Avatar asked Sep 21 '15 16:09

SheedySheedySheedy


People also ask

Does ES6 use prototype?

The prototype property allows you to add properties and methods to any object (Number, Boolean, String and Date, etc.). Note − Prototype is a global property which is available with almost all the objects. Use the following syntax to create a Boolean prototype.

Is it possible to add methods to a class prototype?

You can still attach a method on a class' prototype; after-all, classes are just syntactic sugar over a "functional object", which is the old way of using a function to construct objects. Since you want to use ES6, I'll use an ES6 import.

How to use prototypical inheritance with ES6?

Use prototypical inheritance with ES6 0 Add variable method to ES6 class 0 ES6 Javascript Classes - define class method to an existing function 12 Create ES6/ESNext prototype function with different scope (not an inline function)

What is the purpose of the Class Syntax in ES6?

To alleviate this, ES6 introduces the class syntax. Classes in ES6 don't add any functionality to what we already have in the language, they are just a simpler syntax for building the same objects as we had before. This creates a JavaScript object.

How to import and export variables and functions in ES6?

ES6 | Import and Export 1 Syntax for variable: export let variable_name; 2 Syntax for function: export function function_name () { // Statements } 3 Syntax for class: export class Class_Name { constructor () { // Statements } } 4 Example 1: Create a file named export.js and write the below code in that file. ... More items...


1 Answers

You can still attach a method on a class' prototype; after-all, classes are just syntactic sugar over a "functional object", which is the old way of using a function to construct objects.

Since you want to use ES6, I'll use an ES6 import.

Minimal effort, using the prototype:

import getColor from 'path/to/module';  class Car {     ... }  Car.prototype.getColor = getColor; 

As you can see, you still use the prototype property to attach a method, should you choose to.


Calling the module within a class' method:

Alternatively, if you don't want to use the prototype property, you can always have your method return the function from the module:

import getColor from 'path/to/module';  class Car {     getColor () {         return getColor.call(this);     } } 

Using a Getter

You could also be a bit tricky and use a "getter" to achieve this in a different manner.

import getColor from 'path/to/module';  class Car {     get getColor () { return getColor.bind(this) } } 

You could then use it simply by calling, myInstanceOfCar.getColor()

Or in a more semantic usage of a getter:

class Car {     get color () { return getColor.call(this) } }  // ...  const color = myInstanceOfCar.color; 

Keep in mind that getters/setters cannot have the same name as properties that you set in the constructor. You will end up exceeding the maximum call-stack with infinite recursion when you try to use the setter to set that same property. Example: set foo (value) { this.foo = value }


ES2016 Class Properties

If you're using Babel to transpile (and are using experimental proposals), and want to use some ES2016, you can use the following syntax (but keep in mind that this applies the method to the object directly, and does not set it on the prototype):

import getColor from 'path/to/module';  class Car {     getColor = getColor; } 

Optional binding w/ class properties

If you use the shorthand syntax for setting a property, you won't have to bind the method (setting is as a property changes what "this" refers to, essentially automatically binding it), but you certainly can, should you choose to (like if you'd like to bind something else):

getColor = getColor.bind(this); 
like image 184
ndugger Avatar answered Oct 23 '22 10:10

ndugger