jquery - JavaScript object call one method from another - Stack Overflow

Given the following object literal how do I call c from within b?Update 1 - missed one thing I use JQue

Given the following object literal how do I call c from within b?

Update 1 - missed one thing I use JQuery load, which changes the context:

var a = {
    b: function (e) {

        $o.load(path, function (e) { // need to call c from here });
    },
    c: function (e) { 
    }
};

Given the following object literal how do I call c from within b?

Update 1 - missed one thing I use JQuery load, which changes the context:

var a = {
    b: function (e) {

        $o.load(path, function (e) { // need to call c from here });
    },
    c: function (e) { 
    }
};
Share asked Oct 13, 2012 at 14:29 JsCoderJsCoder 2,2338 gold badges35 silver badges66 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 4

You should be able to do a.c() inside .b:

var a = {
    b: function(e) {
        a.c();
    },
    c: function(e) {}
};

a.b(); // calls c

Also, this will be bound to the a object which will allow you to access its properties using this.property:

b: function(e) {
    this.c();
},

Try this:-

var a = {
b: function(e) {
    a.c();
},
c: function(e) {}
};

a.b(); 
var a = {
    b: function (e) {
        a.c();
    },

    c: function (e) {
        // magic goes here
    }
};

a will be a closure so it's accessible in the functions (that is, a is defined in an wide, outer scope, which the narrower, inner scopes in each function inherit). Calling context is irrelevant; closures are formed when and where the functions are defined, so inside b, object a will always stay the same (unlike this, which can change).

From the method b you may call c using this.c as long as they are on the same object. However for the function expression being passed to $o I would suggest you bind the this pointer of b to it. Thus you do:

var a = {
    b: function (e) {
        $o.load(path, function (e) {
            this.c();
        }.bind(this));
    },
    c: function (e) {
    }
};

Edit: The way you're creating and using this object is fragile. The this pointer may point to anything, such as when you unbind the methods or call it with a different context. Even using a.c isn't foolproof as other code may change a and when the method b is called a will point to something else.

I would do something like this:

var a = function (a) {
    a.b = function (e) {
        $o.load(path, function (e) {
            a.c();
        });
    };

    a.c = function (e) {
    };

    return a;
}({});

This code can not be tampered with and allows you to create private variables and closures.

The easiest solution which requires the minimum changes is to use jQuery.proxy():

var a = {
    b: function (e) {

        $o.load(path, $.proxy(function (e) { this.c(); }, this));
    },
    c: function (e) { 
    }
};

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744918658a4600992.html

相关推荐

  • jquery - JavaScript object call one method from another - Stack Overflow

    Given the following object literal how do I call c from within b?Update 1 - missed one thing I use JQue

    1天前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信