1

I was just going through the code on transit.js here. I came across the following lines of code:

$.cssHooks['transit:transform'] = {
    // The getter returns a `Transform` object.
    get: function(elem) {
        return $(elem).data('transform') || new Transform();
    },

    // The setter accepts a `Transform` object or a string.
    set: function(elem, v) {
        var value = v;

        if (!(value instanceof Transform)) {
            value = new Transform(value);
        }

        // We've seen the 3D version of Scale() not work in Chrome when the
        // element being scaled extends outside of the viewport.  Thus, we're
        // forcing Chrome to not use the 3d transforms as well.  Not sure if
        // translate is affectede, but not risking it.  Detection code from
        // http://davidwalsh.name/detecting-google-chrome-javascript
        if (support.transform === 'WebkitTransform' && !isChrome) {
            elem.style[support.transform] = value.toString(true);
        } else {
            elem.style[support.transform] = value.toString();
        }

        $(elem).data('transform', value);
    }
};

Now I figured out the $.cssHooks part by looking at the jQuery documentation here, but what I don't quite understand is what on earth is Transform() ?

Note:: i am not asking what new is.

Community
  • 1
  • 1
Alexander Solonik
  • 9,838
  • 18
  • 76
  • 174

1 Answers1

2

Transform is a function used as a constructor, it's defined here:

// ## Transform class
// This is the main class of a transformation property that powers
// `$.fn.css({ transform: '...' })`.
//
// This is, in essence, a dictionary object with key/values as `-transform`
// properties.
//
//     var t = new Transform("rotate(90) scale(4)");
//
//     t.rotate             //=> "90deg"
//     t.scale              //=> "4,4"
//
// Setters are accounted for.
//
//     t.set('rotate', 4)
//     t.rotate             //=> "4deg"
//
// Convert it to a CSS string using the `toString()` and `toString(true)` (for WebKit)
// functions.
//
//     t.toString()         //=> "rotate(90deg) scale(4,4)"
//     t.toString(true)     //=> "rotate(90deg) scale3d(4,4,0)" (WebKit version)
//
function Transform(str) {
    if (typeof str === 'string') { this.parse(str); }
    return this;
}

Transform.prototype = {
    // ### setFromString()
    // Sets a property from a string.
    //
    //     t.setFromString('scale', '2,4');
    //     // Same as set('scale', '2', '4');
    //
    ...

So when calling:

var transform = new Transform();

an instance of Transform will be stored in transform, which have the methods provided by Transform.prototype.

Andreas Louv
  • 46,145
  • 13
  • 104
  • 123