background preloader

Learn

Facebook Twitter

JavaScript Inheritance Patterns. In this post, I am going to introduce to you 3 different ways of how you can implement inheritance in JavaScript.

JavaScript Inheritance Patterns

You will see inheritance implemented in languages such as Java by allowing a class to inherit state and behavior from a superclass, where each superclass can have many subclasses. This means that in Java an object is an instance of a class, which can inherit other classes. Now in JavaScript, being prototypal by nature, an object can inherit from an object. For the rest of this post, I will introduce the Pseudoclassical, Functional and Prototypal inheritance patterns in JavaScript.

Pseudoclassical pattern The Pseudoclassical pattern tries to replicate inheritance in a way that is familiar to those who come from a Java or C like background. A pattern which uses a constructor function and the new operator, combined with a prototype added to the constructor is said to be Pseudoclassical. In JavaScript, one way to do this inheritance is: Functional pattern Prototypal pattern Summary.

Backbone.js

Ben Alman. Simple JavaScript Inheritance. I’ve been doing a lot of work, lately, with JavaScript inheritance – namely for my work-in-progress JavaScript book – and in doing so have examined a number of different JavaScript classical-inheritance-simulating techniques.

Simple JavaScript Inheritance

Out of all the ones that I’ve looked at I think my favorites were the implementations employed by base2 and Prototype. I wanted to go about extracting the soul of these techniques into a simple, re-usable, form that could be easily understood and didn’t have any dependencies. Additionally I wanted the result to be simple and highly usable. Here’s an example of what you can do with it: A couple things to note about this implementation: Creating a constructor had to be simple (in this case simply providing an init method does the trick).In order to create a new ‘class’ you must extend (sub-class) an existing class.All of the ‘classes’ inherit from a single ancestor: Class.

Simple Class Creation and Inheritance Initialization Super Method. Suckerfish Dropdowns. A note from the editors: While innovative for its time, this article no longer represents modern best practices.

Suckerfish Dropdowns

“DHTML” dropdown menus have notoriously involved nasty big chunks of JavaScript with numerous browser-specific hacks that render any otherwise neat, semantic HTML quite inaccessible. Oh, the dream of a lightweight, accessible, standards-compliant, cross-browser-compatible method! Enter Suckerfish Dropdowns. Meet the markup#section1 Article Continues Below To start, we should use the best method for defining a navigation menu — a list. Quite straightforward really — nice and neat HTML that, as a result, is highly accessible. Styling it#section2 To get started, all of the lists need to be jigged around a bit — namely, the padding and margin set to zero and the list-style set to none: Now we need to transform the first-level list into a horizontal menu bar.

The next step is to tackle the second-level lists that will be the dropdowns themselves: And now, making the sucker work. Slide Elements in Different Directions. Although jQuery has a nice set of slide methods — .slideDown(), .slideUp(), and .slideToggle() — sometimes we may want to slide an element in a different direction.

Slide Elements in Different Directions

Fortunately, it's pretty easy to do. Reverse the Slide Direction With the built-in slide methods, elements are shown by sliding them down and into view. But what if we want to slide something from the bottom up and into view? The trick here is to use some judicious CSS. <div id="slidebottom" class="slide"> <button>slide it</button> <div class="inner">Slide from bottom</div></div> To get the inner div to slide up, we'll anchor its bottom edge to the bottom of the bottom of the nearest positioned ancestor (in this case, the #slidebottom div):

Google API