最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Understanding syntax and how to pass an argument to a function with no argument list - Stack Overflow

programmeradmin0浏览0评论

This is a two-part question that has been plaguing me for days now.

1) Can someone explain how the following example works?

I'm opening a form in SharePoint and capturing the user's response in a function called CloseCallback. The examples I've found don't indicate passing any arguments with the call.

options.dialogReturnValueCallback = Function.createDelegate(null, CloseCallback);

Yet in the example I have, CloseCallback is defined as:

function CloseCallback(result, args)

How does this work? How can I call the function and not pass anything into it?

2) How does one pass a value into CloseCallback? I have an ID I want to pass in so CloseCallback can use it, but I'm not sure how to get that value in.

This one's driving me batty, and I sure could use the assistance!

This is a two-part question that has been plaguing me for days now.

1) Can someone explain how the following example works?

I'm opening a form in SharePoint and capturing the user's response in a function called CloseCallback. The examples I've found don't indicate passing any arguments with the call.

options.dialogReturnValueCallback = Function.createDelegate(null, CloseCallback);

Yet in the example I have, CloseCallback is defined as:

function CloseCallback(result, args)

How does this work? How can I call the function and not pass anything into it?

2) How does one pass a value into CloseCallback? I have an ID I want to pass in so CloseCallback can use it, but I'm not sure how to get that value in.

This one's driving me batty, and I sure could use the assistance!

Share Improve this question asked Sep 8, 2014 at 13:09 AurumPotabileAurumPotabile 4311 gold badge6 silver badges17 bronze badges 1
  • Callbacks, by definition, are typically functions that you assign (or, in this case, createDelegate) by using just the name of the function, without parentheses. The thing you assign the callback to then has the job of passing arguments as needed. Primitive example: function test(e) {alert("Ermahgerd "+e);} window.onclick = e; – Niet the Dark Absol Commented Sep 8, 2014 at 13:13
Add a ment  | 

2 Answers 2

Reset to default 6

You can define CloseCallback as

var CloseCallback = function (result, args) {

};

Then when you do:

options.dialogReturnValueCallback = Function.createDelegate(null, CloseCallback);

You provide the createDelegate method only the reference to the function, you don't actually call it.

When:

var a = function () {};

Then

var b = a; // assigns b the value of a, b will refer to the same function. 

var c = a(); // c will contain the return value of a

1.

So when you do createDelegate(null, CloseCallback) you only pass the reference to your function (you do a type of b = a)

and createDeleagte will eventually call your callback like:

Function.createDelegate = function (someParam, callback) {
      /*Do some stuff - maybe async etc.*/
      callback.apply(thisArg, args); // here your callback will be called with the appropriate this context, result and the appropriate argument list (result, args)
      /*Do some more stuff*/
}

2

if you wish to have access to a local id variable you should create a context by creating the delegate like:

options.dialogReturnValueCallback = 
    Function.createDelegate(null, function (result, args){
        CloseCallback(id, result, args); // you pass the id reference from the createDelegate (or parent) contexts into CloseCallback
    });

In this case you should modify also the signature of CloseCallback to be:

var CloseCallback = function (id, result, args) {
    ...
};

How does this work? How can I call the function and not pass anything into it?

Based on your question, it doesn't look like you call the function; the API you're leveraging will when it's done. And if it doesn't actually pass any parameters (e.g. they call it like this dialogReturnValueCallback()) then undefined will be assigned to each of your parameters.

How does one pass a value into CloseCallback? I have an ID I want to pass in so CloseCallback can use it, but I'm not sure how to get that value in.

This begins to confuse the workflow. It sounds as if you have control over when and how the callback is executed. If that's the case, then when you execute the callback off of the options object, simply inject the appropriate values from the DOM (e.g. options.dialogReturnValueCallback(someValue)).

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论