简体   繁体   English

如何创建JS类/对象的多个实例?

[英]How to create multiple instances of JS Class/Object?

I would like to create multiple instances for apple in below code. 我想在下面的代码中为apple创建多个实例。 how to achieve it. 如何实现。 I don't want to change my object defining style. 我不想更改我的对象定义样式。

var apple = {
    type: "macintosh",
    color: "red",
    getInfo: function () {
        return this.color + ' ' + this.type + ' apple';
    }
}

Thanks In Advance. 提前致谢。

I suggest a constructor function for creating instances: 我建议使用一个构造函数来创建实例:

function apple(type, color){
    this.type = type;
    this.color = color;
}

apple.prototype.getInfo = function(){
    return this.color + ' ' + this.type + ' apple';
};

var apple1 = new apple('mac', 'red');
apple1.getInfo();

http://jsfiddle.net/6S5b5/ http://jsfiddle.net/6S5b5/

You can use this 你可以用这个

function giveMeApple() {
    var apple = {
    type: "macintosh",
    color: "red",
    getInfo: function () {
        return this.color + ' ' + this.type + ' apple';
      }
    }

    return apple;
}

var apple1 = giveMeApple();
var apple2 = giveMeApple();

// Do something with apples 

You can use Object.create : 您可以使用Object.create

The Object.create() method creates a new object with the specified prototype object and properties. Object.create()方法使用指定的原型对象和属性创建一个新对象。

var apple = {
    type: "macintosh",
    color: "red",
    getInfo: function () {
        return this.color + ' ' + this.type + ' apple';
    }
}

var otherApple = Object.create(apple);

If you need to support < IE 9, the above link contains a polyfill. 如果需要支持<IE 9,则上面的链接包含一个polyfill。

Usually, this would be a case where constructor functions come in handy. 通常,在这种情况下构造函数会派上用场。 Johan's answer contains those. 约翰的答案就是这些。 But since you don't want to change your object: Here you have another answer. 但是,由于您不想更改对象,因此您将获得另一个答案。

In addition to the answer of blunderboy you can also clone the object. 除了blunderboy答案之外,您还可以克隆对象。 There is no native function to do so, but it is easy to write one yourself. 没有本机函数可以执行此操作,但是您可以自己编写一个。

function cloneObject(obj) {
    var obj2 = {},
        i;

    // Take every property of obj
    for (i in obj) {
        if (obj.hasOwnProperty(i)) {

            // And give obj2 the same property with the same value
            obj2[i] = obj[i];
        }
    }
}

apple2 = cloneObject(apple);
var Apple = function () {

            var AppleType = null;
            var AppleColor = null;
            var self = this;

            var OutPutAppleInfo = function () {
                var String = 'My Apple is ' + AppleType + ' And It Is ' + AppleColor + ' In Color.';
                console.log(String);
            }

            return {

                SetAppleColor: function (obj) {
                    AppleColor = obj;
                },
                SetAppleType: function (obj) {
                    AppleType = obj;
                },
                PrintAppleInfo: function () {
                    OutPutAppleInfo();
                }
            };
        }


        function Init()
        {
            var Apple1 = new Apple();
            var Apple2 = new Apple();
            var Apple3 = new Apple();

            Apple1.SetAppleColor('Yellow');
            Apple2.SetAppleColor('Green');
            Apple3.SetAppleColor('Red');

            Apple1.SetAppleType('macintosh');
            Apple2.SetAppleType('Food');
            Apple3.SetAppleType('Model');

            console.log('Apple1');
            Apple1.PrintAppleInfo();
            console.log('Apple2');
            Apple2.PrintAppleInfo();
            console.log('Apple3');
            Apple3.PrintAppleInfo();

        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM