简体   繁体   中英

How are parameters being passed to this anonymous function?

I don't quite understand the convention of the following line from the code below

request.transformRequest = internal.transformRequest;

Is this actually calling the internal.transformRequest function, or just setting the function equal to a method of the same name in the request object? I think it's calling the function because transformRequest is never called anywhere else in the code, but how are the parameters of data and getHeaders passed in that case?

internal.transformResourceUrl = function (url) {
        if (url.substr(-1) === '/')
            url = url.substr(0, url.length - 1);
        return url + '.json';
    };

    internal.transformRequest = function (data, getHeaders) {
        // If this is not an object, defer to native stringification.
        if (!angular.isObject(data)) {
            return (data === null) ? '' : data.toString();
        }

        var buffer = [];
        // Serialize each key in the object.
        for (var name in data) {
            if (!data.hasOwnProperty(name)) continue;
            var value = data[name];
            buffer.push(
                encodeURIComponent(name) +
                '=' +
                encodeURIComponent((value === null) ? '' : value )
            );
        }

        // Serialize the buffer and clean it up for transportation.
        var source = buffer
            .join('&')
            .replace(/%20/g, '+')
        ;

        return source;
    };

    internal.generateRequest = function (method, resource, data, account) {
        method = method.toUpperCase();

        if (!angular.isString(account) || account.length < 1)
            account = '_default';

        resource = 'Accounts/' +
            accounts[account] + '/' +
            internal.transformResourceUrl(resource);

        var request = {
            method: method,
            url: apiEndpoint + resource,
            headers: {
                'Authorization': 'Basic ' + credentialsB64
            }
        };

        if (method === 'POST' || method === 'PUT') {
            if (data) request.data = data;
            request.transformRequest = internal.transformRequest;
            request.headers['content-type'] = 'application/x-www-form-urlencoded; charset=utf-8';
        } else if (data) {
            request.params = data;
        }
        return $http(request);
    };

"Is this actually calling the internal.transformRequest function, or just setting the function equal to a method"
"I think it's calling the function because transformRequest is never called anywhere else in the code"

How internal.transformRequest method get's called

line 7 : transformRequest :method(function) is added to internal :object

internal.transformRequest = function (data, getHeaders) {


line 54 : transformRequest propery of request :object is assigned to above method

request.transformRequest = internal.transformRequest;


line 59 : $http() :function is called with request :object who now has transformRequest :method which points to internal.transformRequest

return $http(request);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM