Skip to main content

JavaScript is too cool and so is the inheritance.

I can say million words about Javascript, it is so cool.

We will start with the  inheritance in the JAVASCRIPT which will make our concepts a little shaky as it implements prototypical inheritance which are not bad the point is we are not used to code in JS. As almost everyone in this world are from JAVA or .NET background.


Inheritance is an important topic in most programming languages.
In the classical languages (such as Java), inheritance (or extends) provides two useful services. First, it is a form of code reuse. If a new class is mostly similar to an existing class, youonly have to specify the differences. Patterns of code reuse are extremely important because they have the potential to significantly reduce the cost of software development. The other benefit of classical inheritance is that it includes the specification of a system of types. 

This mostly frees the programmer from having to write explicit casting operations, which is a very good thing because when casting, the safety benefits of a type system are lost. JavaScript, being a loosely typed language, never casts. The lineage of an object is irrelevant. What matters about an object is what it can do, not what it is descended from.

JavaScript provides a much richer set of code reuse patterns. It can ape the classical pattern, but it also supports other patterns that are more expressive. The set of possible inheritance patterns in JavaScript is vast. 
In this, we’ll look at a few of the most straightforward patterns. Much more complicated constructions are possible, but it is usually best to keep it simple. In classical languages, objects are instances of classes, and a class can inherit from another class. 

JavaScript is a prototypal language, which means that objects inherit directly from other objects.

In JavaScript, a prototype is a property of functions and of objects that are created by constructor functions. The prototype of a function is an object. Its main use is when a function is used as a constructor.

function Vehicle(wheels, engine) {
    this.wheels = wheels;
    this.engine = engine;
}

You can use the prototype property to add properties and methods to objects, even the ones that have already been created:
var testVehicle = new Vehicle(2, false);
Vehicle.prototype.color = "red";
var testColor = testVehicle.color;
The value of testColor is "red".
You can even add properties and methods to predefined objects. For example, you can define a Trim method on the String prototype object, and all the strings in your script will inherit the method.
String.prototype.trim = function()
{
    // Replace leading and trailing spaces with the empty string
    return this.replace(/(^\s*)|(\s*$)/g, "");
}
var s = "    leading and trailing spaces    ";
// Displays "    leading and trailing spaces     (35)"
window.alert(s + " (" + s.length + ")");
// Remove the leading and trailing spaces
s = s.trim();
// Displays "leading and trailing spaces (27)"
window.alert(s + " (" + s.length + ")");
The prototype object can be used to derive one object from another. For example, you can use the Object.create function to derive a new object Bicycle using the prototype of the Vehicle object we defined earlier (plus any new properties you need).
var Bicycle = Object.create(Object.getPrototypeOf(Vehicle), {
    "pedals" :{value: true}
});
The Bicycle object has the properties wheelsenginecolor, and pedals, and its prototype is Vehicle.prototype. The JavaScript engine finds the pedalsproperty on Bicycle, and it looks up the prototype chain to find the wheelsengine, and color properties on Vehicle.
Reference: bible of Javascript, Javascript Good Parts, msdn

Comments

Popular posts from this blog

Send a Fax in windows using faxcomexlib and TAPI in VB code .Net

An application that provides sending fax from faxmodem, connected to the computer, will be explained in the following post.  We can use Telephony Application Programming Interface (TAPI) and the Fax Service Extended Component Object Model (COM) API to send fax. The fax service is a Telephony Application Programming Interface (TAPI)-compliant system service that allows users on a network to send and receive faxes from their desktop applications. The service is available on computers that are running Windows 2000 and later. The fax service provides the following features: Transmitting faxes Receiving faxes Flexible routing of inbound faxes Outbound routing Outgoing fax priorities Archiving sent and received faxes Server and device configuration management Client use of server devices for sending and receiving faxes Event logging Activity logging Delivery receipts Security permissions The following Microsoft Visual Basic code example sends a fax. Note that...

Convert your datatable into generic poco object in c# using linq, ado and reflections.

Follow @harshit_parshii The most common problem that we face these days is to create a common class and method that can be used across all the projects and codes. So today I will be sharing my code where you can see how to make and create a generic function without using entity framework for ado. net. The scenario is like you have an old software that uses stored procedure to return set of entities as a data-table, you do not want to re-write the back-end code as you are creating a web API in c# which needs to be delivered asap. You need to map these data tables to models as you might be using MV* pattern. So here we will be doing one to one mapping of model to data- table, and in similar fashion insert or update can also be done. So basically we are converting a data-table to list of strongly typed object model to do CRUD operations. So we have following things before hand. A helper class is referenced as the database(dbFactory) which executes ado. ne...

Decoding underscore js SortBy - Sorting in javascript

Sorting can be the developers worst nightmare especially if it is done in more then one field. So what can help, recently doing research on it I came across underscore js sortBy (underscorejs.org/#sortBy) function which will help you to sort the array very effectively. So I was going through the documentation and found it very useful, but there are one drawback of it. Will let you know that: How to order in Ascending Order _.sortBy([6, 2, 4, 5, 3, 1], function(num){ return num }); [ 1 , 2 , 3 , 4 , 5 , 6 ] How to order in Descending Order _.sortBy([6, 2, 4, 5, 3, 1], function(num){ return -num }); [ 6 , 5 , 4 , 3 , 2 , 1 ] The underscore sort by will also work with string literals Ascending _.sortBy(['6','2', '4', '5', '3', '1'], function(num){ return num }); [ " 1 " , " 2 " , " 3 " , " 4 " , " 5 " , " 6 " ] Descending _.sortBy(['6','2'...