How to have a class which is a property of something? [duplicate] - javascript

This question already has answers here:
How to namespace es6 classes (for React components)
(3 answers)
Closed 6 years ago.
I have a javascript object menu, and I want to add a property controller which should be a constructor. However, the following gives a syntax error:
class menu.foobar {
// stuff here
}
What is the right way to do this?

Use a class expression:
menu.foobar = class {}

Related

In JavaScript what is this method called where you might define a variable or property as such variable_name$ref [duplicate]

This question already has answers here:
Rules for unquoted JavaScript Object Literal Keys?
(6 answers)
What characters are valid for JavaScript variable names?
(12 answers)
Closed 1 year ago.
I am seeing it crop up more and more in code I am going through on a new project (can't share due to contractual reasons) where Ill see something like:
{
prop1: value$ref,
$prop2: null
}
I have see ${prop3} before, but never an example without the brackets. Can anyone provide direction as to what the method is, or the operator is or whatever the case?

javascript class.method1.method2 [duplicate]

This question already has answers here:
How does basic object/function chaining work in javascript?
(5 answers)
Chaining methods with javascript
(3 answers)
Closed 1 year ago.
i'm looking for something like :
document.getElementById("id").innerText.toString().length;
so : class.method1().method2().method3();
my code :
class Example
{
method1(){ .. }
method2(){ .. }
method3(){ .. }
}
from out class :
var element = new Example();
element.method1(); //works fine
element.method1().method2(); // error: method2 is not defined
any solution?
thanks for all :)
What you're looking for is known as a fluent interface. To implement this, just add return this; to the end of every method in the class.
Side note: document.getElementById("id").innerText.toString().length; is not a fluent interface - each function/attr returns a different value.

JS Get Classname of Class [duplicate]

This question already has answers here:
Get the class name of ES6 class instance
(5 answers)
Closed 2 years ago.
How do i get the own classname of a class in javascript?
I implemented a method and i want to catch an error by logging the error with a message in which class the error happened.
My current solution is dirty: I save the class-name in the constructor
constructor{
this.className = 'MyClass';}
However, i think there must be a better solution, something like Object.protytpe.XXX
I googled and searched in Stackoverflow, but all the answers didnt result in my desired output.
May somebody have an idea?
Use:
this.constructor.name from inside an instance method of your class
obj.constructor.name where obj is an instance of your class
klass.name where klass is a reference to your class

ES6 class member to be a class itself [duplicate]

This question already has answers here:
Nested ES6 classes?
(4 answers)
Closed 6 years ago.
in es6 a class can have methods and static methods as its members. can it also have classes as members? (because Class is actually kind of function itself?)
i would like it to build nested classes.
class foo {
barMethod {
// do something for the instance
}
static bamMethod {
// do something
}
// e.g.
class batClass {
// the constructor and other stuff for batClass
}
}
new foo.batClass();
[No.] (Nested ES6 classes?). Read this post. You can only do as #floribon mentioned in comment.

Possible to override a base classes privileged function? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Is there an equivalent of PHP's 'parent' with javascript prototypal inheritance?
Override a base class function
I have a base class/prototype & a child class/prototype in Javascript.
Is it possible to make the child class override a base classes privileged function?
I know I am trying to treat javascript as an OO language which its not but you never know this maybe possible?
If its possible could you give an example how I do this?
function baseClass()
{
this.privFunct() {}
}
function childClass()
{
this.privFunct()
{
var baseFunct = baseClass.prototype.privFunct;
this.baseFunct();
// no do some object specific actions here
}
}

Categories

Resources