Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
  • Jun 19 2020 20:09
    @krisb1220 banned @Gastony
  • May 14 2020 22:39
    @bjorno43 banned @minitechtips_twitter
  • May 14 2020 22:38
    @bjorno43 banned @real-action
  • Feb 01 2020 00:27
    @bjorno43 banned @Ndoua
  • Jan 07 2020 03:10
    @bjorno43 banned @doctor-sam
  • Aug 17 2019 09:32
    User @bjorno43 unbanned @linkin-park
  • Jun 20 2019 21:19
    @bjorno43 banned @shenerd140
  • Apr 25 2019 17:13
    @mstellaluna banned @cmal
  • Jan 08 2019 22:05
    @mstellaluna banned @dertiuss323
  • Nov 07 2018 04:42
    User @texas2010 unbanned @ows-ali
  • Nov 02 2018 16:25
    @texas2010 banned @ows-ali
  • Oct 12 2018 05:50
    @bjorno43 banned @NACH74
  • Oct 05 2018 23:02
    @mstellaluna banned @JomoPipi
  • Sep 16 2018 12:21
    @bjorno43 banned @yash-kedia
  • Sep 16 2018 12:16
    @bjorno43 banned @vnikifirov
  • Sep 05 2018 08:12
    User @bjorno43 unbanned @androuino
  • Sep 05 2018 07:38
    @bjorno43 banned @androuino
  • Aug 23 2018 16:57
    User @bjorno43 unbanned @rahuldkjain
  • Aug 23 2018 16:23
    @bjorno43 banned @rahuldkjain
  • Jul 29 2018 14:13
    User @bjorno43 unbanned @jkyereh
siliconchild
@siliconchild
also about adding comments, does my comments make sense? is this the way to explain code or can you tell me what I'm doing wrong there
Johnny
@JohnnyBizzel

could you tell me about object literal vs singleton class

I was going to ask you! If you can explain it, it means you understand it.

Comments are great. You are a rare example to many coders who don't bother commenting!!
I would say some are not necessary (for me) but if you are in the habit of writing comments, stick at it!
siliconchild
@siliconchild
Thank You, From my understanding you use classes when you need to create multiple instances of an object, when you just need one and only one instant, a object literal with methods and a class with singleton functions seem similar in utility
I'm confused which is to use when
Johnny
@JohnnyBizzel
Object literals you see everywhere these days
Very useful
Singleton class I think means you can only have once instance of that class
This is used a lot these days with modules in node for example
So you get lots of plugins for node that add functionality to your code -
These should be imported once and from then on you refer to that one instance of the module.
siliconchild
@siliconchild
@JohnnyBizzel yes classes that has one instance, i believe. these classes don;t need to be instantiated and can be used like object literals
Johnny
@JohnnyBizzel
Yes - a good technique to learn
siliconchild
@siliconchild
@JohnnyBizzel sorry i didn't understand their difference, only that node modules seem to use singletons. can you tell me what makes sense in my code, is it object literals and if so why?
Johnny
@JohnnyBizzel
Not sure. I don't see a New anywhere
As in const x = new Calc
I think this is instantiating a class
siliconchild
@siliconchild
@JohnnyBizzel Since I used static functions inside the class, There is no need to create a new instance of the object like const x = new Calc, I can just use const x = Calc.operate() like i would if i was using a simple object literal with methods
this is what confuses me
Johnny
@JohnnyBizzel
Oh ok. I have done similar in C# but not JS.
This is probably the difference then.
Not sure. I would have to read up on it more.
Thanks :)
siliconchild
@siliconchild
From reading online I sort of think that object literals and singletons classes are essentially the same thing, just different ways of writing the same. But I cold be entirely wrong
Johnny
@JohnnyBizzel
@siliconchild Could be.
Aditya
@ezioda004
@siliconchild Semantically Object literals and Classes are different thing. Classes are literal syntactical sugar for:
function Car (name){
  this.name = name
}
Car.prototype.sound = function(){
  console.log("vroom");
}

// class syntax
class Car {
  constructor(name){
    this.name = name;
  }
  sound(){
    console.log("vroom");
  }
}
As I can see you have constructor() function but you arent instantiating the class, so shouldnt you use static class field instead?
A recent JS proposal is adding static fields to the class, I think it'd be better for your use case:
class Calc {
    static sum;
    static operatorMemory;

    static reset() {
        Calc.sum = undefined;
        Calc.operatorMemory = undefined;
    }
 ...
siliconchild
@siliconchild
I didn't knew about static variables, makes sense . Thank you
Aditya
@ezioda004
Note: that you'd have to use babel to transpile this.
Since its a stage 3 proposal and not a standard feature just yet.
siliconchild
@siliconchild
oh so i cannot use static sum just yet without relying on babel?
Aditya
@ezioda004
Sadly no, but its again just syntactic sugar for:
class Calc {
}
Calc.sum;
Calc.operatorMemory;
So you can still use this
siliconchild
@siliconchild
so i have to define the variables outside the class?
Aditya
@ezioda004
Correct.
siliconchild
@siliconchild
what i did only makes sense if need multiple instances of those variables? right?
Aditya
@ezioda004
You mean the constructor() method?
siliconchild
@siliconchild
yeah
Aditya
@ezioda004
Yeah that method is only executed if you instantiate a class like const x = new Calc()
Otherwise its not executed so whatevers inside of it isnt ran either.
If you're interested this is how transpilation works
BTW I like the minimalist design of your calc, goodjob!
siliconchild
@siliconchild

Can you also tell me the difference between using

class Calc {
    static sum;
    static operatorMemory;

    static reset() {
        Calc.sum = undefined;
        Calc.operatorMemory = undefined;
    }
}

and using a object like this

const calc = {
    sum,
    operatorMemory,
    reset: function reset() {
        this.sum = undefined;
        this.operatorMemory = undefined;
    }
}

I am confused when to use which

Aditya
@ezioda004
So former one is more classical OOP that you learn in Java, C++, Python. Latter one is more prototypical inheritance, ie more javascript-y
Honestly, this is more subjective question - many will tell you to use the first one, others will tell you to use the second one as there is no correct answer and both are different design pattern
I personally like to use the first one since it feels more ergonomic to me.
siliconchild
@siliconchild
yeah the first on looks cleaner. So, under the hood it's just the same?
just a different way of writing things without any application difference?