最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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

发布评论

评论列表(0)

  1. 暂无评论