User FAQs

If you have a question, please send it to us.


1) What is Object Orientated Programming ?
This is a style of programming that is based on objects containing methods, properties and fields arranged in a hierarchy.
This type of programming is based around four key features:
Polymorphism - Create routines that can operate on objects of different types. This is handled by late binding and multiple interfaces.
Encapsulation - Hide parts of the implementation and hide complexity. This can be achieved using a Module Pattern.
Inheritance (Interface) - (Public Inheritance) Define methods (without implementation) that must be declared in a derived class.
Inheritance (Implementation) - (Private Inheritance) Inherit method implementation from a base class.


2) How many different ways are there to create Objects ?
*) Object Literal
*) Object.create
*) Function constructor
*) ES 2015 class keyword

var ob2 = {};    // preferred 
var obj1 = new Object();

3) Write code to create an Object Prototype ?
Javascript (ES 2009) used functions as classes

function Person(name, age) { 
  this.name = name;
  this.age = age;
};
var you = new Person('You', 24);

4) What are Object Prototypes ?
All JavaScript objects inherit properties and methods from a prototype.
The prototype property allows you to add new properties and methods to existing objects.

function Person( firstname, lastname ) { 
   this.firstname = firstname;
   this.lastname = lastname;
};

// adds a property
Person.prototype.age = 32;

// adds a method
Person.prototype.name = function() {
   return this.firstname + ' ' + this.lastname;
}

5) What is the syntax for accessing object properties ?

obj.name = 'Russell';  
var name = obj.name;
obj['name'] = 'Russell';
var name = obj['name'];

6) What is the 'this' keyword used for ?
The 'this' keyword refers to the object from where it is called.

var obj = { 
   val: 'hello',
   myFunction: function() {
      console.log( this.val );
   }
};
var obj1 = new obj();
obj.myFunction();

The 'this' keyword provides a way of referring back to the object that is associated with it.
The 'this' keyword is used as a mechanism to reach into the containing object for the properties on that object.
How the function is executed determines what the 'this' keyword refers to.

var obj = { 
   var _this = this;
   _this.val: 'hello',
   _this.myFunction: function() {
      console.log( _this.val );
   }
};
var obj2 = new obj();
obj.myFunction();

7) What is a 'closure' ?
A closer is an inner scope which has access to all the variables defined outside of its block.
An inner object does have access to the local variables of its parent function.


8) Can you think of any disadvantages to creating private methods ?
Private methods are very memory inefficient because a new copy of the method is created for every instance.

var = myClass function(text) { 
   this.name = text;

   // private method
   var myPrivate = function() {
      this.name = this.name.UpperCase();
   };

   // public method
   this.myPublic = function() {
   };
};

9) Write code to add methods to classes ?
It is possible to attach functions to objects

function makePerson(first, last) { 
  return {
    this.first = first,
    this.last = last,

    this.fullName = function() {
      return this.first + ' ' + this.last;
    },

    this.fullNameReversed = function() {
      return this.last + ', ' + this.first;
    }
  };
}
s = makePerson('Russell', 'Smith');
s.fullName(); // "Russell Smith"
s.fullNameReversed(); // "Smith, Russell"

10) How to test for an empty object ?
You cannot use the "===" operator, (obj === {})
You need to pass the object to the built-in method Object.keys() and to check if the object constructor is Object:

const obj = {} 
if (Object.keys(obj).length === 0 && obj.constructor === Object) {
}




© 2022 Better Solutions Limited. All Rights Reserved. © 2022 Better Solutions Limited TopPrevNext