2

I would like to create multiple instances for apple in below code. 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.

1
  • You will need to clone your object, in that case. Commented Mar 7, 2014 at 8:50

5 Answers 5

3

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 
Sign up to request clarification or add additional context in comments.

5 Comments

How to execute giveMeApple(); from variable? For ex var cloneGenerator = giveMeApple(); apple1 = cloneGenerator; apple2 = cloneGenerator;
@vrs As I mentioned above in my code, you need to call giveMeApply method multiple times to create new objects. So giveMeApply is not a clone generator but it instead returns an apple object.
yeah that one worked. But I need some thing like cloneGenerator variable or factory, which i am going to expose to other js libaries.
You need to understand assigning a variable to another variables multiple times won't create new objects for you. In any case, you need to call atleast one method. You can write cloneGenerator class and define a method getApple in that class and pass the object of cloneGenerator to other libraries.
var cloneGenerator = giveMeApple; var newApple = cloneGenerator(); or look at my answer for another way to 'clone'.
2

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/

Comments

1

You can use Object.create:

The Object.create() method creates a new object with the specified prototype object and properties.

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.

Comments

1
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();

        }

Comments

0

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. 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);

1 Comment

Or var apple2 = $.extend({}, apple); if he's using jQuery ;)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.