简体   繁体   English

在javascript中使用内部对象文字之间的函数

[英]Use of functions between inner object literals in javascript

My issue is I have 2 inner objects in my js class and I'm trying to use the methods from one of those objects in my other object (examples of what I'm trying to do below). 我的问题是我的js类中有2个内部对象,我正在尝试使用其他对象中的一个对象中的方法(我在下面尝试做的例子)。 I understand why this doesn't work because of a the scope. 我理解为什么这不起作用,因为范围。 I'm just wondering if there is a way to get it to work. 我只是想知道是否有办法让它发挥作用。

var Class1 = {

    self : this,
    Obj1 : {

        Obj1Method : function () {
            alert("Do something");
        },
        Obj1Method2 : function () {
            alert("Do something else");
        },

        InnerObj1 : {
            InnerNestObj1Method : function (val) {
                alert(val + 2);
            }
        }
    },

    Class1Method2 : function () {
        this.Obj1.Obj1Method2();
    },

    Obj2 : {

        Obj2Method : function (val2) {
            self.Obj1.InnerObj1.InnerNestObj1Method(val2);
        },

        Obj2Method2 : function () {
            self.Class1Method2();
        }
    }
};

Class1.Obj1.InnerObj1.InnerNestObj1Method(3); //works
Class1.Class1Method2(); //works
Class1.Obj2.Obj2Method2(); //No bueno
Class1.Obj2.Obj2Method(5); //No bueno

You can fix your example by replacing self with Class1 . 你可以通过用Class1替换self来修复你的例子。 The line self : this, is setting Class1.self to point to the global object ( this when that line is evaluated). self : this,是设置Class1.self指向全局对象( this时线被评估)。

 var Class1 = { self : this, Obj1 : { Obj1Method : function () { alert("Do something"); }, Obj1Method2 : function () { alert("Do something else"); }, InnerObj1 : { InnerNestObj1Method : function (val) { alert(val + 2); } } }, Class1Method2 : function () { this.Obj1.Obj1Method2(); }, Obj2 : { Obj2Method : function (val2) { Class1.Obj1.InnerObj1.InnerNestObj1Method(val2); }, Obj2Method2 : function () { Class1.Class1Method2(); } } }; Class1.Obj1.InnerObj1.InnerNestObj1Method(3); //works Class1.Class1Method2(); //works Class1.Obj2.Obj2Method2(); //bueno Class1.Obj2.Obj2Method(5); //bueno 

What happens when you do self: this 当你做self: this时会发生什么self: this

// If this is running in non strict mode, from the global scope, `this` points
// To the global object because there was no function call setting `this`
var Class1 = {
    self : this,
};

What you need to understand is that this is set by whoever called the function using this . 您需要了解的是, this是由使用this函数调用函数的人设置的。 In the example above, there is no caller, so the runtime sets this to point to the global object. 在上面的例子中,没有呼叫者,因此运行时设置this指向全局对象。

Here's how you could you could make your object a bit more reusable and give yourself a reference to the outer object: 以下是您可以如何使对象更具可重用性,并为自己提供对外部对象的引用:

 function createClass() { var self = { Obj1: { Obj1Method: function() { alert("Do something"); }, Obj1Method2: function() { alert("Do something else"); }, InnerObj1: { InnerNestObj1Method: function(val) { alert(val + 2); } } }, Class1Method2: function() { self.Obj1.Obj1Method2(); }, Obj2: { Obj2Method: function(val2) { self.Obj1.InnerObj1.InnerNestObj1Method(val2); }, Obj2Method2: function() { self.Class1Method2(); } } }; return self; } var Class1 = createClass(); Class1.Obj1.InnerObj1.InnerNestObj1Method(3); //works Class1.Class1Method2(); //works Class1.Obj2.Obj2Method2(); //works Class1.Obj2.Obj2Method(5); //works 

You can do it with Classes : 你可以用Classes做到这一点:

 "use strict" class Class1 { constructor() { this.Obj1 = { Obj1Method: function() { alert("Do something"); }, Obj1Method2: function() { alert("Do something else"); }, InnerObj1: { InnerNestObj1Method: function(val) { alert(val + 2); } } }; var self = this; this.Obj2 = { Obj2Method: function(val2) { self.Obj1.InnerObj1.InnerNestObj1Method(val2); }, Obj2Method2: function() { self.Class1Method2(); } }; } Class1Method2() { this.Obj1.Obj1Method2(); } }; var c1 = new Class1(); c1.Obj1.InnerObj1.InnerNestObj1Method(3); //works c1.Class1Method2(); //works c1.Obj2.Obj2Method(3); //works c1.Obj2.Obj2Method2(); //works 

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

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