Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery creating objects [duplicate]

Tags:

How would I create an object in jQuery, and then proceed to make a couple of different instances of this object I.e

Create an object named box which holds a variable called color.

And then make a couple of instances of this object with different stored colours.

like image 937
Ben_hawk Avatar asked Jul 06 '12 02:07

Ben_hawk


2 Answers

Another way to make objects in Javascript using JQuery, getting data from the dom and pass it to the object Box and, for example, store them in an array of Boxes, could be:

var box = {}; // my object var boxes =  []; // my array  $('div.test').each(function (index, value) {     color = $('p', this).attr('color');     box = {         _color: color // being _color a property of `box`     }     boxes.push(box); }); 

Hope it helps!

like image 199
Luis Avatar answered Nov 07 '22 01:11

Luis


May be you want this (oop in javascript)

function box(color) {     this.color=color; }  var box1=new box('red');     var box2=new box('white');     

DEMO.

For more information.

like image 40
The Alpha Avatar answered Nov 07 '22 01:11

The Alpha