Javascript:使用setTimeout时对象的封装

发布于 2024-12-10 10:33:05 字数 958 浏览 0 评论 0原文

如何使用setTimeout这样的函数来调用对象的成员函数,并在被调用函数内部使用this关键字?

请查看我的消息来源。我通过使用 Javascript closure 变量来模拟这一点。在我的例子中,被调用函数的参数 context 实际上是对象 othis

    var Utils = 
    {
        Caller: function( context, func )
        {
            var _context = context;
            var _func = func;

            this.call = function()
            {           
                _func( _context );
            };

            return this;
        }
    };

// example of using:

function Object()
{
    this._s = "Hello, World!";
    this.startTimer = function()
    {
        var caller = new Utils.Caller( this, this._hello );
        setTimeout( caller.call, 1000 );
    };
    this._hello = function( context )
    {
        alert( context._s );
    }
}

var o = new Object();
o.startTimer();

是否可以保存 _hello() 函数的常用声明并使用关键字this,但不在内部使用context

How to use such functions as setTimeout to call member functions of objects with using of this keyword inside of called functions?

Look to my source please. I simulate this by using Javascript closure variables. In my case called function has argument context that is actually this for the object o:

    var Utils = 
    {
        Caller: function( context, func )
        {
            var _context = context;
            var _func = func;

            this.call = function()
            {           
                _func( _context );
            };

            return this;
        }
    };

// example of using:

function Object()
{
    this._s = "Hello, World!";
    this.startTimer = function()
    {
        var caller = new Utils.Caller( this, this._hello );
        setTimeout( caller.call, 1000 );
    };
    this._hello = function( context )
    {
        alert( context._s );
    }
}

var o = new Object();
o.startTimer();

Is it possible to save usual declaration of _hello() function and use keyword this, but not to use context inside?

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

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

发布评论

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

评论(2

潇烟暮雨 2024-12-17 10:33:05

如果您尝试对经典 OOP 进行传统的私有成员隐藏,请使用以下方法:

    function MyObj() {

        // setup private closure scope
        var that = this;  // keep reference to this in constructor closure scope
        var s = "Hello, World!";
        var hello = function() {
            alert(s);
        };

        // expose public methods  
        this.startTimer = function() {
            setTimeout(function() {
                hello();
            }, 1000);
        };
    }

    var o = new MyObj();
    o.startTimer();

另一种方法:

    function MyObj() {
        var that = this;
        this._s = "Hello, World!";
        this._hello = function() {
            alert(this._s);
        };
        this.startTimer = function() {
            setTimeout(function() {
                hello.call(that);
            }, 1000);
        };
    }

If you are trying to do traditional private member hiding from classical OOP, use the following:

    function MyObj() {

        // setup private closure scope
        var that = this;  // keep reference to this in constructor closure scope
        var s = "Hello, World!";
        var hello = function() {
            alert(s);
        };

        // expose public methods  
        this.startTimer = function() {
            setTimeout(function() {
                hello();
            }, 1000);
        };
    }

    var o = new MyObj();
    o.startTimer();

Another approach:

    function MyObj() {
        var that = this;
        this._s = "Hello, World!";
        this._hello = function() {
            alert(this._s);
        };
        this.startTimer = function() {
            setTimeout(function() {
                hello.call(that);
            }, 1000);
        };
    }
小猫一只 2024-12-17 10:33:05

好吧,我不明白这个问题,这是一些修改后的代码:

var Utils = {
    Caller: function ( context, func ) {
        this.exec = function () {
            func.call( context );
        };
    }
};

function Obj() {
    this._s = 'Hello, World!';

    this._hello = function () {
        alert( this._s );
    }

    this.startTimer = function () {
        var caller = new Utils.Caller( this, this._hello );
        setTimeout( caller.exec, 1000 );
    };  
}

var o = new Obj();
o.startTimer();

告诉我你的想法。

Ok, I didn't understand the question, here is the code after some modifications:

var Utils = {
    Caller: function ( context, func ) {
        this.exec = function () {
            func.call( context );
        };
    }
};

function Obj() {
    this._s = 'Hello, World!';

    this._hello = function () {
        alert( this._s );
    }

    this.startTimer = function () {
        var caller = new Utils.Caller( this, this._hello );
        setTimeout( caller.exec, 1000 );
    };  
}

var o = new Obj();
o.startTimer();

Tell me what you think.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文