javascript自动执行函数作为对象的属性[重复]

时间:2022-02-25 09:51:25

This question already has an answer here:

这个问题在这里已有答案:

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.

现在 - 我想要实现的是创建一个类型列表,这些类型将是从同一个原型(typeObj)创建的对象并具有自己的属性。这些类型在media.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.

唯一的问题是 - 这个自执行函数必须引用父(所以媒体)才能访问它的数据属性。我试图调用匿名函数传递THIS作为参数,然后在其中使用父变量,但父级是未定义的。

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

我的问题是 - 有没有其他方法可以在自执行函数中引用父对象?

2 个解决方案

#1


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;

    })(),

#2


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.

您可以像现在一样为json对象创建一个匿名函数,但是您需要在json结构的末尾调用它,如下所示。

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

因为在创建json对象之前,此对象将引用脚本的父对象而不是媒体对象。

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

运行代码@ JSFiddle

#1


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;

    })(),

#2


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.

您可以像现在一样为json对象创建一个匿名函数,但是您需要在json结构的末尾调用它,如下所示。

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

因为在创建json对象之前,此对象将引用脚本的父对象而不是媒体对象。

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

运行代码@ JSFiddle