Last week, I have mentioned that triggering the simulation has been done. I could make the modules blink for now. However, the goal now is to simulate every component on its own which is the main purpose of the simulation in the first place. To be able to simulate each and every component differently, we need to run unique Store() and Load() functions of each component. The best way to do that is to implement OOP (Object-oriented programming). Thus, I spent this week studying JavaScript Objects about which I am going to talk this time.

Although each “object-oriented” programming language has its own particular set of semantics, the majority in popular use have “classes.” A class is an entity responsible for creating objects and defining the behaviour of objects. However, JavaScript objects don’t have a formal class, and thus there’s no special syntax for defining how to create an instance or define its behaviour. JavaScript instances are created with a constructor. The constructor of an instance is a function that was invoked with the new operator. In JavaScript, any function can be a constructor, even if it doesn’t look like one:

function square (n) { return n * n; }
  //=> undefined
square(2)
  //=> 4
square(2).constructor
  //=> [Function: Number]
new square(2)
  //=> {}
new square(2).constructor
  //=> [Function: square]

As you can see, the square function will act as a constructor if you call it with new. There is no special kind of thing that constructs new objects, every function is (potentially) a constructor. That’s different from a true classical language, where the class is a special kind of object that creates new instances.

JavaScript doesn’t have a special syntax or special kind of object for that, it has “prototypes.” Prototypes are objects, but unlike a classical system, there are no special methods or properties associated with a prototype. Any object can be a prototype, even an empty object. In fact, that’s exactly what is associated with a constructor by default.

summary of the difference between classes and prototypes

A class in a formal classist language can be an object, but it’s a special kind of object with special properties and methods. It is responsible for creating new instances and for defining the behaviour of instances.

Instance behaviour in a classist language is defined with special syntax. If changes are allowed dynamically, they are done with special syntax and/or special methods invoked on the class.

JavaScript splits the responsibility for instances into a constructor and a prototype. A constructor in JavaScript can be any function. Constructors are responsible for creating new instances.

A prototype in JavaScript can be any object. Prototypes are responsible for defining the behaviour of instances. prototypes don’t have special methods or properties.

Instance behaviour in JavaScript is defined by modifying the prototype directly, e.g. by adding functions to it as properties. There is no special syntax for defining a class or modifying a class.

Finally, I was able to run STORE and LOAD related functions which are methods of JavaScript Objects. Actually, all modules on the schematic are JavaScript objects that were defined by JointJs library. I just studied how these functions were built so that I can add the functions I need to these objects so we can access them easily. For example, if you run the command module.store(), the store function that is in the definition of the object will run according to its type, UART, MSPI, LCD and so on.


0 Comments

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.