How to call super/base methods in JavaScript? [duplicate] - javascript

This question already has answers here:
Emulate super in javascript
(11 answers)
Closed 8 years ago.
I have to implement following code in Javascript, but cannot find equivalent of super.say() in javascript. How to do this translation ?
class Foo {
public void say() {
System.out.println("foo");
}
}
class Bar extends Foo {
static int counter = 0;
public void say() {
super.say();
counter++;
System.out.println("bar " + counter);
}
}

There's an article that describes it in detail: http://joshgertzen.com/object-oriented-super-class-method-calling-with-javascript/
Basically, you need to store a reference to your base method, then call it in the "derived" (perhaps "shadowed" might be a better word here) method.

The keyword super doesn't work in Javascript as in Java because there's no notion of classes. Without a class, you can't have a descendant, and thus super is useless in that context.
You could implement something super-like, as described here.

Related

Can Javascript Class methods still be considered "pure", if they call other "pure" class methods? [duplicate]

This question already has answers here:
Can a method be a pure function?
(1 answer)
Can function which calls pure function can be called pure function?
Can a pure function call external function?
(3 answers)
Is a nested pure function still a pure function?
(3 answers)
Closed 11 months ago.
Consider
class Service {
methodA(input: string[]): number {
const resB = this.methodB(input);
return resB * 2;
}
methodB(input: string[]): number {
return input.length;
}
}
If MethodB is a pure function, can MethodA also be considered a pure function?
What about the case, when MethodB is a pure function from an injected class?
class Service {
constructor(helper:Helper){}
methodA(input: string[]): number {
const resB = this.helper.methodB(input);
return resB * 2;
}
}
In both cases I argued yes in my team, because both cases no side effects takes place and the input>output is deterministic.
On the other side, let alone the the fact, that MethodA was using this. inside its function body was enough for other to say no.

Typescript: calling static methods in constructor [duplicate]

This question already has answers here:
Access to static properties via this.constructor in typescript
(5 answers)
Closed 1 year ago.
here is an example of my class with the methods in question
class Example {
constructor(info) {
// call validateInfo(info)
}
static validateInfo(info):void {
// validate the info
}
I would like to call validateInfo in the constructor, but I can't just do this with this.validateInfo(info) because it is a static method.
In Javascript, I could do this:
constructor(info) {
this.constructor.validateInfo(info)
}
but, in Typescript this gives the following error:
error TS2339: Property 'validateInfo' does not exist on type 'Function'.
I understand the error message, but is there a Typescript equivalent for the Javascript solution?
The problem is that a subclass may have called the constructor, not Example. I'd reference Example specifically:
constructor(info) {
Example.validateInfo(info)
}
Another option to consider would be making it a normal method rather than a static method.

how to check if ES6 static method eists [duplicate]

This question already has answers here:
When do I use parentheses and when do I not?
(5 answers)
How to check if function exists in JavaScript?
(34 answers)
Closed 3 years ago.
I have an ES6 class with a static method. From another class, I wish to call that static method, but in some instances, the method may not exist.
how do I check if a static method exists before calling it?
class MyClassOne
{
static myStaticMethod()
{
.... do something
}
}
Thanks
class MyClassTwo
{
myFunc()
{
if(MyClassOne.myStaticMethod) <-- what should this be?
MyClassOne.myStaticMethod;
}
}

Is there any difference between defining public methods inside and outside constructor? [duplicate]

This question already has answers here:
Use of 'prototype' vs. 'this' in JavaScript?
(15 answers)
Closed 7 years ago.
class My {
constructor() {
this.myMethod1 = function(){};
}
myMethod2() {
}
}
So is there any difference between myMethod1 and myMethod2?
Yes. Defining the method inside the constructor results in the function being created and assigned when the object is first constructed, whereas defining it in the class body results in the method being added to the class's prototype.
class My {
constructor() {
this.myMethod1 = function(){};
}
myMethod2() {
}
}
console.log(My.prototype.myMethod1) //=> undefined
console.log(My.prototype.myMethod2) //=> function myMethod2() { ... }
Practically speaking, it's usually preferable to keep method definitions in the class body both for performance reasons and overall readability and semantic correctness. There may be some edge cases where it might make sense to assign methods in the constructor like that, but in my experience they're pretty rare.
For more detailed information on the technical differences between assigning methods to the prototype vs in the constructor, see Use of 'prototype' vs. 'this' in JavaScript?

Extending functionality in TypeScript [duplicate]

This question already has answers here:
Closed 10 years ago.
The community reviewed whether to reopen this question 5 months ago and left it closed:
Original close reason(s) were not resolved
Possible Duplicate:
How does prototype extend on typescript?
I am currently learning TypeScript, and would like to know how it is possible to add functionality to existing objects. Say I want to add an implementation for Foo to the String object. In JavaScript I would do this:
String.prototype.Foo = function() {
// DO THIS...
}
Understanding that TypeScript classes, interfaces and modules are open ended led me to try the following, without success
1. Reference the JavaScript implementation from TypeScript
JavaScript:
String.prototype.Foo = function() {
// DO THIS...
}
TypeScript:
var x = "Hello World";
x.Foo(); //ERROR, Method does not exist
2. Extend the interface
interface String {
Foo(): number;
}
var x = "Hello World";
x.Foo(); //Exists but no implementation.
3. Extend the class
class String {
Foo(): number {
return 0;
}
}
// ERROR: Duplicate identifier 'String'
As you can see from these results, so far I have been able to add the method via an interface contract, but no implementation, so, how do I go about defining AND implementing my Foo method as part of the pre-existing String class?
I have found the solution. It takes a combination of the interface and the JavaScript implementation. The interface provides the contract for TypeScript, allowing visibility of the new method. The JavaScript implementation provides the code that will be executed when the method is called.
Example:
interface String {
foo(): number;
}
String.prototype.foo= function() {
return 0;
}
As of TypeScript 1.4 you can now also extend static members:
interface StringConstructor {
bar(msg: string): void;
}
String.bar = function(msg: string) {
console.log("Example of static extension: " + msg);
}

Categories