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.
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 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 wheels, engine, color, 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 wheels, engine, and color properties on Vehicle.
Reference: bible of Javascript, Javascript Good Parts, msdn
Comments
Post a Comment
Important - Make sure to click the Notify Me check-box below the comment to be notified of follow up comments and replies.