Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Object-Oriented JavaScript tools

I have been working on the user interface of my website (www.swalif.com: do use chrome to translate if you like to). Not being familiar with jQuery I started off with JavaScript and now the file is huge: about 1000 lines of code. Furthermore the code is getting complex to handle and change.

Therefore I was looking for a way I could approach this problem in an object oriented manner that would result in a clean, re-usable system with a good architecture. Also would be nice to use features as provided by JQuery to keep the code small.

The problem is that there are a lot of tools out there and I cannot decide which one to invest time into to accomplish this task. e.g. mootools, prototype, jQuery, etc. So I need someone to lead me in the right direction.

This is our website www.swalif.com. Any other suggestion are also welcome.

like image 201
Imran Omar Bukhsh Avatar asked Apr 13 '11 08:04

Imran Omar Bukhsh


People also ask

Can you do OOP in JavaScript?

JavaScript is not a class-based object-oriented language. But it still has ways of using object oriented programming (OOP). In this tutorial, I'll explain OOP and show you how to use it. The most popular model of OOP is class-based.

What is object-oriented tool?

Object-oriented programming (OOP) is a computer programming model that organizes software design around data, or objects, rather than functions and logic. An object can be defined as a data field that has unique attributes and behavior.

What is object-oriented language in JavaScript?

To be more precise, JavaScript is a prototype based Object Oriented Language, which means it doesn't have classes, rather it defines behaviors using a constructor function and then reuse it using the prototype.

Is JavaScript ES6 object-oriented?

In this course, Object-oriented Programming in JavaScript - ES6, you will learn this new syntax and create many different kinds of classes. You'll learn all the features of JavaScript classes including working with constructors, prototypes, inheritance, and simulating public, private, and static members.


3 Answers

For object-oriented javascript development I would recommend John Resig's Simple javascript Inheritance. It is a tiny bit of javascript that allows you to define classes, derive from base classes and override methods.

var Person = Class.extend({
  init: function(isDancing){
    this.dancing = isDancing;
  },
  dance: function(){
    return this.dancing;
  }
});
var Ninja = Person.extend({
  init: function(){
    this._super( false );
  },
  dance: function(){
    // Call the inherited version of dance()
    return this._super();
  },
  swingSword: function(){
    return true;
  }
});

var p = new Person(true);
p.dance(); // => true

var n = new Ninja();
n.dance(); // => false
n.swingSword(); // => true

// Should all be true
p instanceof Person && p instanceof Class &&
n instanceof Ninja && n instanceof Person && n instanceof Class
like image 189
Jakub Konecki Avatar answered Oct 23 '22 16:10

Jakub Konecki


I think you'd be better off with a framework that actively developed and is build with OOP with extendability, reusability, mixings, mutators in mind.

This is exactly why MooTools was created.

That said, if you're not familiar with JS, it would be pretty difficult to grasp MooTools, since it's not a framework for beginners. Then again, if you grasp the notion of OOP, you should be ok.

like image 23
Oskar Krawczyk Avatar answered Oct 23 '22 17:10

Oskar Krawczyk


Don't actually use a framework to implement your OOP. You get a much richer understanding of Javascript as a language when you deal with the nitty gritty of using Javascript's very flexible function prototype system to implement OOP-like operations.

Read about it here: http://phrogz.net/JS/Classes/OOPinJS.html.

like image 3
Chris W. Avatar answered Oct 23 '22 17:10

Chris W.