Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery OOP basics

Tags:

I want to start developing jQuery games, thus I need to learn jQuery OOP. I have some (enough) experience with C++ OOP (developed some games).

I know that I can replace the C++ "class" with jQuery "objects" but I don't know how exactly that works.

Also does jQuery have more advanced "options" like C++ ? (abstract classes/objects, inheirtance, etc... )

Can you guys explain that to me? (or give me the link to some good javascript OOP tutorials).

like image 316
XCS Avatar asked Feb 27 '11 16:02

XCS


People also ask

Is jQuery an OOP?

jQuery's each() function is used to loop through each element of the target jQuery object — an object that contains one or more DOM elements, and exposes all jQuery functions. It's very useful for multi-element DOM manipulation, as well as iterating over arbitrary arrays and object properties.

Can we use OOP in JavaScript?

JavaScript is not a class-based object-oriented language. But it still has ways of using object oriented programming (OOP).

What is are the 4 fundamentals of JS Oops?

The Four Principles of OOP. OOP is normally explained with 4 key principles that dictate how OOP programs work. These are inheritance, encapsulation, abstraction and polymorphism.

What is the OOP in JS?

Object-Oriented Programming is a way of writing code that allows you to create different objects from a common object. The common object is usually called a blueprint while the created objects are called instances. Each instance has properties that are not shared with other instances.


1 Answers

OOP programming in JavaScript can be done in many ways. There are a lot of patterns around.

I will show you two, an Implementation of object inheritance and an implementation of object composition.

This does have absolutely nothing to do with jQuery. jQuery should be used for DOM manipulation and event manipulation. You shouldn't make your core objects and constructors based on jQuery objects. In a game jQuery's role is reading keyboard input and optionally rendering your graphics into the DOM (If you're not using the <canvas> for some reason).

Live example

Inheritance

var Constructor = function(name) {     this.name = name };  Constructor.prototype.mymethod = function() {     alert("my name is : " + this.name); };  var obj = new Constructor("foo"); obj.mymethod(); // my name is : foo 

Here were defining a Constructor function that you can call to create a new object. You refer to the object inside the constructor with this.

You can add (static) methods and variables to the prototype of the Constructor that will be inherited by the object.

function inherits(child, parent) {     var f = new Function;     f.prototype = parent.prototype;     f.prototype.constructor = parent;     child.prototype = new f;     child.prototype.constructor = child; } 

You may use an inherits function which set's the prototype of a constructor function to an instance of a different constructor. This means that all the methods and properties of that parent object are available on the child

var SecondConstructor = function(name) {     this.name = name + "bar"; }; inherits(SecondConstructor, Constructor); var obj = new SecondConstructor("foo"); obj.mymethod(); // my name is : foobar 

This is JavaScripts prototypical inheritance. Basically you set the prototype of a function to a particular object. Then when you use the function to create objects those objects implement the prototype.

Composition

Using the prototype isn't actually neccesary you can also use object composition instead. This method does require a good understanding of the this state which you can read about elsewhere.

I'm going to cheat and delegate some of the tedious helper functions to underscore.js

var Constructor = function(name) {     this.name = name;      this.mymethod = function() {         alert("my name is : " + this.name);     }; };  var SecondConstructor = function(name) {     var constructor = new Constructor(name + "bar");     _.bindAll(constructor);     _.extend(this, {         "mymethod": constructor.mymethod     }); };  var obj = new SecondConstructor("foo"); obj.mymethod(); 

Here the SecondConstructor creates an instance of Constructor for itself rather then inheriting from it. It then binds the reference of this on all the methods of that constructor object so that we can delegate the call to mymethod to our own constructor object. This only works for methods but that shouldn't be an issue because you really shouldn't have public fields.

like image 191
Raynos Avatar answered Oct 11 '22 22:10

Raynos