使用 javascript 模块模式变体时访问父对象

发布于 2024-12-13 20:05:47 字数 818 浏览 3 评论 0原文

您好,我正在使用此模块模式变体,我正在寻找访问父对象的最佳方法。我意识到无法知道对象有哪些父对象,因此我想在构造函数中包含一些上下文。我以为这会起作用,但事实并非如此,有什么想法吗?

$(document).ready(function(){ 

    var main = new Main();

});


function Main() {

    var one = 'hello';

    init();

    function init() {
        var data = new Data(this);
        var two = data.load();
        console.log(one+' '+two);
        data.output();
    }

}

function Data(context) {

    // public vars / methods

    var pub = {
        'load' : function() {
            return ('world');
        },
        'output' : function() {
            var one = context.one // <-- what should this be?
            var two = this.load();
            console.log (one+' '+two);
        }
    }

    return pub;

}

输出是:

hello world
undefined world

Hi I'm using this module pattern variant and I'm looking for the best way to get access to a parent object. I realise there is no way to know what parents an object has so I want to include some context in the constructor. I thought this would work but it doesn't, any ideas?

$(document).ready(function(){ 

    var main = new Main();

});


function Main() {

    var one = 'hello';

    init();

    function init() {
        var data = new Data(this);
        var two = data.load();
        console.log(one+' '+two);
        data.output();
    }

}

function Data(context) {

    // public vars / methods

    var pub = {
        'load' : function() {
            return ('world');
        },
        'output' : function() {
            var one = context.one // <-- what should this be?
            var two = this.load();
            console.log (one+' '+two);
        }
    }

    return pub;

}

The output is:

hello world
undefined world

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白首有我共你 2024-12-20 20:05:47

当您使用 new 运算符调用构造函数时,您基本上会执行类似

function Main(){
    var this = //magic new object
               //provided by the runtime

    //your code comes here

    return this;
    //because your Data function returns a value,
    // you never get to this line. Perhaps you should use
    // a regular non-constructor data() function instead?
}

当您使用 var 声明私有变量时它将只是一个普通变量而没有其他内容。如果您想向 this 添加内容,您需要明确地这样做,

this.one = 'hello';

但这还不是全部! this 没有词法作用域,因此 init 函数会从外部获取与 this 无关的其他 this (这解释了你得到的undefined)。当您想在内部函数中使用 this 时,您需要采取解决方法,例如:

var that = this;

function init(){
    new Data(that);
}
init();

也就是说,仅通过您的示例,我不明白为什么您需要执行所有这些操作。我更喜欢仅在绝对必要时(当我想使用原型继承时)才使用构造函数(和 new)。在你的情况下,也许你可以采用“更少的面向对象”方法?

//main doesn't need to be a class
// this is not Java :)
function main(){

    //just a plain object for the 
    //context. Create a separate class
    //if you really need to...
    var context = {
        one: 'hello'
    };

    var data = new Data(context);
    var two = data.load();
    console.log(one+' '+two);
    data.output();
}

When you call a constructor function with the new operator, you are basically doing something like

function Main(){
    var this = //magic new object
               //provided by the runtime

    //your code comes here

    return this;
    //because your Data function returns a value,
    // you never get to this line. Perhaps you should use
    // a regular non-constructor data() function instead?
}

When you declare a private variable with var it will just be a plain variable and nothing else. If you want to add things to the this you need to do so explicitely

this.one = 'hello';

But that is not all! this is not lexically scoped so the init function gets its own other this that is unrelated to the this from outside (this explains the undefined you get). When you want to use this in an inner function you need to do a workaround, like in:

var that = this;

function init(){
    new Data(that);
}
init();

That said, just by your example I don't see why you need to do all of this. I prefer to use constructor functions (and new) only when strictly necessary (when I want to make use of prototypal inheritance). In your case perhaps you can get away with a "less OO" approach?

//main doesn't need to be a class
// this is not Java :)
function main(){

    //just a plain object for the 
    //context. Create a separate class
    //if you really need to...
    var context = {
        one: 'hello'
    };

    var data = new Data(context);
    var two = data.load();
    console.log(one+' '+two);
    data.output();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文