JS Get Classname of Class [duplicate] - javascript

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

Related

( Uncaught TypeError: Cannot set property 'display' of undefined [duplicate]

This question already has answers here:
What do querySelectorAll and getElementsBy* methods return?
(12 answers)
Closed 1 year ago.
function open() {
document.getElementsByClassName("nav").style.display='flex';}
I have tried several times to display the items but can't resolve it ,I hope someone
helps thanks in advance!!
document.getElementsByClassName("nav")
The getElementsByClassName method of Document interface returns an
array-like object of all child elements which have all of the given
class name(s). When called on the document object, the complete
document is searched, including the root node. You may also call
getElementsByClassName() on any element; it will return only elements
which are descendants of the specified root element with the given
class name(s).
you probably need to do the following (assuming you only have one element with class name as nave):
(document.getElementsByClassName("nav")[0]).style.display='flex';

What does question mark (?) after class name mean when calling class methods in TypeScript? [duplicate]

This question already has answers here:
Safe navigation operator (?.) or (!.) and null property paths
(7 answers)
Closed 2 years ago.
I have a TypeScript code snippet that contains the following statement row?.delete();.
I was wondering what does the question mark symbolize?
What would happen in case row was null?
Thanks!
This operator is called optional chaining. What it does is first checks if the row is defined and then tries to access the delete.
In case row is null this row?.delete() will just return undefined.If you used it without this operator like row.delete() and row was null you would get Uncaught Type Error: Cannot read property 'row' of undefined.
For more details and examples you can check here https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html

Method to call a function in javascript in a particular way [duplicate]

This question already has answers here:
How do I write an extension method in JavaScript?
(2 answers)
Add method to string class
(6 answers)
Closed 2 years ago.
I am kinda new to js and would appreciate some help to clarify one subject.
Basically i want to call some functions that i write like default javascript are called:
//declaring function
const splitAsExample = text => text.split('|')
//calling function
splitAsExample('Yesterday|Today|Tomorrow')
Instead of calling the function as mentioned above, i would like to know if it's possible to make a function that can be called like:
'Yesterday|Today|Tomorrow'.splitAsExample()
//and || or
'Yesterday|Today|Tomorrow'.splitAsExample
I learned js all by myself and didn't manage to find a specific name for this question to search up in google. :)
If you can clarify this topic for me it would be great, but if you could give me the name to search it up would be even better!
You could add a prototype function to String.
This allows method chaining with a given object.
String.prototype.splitAsExample = function () { return this.split('|'); };
console.log('Yesterday|Today|Tomorrow'.splitAsExample());

Seeking an alternative to arguments.callee.name for use in console.log [duplicate]

This question already has answers here:
How to get function name in strict mode [proper way]
(2 answers)
argument.callee.name alternative in the new ECMA5 Javascript Standard [duplicate]
(4 answers)
Closed 5 years ago.
'use strict';
function foobar(){
console.log(arguments.callee.name); // foobar
}
I hear this once worked, but now it's deprecated. There seem to be other alternative to it for other situations, and I've seen lots of references to using function.name, but having to enter foobar.name defeats the purpose. Within a class I can use this.constructor.name and get the name of the class. I'm looking for something like that for the name of a function.
Why this is not a duplicate to any existing question since ES6: one answer to one similar question talks about using named functions, but this is a named function. To another question the answer is to use function.name - but this question is basically asking if there is a way to .log the name without knowing the name. To another question the answer was to use something like ()=>Function.caller.name but that is non-standard.
If you're using ES6, the function now has a property 'name'
Using your example:
function foobar(){
console.log(foobar.name); // foobar
}

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

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 {}

Categories

Resources