How to convert javascript object variable into a class - Stack Overflow

How do I define a class I can instatiate later?I wrote myself a rotator class using jquery which looks

How do I define a class I can instatiate later?

I wrote myself a rotator class using jquery which looks like:

var Rotator =
{
Loop: null,

init: function(identifierName)
    {
    ........
    }
}

If i want to have one rotator on my page it's good. I just call Rotator.init() and it's ready. However when I want to have 3 rotators i got to define 3 times the whole class code changing its name.

Way easier it would be if I could just do

  Instance1 = new rotator;
  Instance2 = new rotator;
  Instance3 = new rotator;

How do I define a class I can instatiate later?

I wrote myself a rotator class using jquery which looks like:

var Rotator =
{
Loop: null,

init: function(identifierName)
    {
    ........
    }
}

If i want to have one rotator on my page it's good. I just call Rotator.init() and it's ready. However when I want to have 3 rotators i got to define 3 times the whole class code changing its name.

Way easier it would be if I could just do

  Instance1 = new rotator;
  Instance2 = new rotator;
  Instance3 = new rotator;
Share Improve this question edited Mar 29, 2013 at 18:40 Evan Davis 36.7k7 gold badges52 silver badges58 bronze badges asked Mar 29, 2013 at 18:08 GrzegorzGrzegorz 3,6084 gold badges30 silver badges48 bronze badges 2
  • 2 this might be useful – Denys Séguret Commented Mar 29, 2013 at 18:09
  • Thx, clears bit more :) – Grzegorz Commented Mar 30, 2013 at 4:18
Add a ment  | 

2 Answers 2

Reset to default 3

The following is what your object literal might look like as a re-usable Named Function that can be instantiated multiple times:

var Rotator = function(name) {
    this.Name = name;
    this.Loop = null;

    this.init = function(identifierName)
    {
        this.Name = identifierName;
    };
};

// usage:

var foorotator = new Rotator('foo');
var barrotator = new Rotator('bar');

alert(foorotator.Name);
alert(barrotator.Name);

http://jsfiddle/JzWCL/

After Edit:

http://jsfiddle/mPzsq/

Xander's solution looks like an acceptable form for a class-like object used only once. If you plan to subclass or multiply instantiate it, however, you should apply methods to the prototype rather than defining them within the main class (constructor) function. For example:

var Rotator = function(name) {
    //run your initialization logic inside this constructor function
    this.Name = name;
    this.Loop = null;
}

Rotator.prototype.someMethod = function() {
    //method code
}

var rotator1 = new Rotator('foo');
var rotator2 = new Rotator('bar');

The reason to use this structure is to prevent the methods from being reconstructed every time the class is instantiated. By applying the methods to the prototype, they will be shared between all instances of the class.

I've found this to be a helpful reference for some basics of JavaScript class definition: 3 Ways to Define a JavaScript Class

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745342203a4623378.html

相关推荐

  • How to convert javascript object variable into a class - Stack Overflow

    How do I define a class I can instatiate later?I wrote myself a rotator class using jquery which looks

    3小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信