-1

I have the following object

    media = {
    data: { files: []},
    types: (function(parent)
    {
        var typesList = { image: 10, epc: 1, pdf: 5, floor_plan: 10, video: 1 };

        var typeObj = {

            remaining: function()
            {
                var that = this;

                return that.limit - parent.data.files.filter(function(element)
                {
                    return element.type == that.name;
                }).length;
            }
        }

        var allTypes = {};

        $.each(typesList, function(index, element)
        {
            allTypes[index] = Object.create(typeObj, { 

                limit: { writable: false,  configurable: false, value: element }
            });
        });

        return allTypes;

    })(this),

Now - what I was trying to achieve was to create a list of types which would be objects created from the same prototype (typeObj) and having their own properties. These types would be available in media.types hence the self executing function returns all these types.

The only problem is - this self-executing function has to refer to the parent (so media) to be able to access it's data property. I tried to call that anonymous function passing THIS as an argument then using parent variable inside it but parent is undefined.

My question is - is there any other way to be able to refer to parent object inside that self executing function?

peterstarling
  • 533
  • 4
  • 12

2 Answers2

0
var data = { files: []};
media = {
    data: data,
    types: (function()
    {
        var typesList = { image: 10, epc: 1, pdf: 5, floor_plan: 10, video: 1 };

        var typeObj = {

            remaining: function()
            {
                var that = this;

                return that.limit - data.files.filter(function(element)
                    {
                        return element.type == that.name;
                    }).length;
            }
        }

        var allTypes = {};

        $.each(typesList, function(index, element)
        {
            allTypes[index] = Object.create(typeObj, {

                limit: { writable: false,  configurable: false, value: element }
            });
        });

        return allTypes;

    })(),
Vlad Churakov
  • 350
  • 2
  • 7
0

You can create a anonymous function for the json object as you have done now, but you need to call that at the end of the json structure like below.

because until the json object is not created the this object will refer to parent object of the script not the media object.

var media = {
  data: { files: "dummy object"},
  types: function()
  {
    console.log(this.data.files);//will be able to print the files obj
  return this;
  }
}.types();

Running Code @ JSFiddle

dreamweiver
  • 6,002
  • 2
  • 24
  • 39
  • The `types` function needs to return `this`. Also, this has nothing to do with JSON. –  Jan 27 '16 at 14:11
  • Good catch bro,I mentioned json because it will make sense to OP in his code but I agree this is generic logic which has nothing to do with json object – dreamweiver Jan 27 '16 at 14:17