31

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.

Cesar Jr Rodriguez
  • 1,691
  • 4
  • 22
  • 35
  • 2
    Generally when you want to find out more information about syntax, you can Google it first. I would recommend doing that first, and then asking here if something is unclear. – 4castle Mar 19 '17 at 09:09

1 Answers1

35

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.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • 2
    I would be very careful with that first sentence. The syntax is similar, and that's about all there is in common. A `class` in JS is just syntactic sugar for a constructor function with a `prototype`. – 4castle Mar 19 '17 at 09:13
  • @4castle I'm scared. `Static properties (or class properties) are properties of Foo itself. If you prefix a method definition with static, you create a class method:` this applies to Java as well. Correct me if I am wrong with any sentence. – Suresh Atta Mar 19 '17 at 09:15
  • 4
    That's all correct, but assuming JS will behave like Java is dangerous. For example, in JS, `static` properties are inherited by the functions which `extend` the original function, also objects which inherit from `Foo.prototype` will not have access to the `static` method like they would using Java syntax. I find that people who use `class` syntax don't take the time to learn the "old-fashioned" way to do things, and they end up using classes in inappropriate ways. – 4castle Mar 19 '17 at 09:22
  • 1
    @4castle That is the reason I said that the pretty much and not completely :) and I have to agree with your last sentence completely. – Suresh Atta Mar 19 '17 at 09:28