Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Whats the meaning of 'static get' in Javascript (ES6)? [duplicate]

I need to now whats the meaning of an expression like this in Javascript

static get is() { return "custom-element"; }

I guess that static can have a behaviour similar to Java or C++ but I need more information about these syntax.

like image 968
Cesar Jr Rodriguez Avatar asked Mar 19 '17 09:03

Cesar Jr Rodriguez


People also ask

What is static get in JavaScript?

That is a static method you are looking at and the get is a getter for the property or the Object you want to get. If you look at explore static. static methods. Static properties (or class properties) are properties of Foo itself. If you prefix a method definition with static, you create a class method: > typeof Foo.

What is static in es6?

Static methods are often used to create utility functions for an application.” In other words, static methods have no access to data stored in specific objects. Note that for static methods, the this keyword references the class. You can call a static method from another static method within the same class with this.

What is the purpose of static methods?

A static method has two main purposes: For utility or helper methods that don't require any object state. Since there is no need to access instance variables, having static methods eliminates the need for the caller to instantiate the object just to call the method.

Can you call a static method from an instance JS?

So like java we do not require an instance of the class to call the static method in JavaScript also. Hence static method in JavaScript belongs to the class itself. In JavaScript also we use the static keyword to define any method as a static method. We just need to use the static keyword along with the method name.


1 Answers

You are correct. They are pretty much close to any other object oriented programming languages like C++ and Java

Everything is documented. That is a static method you are looking at and the get is a getter for the property or the Object you want to get.

If you look at explore static

static methods. Static properties (or class properties) are properties of Foo itself. If you prefix a method definition with static, you create a class method:

> typeof Foo.staticMethod
'function'
> Foo.staticMethod()
'classy'

And static property:

I can't think a great example than given in docs on top of my head right now. Here i am pasting essential part.

class Point {
    constructor(x, y) {
        this.x = x;
        this.y = y;
    }
}

Point.ZERO = new Point(0, 0);

You could use Object.defineProperty() to create a read-only property, but I like the simplicity of an assignment.

Second, you can create a static getter:

class Point {
    constructor(x, y) {
        this.x = x;
        this.y = y;
    }
    static get ZERO() {
        return new Point(0, 0);
    }
}

In both cases, you get a property Point.ZERO that you can read. In the first case, the same instance is returned every time. In the second case, a new instance is returned every time.

like image 117
Suresh Atta Avatar answered Oct 14 '22 01:10

Suresh Atta