Search code examples
javascriptinheritancereferenceclosuresobject-literal

Javascript - Inheritence and reference


In javascript, other than prototypal inheritence using prototypes, is there any other way of inheritence. Is the use of object literal only for creating a singleton class.

What is a reference when it comes in javascript. What are the uses of closure concept? Is there an alias in C++?


Solution

  • is there any other way of inheritence than prototypal inheritance.

    Yes, many other patterns are available, all of them based on creating instance-specific properties on the objects. Look for parasitic inheritance or the mixin pattern.

    Is the use of object literal only for creating a singleton class.

    Object literals don't create classes, they create objects. Since almost everything is an object in JavaScript, an object can represent almost every kind of data structure. Most of these uses can include the creation via an object literal.

    what is a reference when it comes in javascript.

    Every object basically is a reference to its properties. Whenever you deal with objects, all you are holding is a reference to the actual data structure that contains the properties. You might want to get familiar with OOP. Also have a look at How to explain object references in ECMAScript terms?.

    what are the uses of closure concept

    Encapsulation, mostly. You won't find a exhaustive list, though, closures are pretty ubiquitous in JavaScript code.

    is there an alias in C++?

    Yes, C++ has closures as well, but you need to explicitly declare them.