Похожие чаты

Hi, guys, I have a question: I have started to

learn classes in ES6, and I want to do the task: create contructor RandomNumber, which returns random number from 1 to 100. For example:

const random = new RandomNumber();

alert(random); // 46

I tried to solve it in the following way:

class RandomNumber {
constructor() {
this.randomNum = (function() {
return Math.floor(Math.random() * 101);
})();
}
}

const random = new RandomNumber();

console.log(random);

But it returns the object like { randomNum: 46 }. How to return only number when I run new RandomNumber(), not the object? Thank you in advance

5 ответов

5 просмотров
Sasha-Jarvi Автор вопроса

Here is jsfiddle https://jsfiddle.net/j24o0gxm/

You use classes to construct objects, not primitives like numbers. You could create an object with the class that has a method to return a random number, but that seems like overkill. I think it'd make more sense to just write a function to return the random number instead of creating a class.

Sasha-Jarvi Автор вопроса
Banana
You use classes to construct objects, not primitiv...

Thanks, I'll try to make it with function

You want a number returning function, not a class

Learning how to do it with classes is a great idea, but you are returning on function to a property of the class thereby making it a method, I hope you know what you are doing

Похожие вопросы

Обсуждают сегодня

Всем привет, написал код ниже, но он выдает сегфолт, в чем причина? #include <stdio.h> #include <stdlib.h> #include <string.h> struct product { char *name; float price; };...
buzz базз
75
Hey, Oceaners 🌊 As we approach Phase 1 of the token merger, OCEAN and AGIX will migrate to FET starting July 1st! Here's a list of exchanges that have communicated their su...
KreigDK | Never DM first🌊
3
VIP 332 Quarterly XVS Buyback, Funds Allocation and New Tokenomics Summary This VIP outlines the protocol’s Quarterly Buyback and Funds Allocation strategy as per our Tokeno...
Venus Announcements
1
База данных не поможет. Шифрование не поможет. Какие там ещё варианты? Накидывайте.
КТ315
20
А табстоп это сообщение от окна или от элемента управления?
The Bird of Hermes
18
I love the passion, really do. I do think this is being blown way out of proportion. We are not deleting our Telegram or not allowing you to talk about anything you want, a...
Mike Herron
3
Did you guys see the latest tweet from TonGifts? 🚀
Mike
44
А как лучше конвертировать физический адрес в виртуальный при маппинге? В случае ядра у меня, например, direct mapping, первые 768МБ я как есть мапплю в higher half, а остальн...
Evg Resh
26
Hey, what is the best and maintained way of transpiling cpp to c? (As a need to support many different toolchains quickly and may not have a c++ support in the given toolch...
Meitar Reihan
11
Hey guys, did you see the latest announcement about $FUN? 🔥
Brian
21
Карта сайта