简体   繁体   English

如何将参数传递给该匿名函数?

[英]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? 这实际上是在调用internal.transformRequest函数,还是只是将函数设置为与request对象中的同名方法相等? 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? 我认为它正在调用该函数,是因为从未在代码中的任何其他位置调用transformRequest ,但是在这种情况下如何传递datagetHeaders的参数?

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" “这实际上是在调用internal.transformRequest函数,还是只是将函数设置为等于方法”
"I think it's calling the function because transformRequest is never called anywhere else in the code" “我认为它正在调用该函数,因为从未在代码的其他任何地方调用transformRequest”

How internal.transformRequest method get's called 如何调用internal.transformRequest方法

line 7 : transformRequest :method(function) is added to internal :object 第7行transformRequest :method(function)添加到internal :object

internal.transformRequest = function (data, getHeaders) {


line 54 : transformRequest propery of request :object is assigned to above method 第54行transformRequest request :object的transformRequest属性分配给上述方法

request.transformRequest = internal.transformRequest;


line 59 : $http() :function is called with request :object who now has transformRequest :method which points to internal.transformRequest 第59行$http() :function被request :object调用,后者现在具有transformRequest :method指向internal.transformRequest

return $http(request);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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