JavaScript with LinkedIn Learning, Day 3

Another day, another example of JavaScript doing things strangely, where strange is defined in the self-centered manner of “not how I would expect, based on what I learned”. This time it’s the flexible method of creating a class: either using a declaration:

class ClassName { properties }

Or using an expression:

const ClassName = class { properties }

According to the lesson the trend is towards using an expression, but the instructor uses a declaration, and it seems to me to line up with other languages’ way of doing things a bit better.

You can also create a “class” of objects just by creating a constructor function, which will contain all the properties including other functions (in the declaration and expression options, the constructor function is separated out from the other methods of the “class”), like so:

function ClassName ( attributes ) { properties }

Here attributes are the variables passed into the constructor and properties are any work done to set the instance variables, any other variables, and any other functions the “class” might have.

The tutorial makes a point of saying that creating a “class” by just using the object constructor function is not the same as making a real class, and that real classes are much more flexible and extendable than just using a function, so the third option is not recommended; it’s just that JavaScript didn’t have classes for a while, so older code and tutorials might use object constructor functions.

That’s going to be all for today — hoping I get some time to put this into practice soon.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

css.php