BY Simmi Kava24 May 2021 Edit
JavaScript: Class - Facts

Classes

Classes are template for creating objects. They are "special functions" and have two components:

  • Class Expression
  • Class Declaration

Let's learn about some facts related to a Class.

1. Class declarations are not hoisted

Function declarations are hoisted but class declarations are not. The class has to be declared first and accessed later, otherwise will throw a Reference Error.

var shape = new Shape(); //Reference Error 

class Shape{}

2. Class body always 'use strict' syntax

All the code written in a class construct is subject to strict mode syntax for increased performance.

3. Class can be used as an expression

Class expressions may or may not have a binding identifier. They allow you to redfine classes without errors. The typeof classes generated using class expression always returns function. The classes can be defined inside another expression, passed, returned or assigned; when used as a class expression.

4. Class methods are non-enumerable

Once you define a class, the enumerable flag is set to false for all methods in the prototype. This helps when we execute for...in over an object, class methods are usually not needed.

5. Classes can be created dynamically i.e. at runtime

function webAtoms(input) {
return class {
greet() {
console.log("Hello " + input); 
}
};
}

var message = webAtoms("WebAtoms"); //Create a new class
new message().greet(); //Hello WebAtoms

6. 'Class fields' allows to add data property to a class

Class fields synatx allows to add any data property to a class. The property created using class fields is not set on on class prototype, but on the individual object.

class Software{
name = "WebAtoms";
type = "FrameWork"
}

var software = new Software();
console.log("Hello " + software.name + " " + software.type); //Hello WebAtoms Framework
console.log("Software Prototype: ", Software.prototype.name) //undefined

Output

Hello WebAtoms FrameWork
Software Prototype:  undefined

We can also use complex expression and function calls for a class field.

7. Class may include getter/setter or compound properties

The syntax is similar to object literals.

class GetSetDemo {
get data() {
return "I am getter property";
}
set data(message) {
console.log(message + " I am a setter property")
}

}

var getSetDemo = new GetSetDemo();
getSetDemo.data = "Hi,";
console.log(getSetDemo.data);

Output

Hi, I am a setter property
I am getter property

8. Class expression identifier is visible only to class

The identifier i.e. name with which a class expression is identified is visible within the expression only. You cannot access it from the outside.

const Demo = class Test{
getIdentifier() {
return Test.name;
}
};

var instance = new Demo();
console.log(instance.getIdentifier()); //Test
console.log(Test.name); //Reference Error

Output

Test
Uncaught ReferenceError: Test is not defined

9. Base Class can be provided dynamically

Like, you can create classes at run-time; base class can also be provided at run-time.

Final Notes

  • eval or arguments are reserved keywords and cannot be used as class names
  • constructor name cannot be used for a generator method or getter/setter method
  • prototype methods cannot be used as constructor; results in TypeError
BY Simmi Kava
LikeCommentSave
LikeCommentSaveShare
0
Categories
General
YantraJS
Developer Guides
Tutorials
Web Atoms Updates

POPULAR POSTS
17 Mar 2021
LATEST ACTIVITY
Simmi Kava
commented this post.
Simmi Kava
liked this post.
Show more
ARCHIVES
2025
2024
2023
2022
TAGS
javascript (56)
developer (25)
javascriptdeveloper (16)
Xamarin.Forms (16)
Html (14)
typescript (12)
webatoms (12)
xamarin (11)
coding (10)
web-atoms (10)
arrays (9)
android (8)
javascript-developer (8)
csharp (7)
dotnet (7)
css (6)
update (6)
dotnet-standard (5)
function (5)
iOS (5)
methods (4)




Web Atoms: JSX (TSX + TypeScript) for Xamarin.Forms, Hot Reload Your App in Production Environment

PlaygroundSamples Repository