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

How to initialize an object in javascript? - Stack Overflow

programmeradmin2浏览0评论

I would like to initialize an object in javascript..

My code is as follows:

var PageSlider = {

    sliders: [],

    addSlider: function(display, target, itemClass, width, height, itemsPerPage, defaultPage){
        var slideConfig = {
                display : display,
                target : target
        };
        var slider = this.createSlider(slideConfig);
        slider.initSlider();
        this.sliders.push(slider);

    },

    copy : function(obj) {
        if (null == obj || "object" != typeof obj) return obj;
        var copy = obj.constructor();
        for (var attr in obj) {
            if (obj.hasOwnProperty(attr)) copy[attr] = obj[attr];
        }
        return copy;
    },

    getInstance : function(){
        var copy = PageSlider.copy(PageSlider);
        this.sliders.length = 0;
        return copy;
    }

}

Using the getInstance() method just copy the object. What i need to do is to get the instance of the object.

Help please. Thank you.

I would like to initialize an object in javascript..

My code is as follows:

var PageSlider = {

    sliders: [],

    addSlider: function(display, target, itemClass, width, height, itemsPerPage, defaultPage){
        var slideConfig = {
                display : display,
                target : target
        };
        var slider = this.createSlider(slideConfig);
        slider.initSlider();
        this.sliders.push(slider);

    },

    copy : function(obj) {
        if (null == obj || "object" != typeof obj) return obj;
        var copy = obj.constructor();
        for (var attr in obj) {
            if (obj.hasOwnProperty(attr)) copy[attr] = obj[attr];
        }
        return copy;
    },

    getInstance : function(){
        var copy = PageSlider.copy(PageSlider);
        this.sliders.length = 0;
        return copy;
    }

}

Using the getInstance() method just copy the object. What i need to do is to get the instance of the object.

Help please. Thank you.

Share Improve this question edited Jun 21, 2012 at 22:52 blockhead 9,7053 gold badges46 silver badges69 bronze badges asked Jun 21, 2012 at 22:44 newbienewbie 15k33 gold badges108 silver badges149 bronze badges 3
  • The method createSlider - is this supposed to be a function on PageSlider, or is it a function on the object that's creating PageSlider? – Andrew Shepherd Commented Jun 21, 2012 at 23:03
  • yes. that's correct. i need to make an instance of an object because i'm going to use it three times in one page. The problem is object is not being initialized – newbie Commented Jun 21, 2012 at 23:21
  • just use the word "new" as in: var slider = new PageSlider(); – Orbiting Eden Commented May 13, 2016 at 9:03
Add a comment  | 

5 Answers 5

Reset to default 8

I would recommend prototyping, such as the following

var PageSlider = function() {
    this.sliders = [];
}

PageSlider.prototype.addSlider = function(display, target, itemClass, width, height, itemsPerPage, defaultPage){
    var slideConfig = {
            display : display,
            target : target
    };
    var slider = this.createSlider(slideConfig);
    slider.initSlider();
    this.sliders.push(slider);

}

slider = new PageSlider();

PageSlider is an object already. So to get an instance outside of it, just use its name, like this:

var giveMeAnInstanceOfPageSlider = PageSlider;

You can do it this way since PageSlider is already an object

var pageSlider = PageSlider

How about var pageSlider = PageSlider. PageSlider is already an object.

The idea behind the code is that getInstance will copy the object PageSlider, in effect initializing a new, empty object using the values of the existing PageSlider object — equivalent to getting an instance of the object.

This code is kind of strange as it blurs the line between "classes" and objects in JavaScript (kind of similar to what the programming language Io does), and more true to the notion of object prototypes.

发布评论

评论列表(0)

  1. 暂无评论