从javascript对象访问父级的父级

Rob*_*ert 54 javascript oop

就像是

var life= {
        users : {
             guys : function(){ this.SOMETHING.mameAndDestroy(this.girls); },
             girls : function(){ this.SOMETHING.kiss(this.boys); },
        },
        mameAndDestroy : function(group){ },
        kiss : function(group){ }
};
Run Code Online (Sandbox Code Playgroud)

这个.SOMETHING是我想象的格式,但它可能不是.什么会退回到对象的父级?

jaz*_*kat 58

我只是添加了第一个函数

parentThis = this;
Run Code Online (Sandbox Code Playgroud)

并在子功能中使用parentThis.为什么?因为在JavaScript中,对象很软.可以通过简单的赋值将新成员添加到软对象中(不像是经典对象很难的Java.将新成员添加到硬对象的唯一方法是创建一个新类)更多相关内容:http ://www.crockford.com/javascript/inheritance.html

而且最后你不必杀死或破坏对象.为什么我在这里找到:http://bytes.com/topic/javascript/answers/152552-javascript-destroy-object

希望这可以帮助

  • 在这里超级晚了,但有人可以用代码示例详细说明吗?我没有看到这对OP的代码有什么帮助. (6认同)
  • 我很惊讶这个答案是最受欢迎的答案.谁是"第一职能"?当对象是一个函数时(如tntu答案),它可以工作,但是当对象是OP的问题中的静态对象时,它不起作用!parentThis将返回`Window`对象 (3认同)

har*_*333 45

JavaScript本身不提供此功能.我怀疑你甚至可以创建这种类型的功能.例如:

var Bobby = {name: "Bobby"};
var Dad = {name: "Dad", children: [ Bobby ]};
var Mom = {name: "Mom", children: [ Bobby ]};
Run Code Online (Sandbox Code Playgroud)

鲍比属于谁?

  • 这是与多个父母相关的异常比选择的答案更清楚的例子. (8认同)

Sho*_*og9 19

在这种情况下,您可以使用life引用父对象.或者您可以life在users对象中存储引用.在语言中没有固定的parent可用对象,因为用户只是对对象的引用,并且可能有其他引用...

var death = { residents : life.users };
life.users.smallFurryCreaturesFromAlphaCentauri = { exist : function() {} };
// death.residents.smallFurryCreaturesFromAlphaCentauri now exists
//  - because life.users references the same object as death.residents!
Run Code Online (Sandbox Code Playgroud)

您可能会发现使用以下内容很有帮助:

function addChild(ob, childName, childOb)
{
   ob[childName] = childOb;
   childOb.parent = ob;
}

var life= {
        mameAndDestroy : function(group){ },
        kiss : function(group){ }
};

addChild(life, 'users', {
   guys : function(){ this.parent.mameAndDestroy(this.girls); },
   girls : function(){ this.parent.kiss(this.boys); },
   });

// life.users.parent now exists and points to life
Run Code Online (Sandbox Code Playgroud)


amu*_*ell 5

关于电话:

您可以通过使用.call()which来解决这个问题:

  • 必须在函数上调用 addName.call()
  • 你传递给它一个你想成为“这个”的对象 addName.call({"name" : 'angela'});
  • 您可以传递可以在调用它的函数中使用的其他参数,addName.call({"name": "angela"}, true);其中可能addName接受 boolean 参数append

使用调用:

对于这个特殊问题,我们可以通过“父”对象call来覆盖this子对象中通常存在的内容。

先看看我们的问题

var app = {
    init: function() {
        var _this = this; // so we can access the app object in other functions

        $('#thingy').click(function(){
            alert(_this.options.thingy());
        });

        $('#another').click(function(){
            alert(_this.options.getAnother());
        });
    },
    options: {
      thingy: function() {
      // PROBLEM: the this here refers to options
        return this.getThingy();
      },
      getAnother: function() {
        // PROBLEM 2: we want the this here to refer to options,
        // but thingy will need the parent object
        return 'another ' + this.thingy();
      },
    },
    getThingy: function() {
        return 'thingy';
    }
};
Run Code Online (Sandbox Code Playgroud)

现在,这是一个解决方案 use call

JSFIDDLE看看它的工作原理。

var app = {
    init: function() {
        var _this = this; // so we can access the app object in other functions

        $('#thingy').click(function(){
            // SOLUTION: use call to pass _this as the 'this' used by thingy
            alert(_this.options.thingy.call(_this));
        });

        $('#another').click(function(){
            // SOLUTION 2: Use call to pass parent all the way through
            alert(_this.options.getAnother.call(_this)); 
        });
    },
    options: {
      thingy: function() {
        // SOLUTION in action, the this is the app object, not options.
        return this.getThingy(); 
      },
      getAnother: function() {
        // SOLUTION 2 in action, we can still access the options 
        // AND pass through the app object to the thingy method.
        return 'another ' + this.options.thingy.call(this); 
      },
    },
    getThingy: function() {
        return 'thingy';
    }
};
Run Code Online (Sandbox Code Playgroud)

综上所述

.call()每当您在主对象的属性上使用方法时,您都可以使用:app.options.someFunction(arg)应始终使用.call-调用app.options.someFunction.call(this, arg);- 这样您就可以确保您始终可以访问对象的每个部分。它可以让您访问另一个属性的方法,例如app.helpers.anotherFunction().

一个好主意是在somefunction, 存储this在一个变量中,_parentThis因此很明显this反映了什么。