Thu. Jun 30th, 2022

What is Instance-Based Programming?

When you create a new object with JavaScript, the constructor function performs any necessary initialization tasks. This is called a constructor’s body. Though JavaScript is an instance-based language, you do not need to explicitly use this keyword to make objects. If you do not use this, the instance object’s constructor function does the following:
A constructor is a function that is called when an object is created, and it’s body actually performs the initialization tasks. You can learn more about the creation process for constructed objects in the [Building an Object with JavaScript – Part 1](https://www.programgood.net/articles/2016/03/10/building-an-object-with-javascript-part-1/) and [Building an Object with JavaScript – Part 2](https://www.programgood.net/articles/2016/03/11/building-an-object-with-javascript-part-2/) posts.
The JavaScript language does not have any keywords that are specific to constructors; however, JavaScript code can contain statements that look like constructors. These statements do the following:
make objects out of existing data and variables
set up methods and properties on an object

when a function is called, flow of control moves to the function's prototype.
when a function is called, flow of control moves to the function’s prototype.

What are the Benefits of Instance-Based Programming?

For instance-based objects, there are no global variables or function definitions. Thus, the program is more compact and safer than traditional procedural programs. In addition, only one copy of each instance object is created, and all the objects of a particular class are identical. For example, all the instance variables associated with a particular object are the same. Instance-based programs are also easy to debug. The only difference is that one instance object is not shared.
In order to understand the difference between global and local variables, consider this example:

If you had a variable called $count, its value would be the same for all the instances of the class. That is, if you defined another variable with a name like $count2, it would have a value of 1 in all the instance objects of this class (x).

when a function is called, flow of control moves to the function's prototype.
when a function is called, flow of control moves to the function’s prototype.

How Can You Start Using an Instance-Based Programing Language Today?

A class definition specifies properties and methods. These properties and methods are associated with an object created dynamically at run time. In the following example, we create a person class that includes name, address, birthdate and height:
class Person {
public :
Person ( const std :: string & name = “John Smith” , const std :: string & address = “P.O. Box 1001” , int birthdate = 1246991992 , int height ) : name ( name ), address ( address ), birthdate ( birthdate ), height ( height ) {}
Person () { } ;
void ShowName () const ; // and others …
std :: string GetName () { return name ; } // and others …
};
We can then create instances of objects with the name, address, birthdate and height:
Person personOne ( “John Smith” , “P.O. Box 1001” , 1246991992 , 178 ); // std::string is defined in header file

when a function is called, flow of control moves to the function's prototype.
when a function is called, flow of control moves to the function’s prototype.

Concluding Thoughts on the Benefits of Instance-Based Programming

Programs that use instance-based objects are often small and faster than programs that use traditional sequential languages. Instance-based language programs are also easier to maintain and less error prone. A program that uses class definitions is sometimes referred to as OO (object-oriented).
The benefits of using an object-oriented approach to programming are well known. Just as important, however, is the place of object-oriented techniques within the big picture. What are the issues involved in converting a sequential language to an object-oriented one? How can we use techniques that have been developed for small, rapidly developing systems to write large, corporate-wide programs? In this article I will try to answer these questions.
All software development is an iterative process in which small tasks lead to larger ones. This process can be viewed in a number of ways.
Small tasks can be used to develop small programs. These small programs can then be integrated into larger ones. A single, large program is never written; instead a thousand little programs are pieced together to make one big one. The big program grows in small steps, each of which is easy to implement and test because it is short and self-contained.

when a function is called, flow of control moves to the function's prototype.
when a function is called, flow of control moves to the function’s prototype.