SoFunction
Updated on 2025-04-13

Javascript YUI code reading diary - Part.2 0

batch: function(el, method, o, override) {
// Let el always be HTMLElement
    el = (el && ( || )) ? el : (el); 

    if (!el || !method) {
        return false;
    }

//   Determine the returned object
    var scope = (override) ? o : window;

// It looks like an HTMLElement or not an Array
    if ( ||  === undefined) {
        return (scope, el, o);
    } 

    var collection = []; 
    for (var i = 0, len = ; i < len; ++i) {
        collection[] = (scope, el[i], o);
    }

    return collection;
}, Pony supplement

batch is one of the core of the YUI Dom library. The biggest significance of it is that it makes most other methods of the Dom library
The first parameter can be an id / element object or a set of id/ element objects, reducing the use of the loop. Here you can find the usage of call and apply. After understanding batch, let’s take a look at how to use this method and look at two functions in one breath.

getStyle: function(el, property) {
// toCamel function is introduced later
    property = toCamel(property);

// Get the style of the node
    var f = function(element) {
        return getStyle(element, property);
    };

    return (el, f, , true);
},setStyle: function(el, property, val) {
    property = toCamel(property);

// Set the style of the node
    var f = function(element) {
        setStyle(element, property, val);        
    };

    (el, f, , true);
}, For the specific usage of these two functions, you can read the relevant documents. In fact, it is easy to understand how to use it from the parameters. Looking at the above two functions is helpful to understand the calling method.

Next, take a brief look at getXY

getXY: function(el) {
    var f = function(el) {
// Determine whether the element is "visible to the naked eye"
        if ( ( === null ||  === null ||
                (el, 'display') == 'none') && 
                            el != ) {
            return false;
        }

        return getXY(el);
    };

    return (el, f, , true);
}, getX and getY methods also call this function, but the array elements that get the return value are different. Due to browser compatibility issues, what is provided to the user is to judge the variables and then throw them to the most complex internal getXY function.

OK, leaving too many "suspense" behind, and the next issue focuses on solving them.