开发者

How to call a function of the caller's namespace with this?

Goal: I'm trying to shorten how I call a function of the same namespace in a function by using "this" instead of the namespace string. But apparently "this" wouldn't work for a function, it only works for a variable. I wish to know if it's due to my misusage, or to call function with a namespace string is the only way.

Another beginner's question: I'm calling a functio开发者_Python百科n of the same namespace as the caller's. I googled and someone said by calling: this.myfunction would work, but it doesn't. Any ideas? thanks.

I'm calling: singleEntry.editContent.accept(tinyMCE.get('editContentTa').getContent());

from: index.fragment.singleEntry.editContent.load

I've tried to do it as: this.accept, but firebug says this.accept is not a function...

  index.fragment.singleEntry.editContent.load = (function(singleEntry) {
return function() {
    // prepare edit dialog
    $("#editContentDiv").dialog({
        autoOpen : false,
        height : 500,
        width : 600,
        modal : true,
        buttons : {
            "OK" : function() {
                singleEntry.editContent.accept(tinyMCE.get('editContentTa').getContent());
                $(this).dialog("close");
            },
            "CANCEL" : function() {
                $(this).dialog("close");
            }
        },
        open : function() {
            // set value to the edit area
            tinyMCE.get("editContentTa").setContent($("#veContent").html());
        }
    });


I'm not quite following your ultimate goal, so some theory in hopes it helps you get there:

When you call a function via an object's property (so, via dotted notation, or [] notation), this within the function call will be the object reference. So when you call

singleEntry.editContent.accept(tinyMCE.get('editContentTa').getContent());

...within that call, this will refer to singleEntry.editContent.

If you want this to refer to something else, you can use call:

singleEntry.editContent.accept.call(thisObj, tinyMCE.get('editContentTa').getContent());
//                            ^^^^^^^^^^^^^

...where thisObj is whatever object you want the function to see as this during the call. More here: Mythical methods

call is a property of all (real) JavaScript functions, and is provide specifically to make it easy to call the function and set what this should be (what the context is) during the call. There's also apply, which does exactly the same thing but accepts the arguments to pass to the function as an array rather that as discrete arguments. E.g., these two calls are identical:

// Using `call`
singleEntry.editContent.accept.call(thisObj, tinyMCE.get('editContentTa').getContent());

// Using `apply`
singleEntry.editContent.accept.call(thisObj, [tinyMCE.get('editContentTa').getContent()]);
//                        Note the `[]` -----^

All of this is made possible because in JavaScript, this is determined entirely by how a function is called, not where it's defined — I think you know that, based on your question, but worth flagging up anyway.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜