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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With