简体   繁体   中英

Explain the difference between var and function.varName in Javascript

I am working the this code and would like to know the difference between var Point = Geometry.Point and GelImageLaneViewer.viewers = []

Under what circumstances would I choose to define a var inside a function versus a function.varName?

Code snippet follows from working code :

var GelImageLaneViewer = (function() {
    "use strict";

    var Point = Geometry.Point,
        **Rect = Geometry.Rect**,
        harmony = false,
        isGel1D = false,
        states = {
            "identified" : {
                "label" : "Identified",
                "visible" : true
                },
            "notAnalyzed" : {
                "label" : "Not Analyzed"
                }
            };
    function GelImageLaneViewer(elem) {
    var spotToolbar,
        svgBox,
        spotToolbarBox,
        oldViewBox,
        newHeight;
    ImageViewer.prototype.setViewportPosition('107px', '5px');
    ImageViewer.prototype.setRegionsOfInterest($('.gel1d').attr('data-all-regionsofinterest'));
    ImageViewer.call(this, elem);
    if($('.gel1d').attr('class') === 'hsppImageViewer gel1d') {
        isGel1D = true;
    }
    this.states = {};
    svgBox = this.svg.getBoundingClientRect();
    this.svg.setAttribute(
        "height",
        svgBox.height + 'px'
    );
    **oldViewBox = Rect.fromViewBox(this.svg);**
    this.svg.setAttribute(
        "viewBox",
        [oldViewBox.x, oldViewBox.y, oldViewBox.width, svgBox.height].join(' ')
    );
    this.image.setAttribute('height', svgBox.height);
}

    GelImageLaneViewer.prototype = Object.create(ImageViewer.prototype);
    GelImageLaneViewer.prototype.constructor = ImageViewer.constructor;

    **GelImageLaneViewer.viewers = [];**
    GelImageLaneViewer.noConflict = function () {
        harmony = true;
    }
    window.addEventListener("DOMContentLoaded", function () {
        var images = document.querySelectorAll('.hsppImageViewer'),
            stop = images.length,
            i,
            viewer,
            src;
        if (!harmony) {
            for (i = 0; i < stop; i += 1) {
                viewer = new GelImageLaneViewer(images[i]);
                src = images[i].getAttribute('data-src');
                if (!!src) {
                    viewer.loadURI(src);
                }
                viewer.initDraw();
                **GelImageLaneViewer.viewers.push(viewer);**
            }
        }
    }, false);

    return GelImageLaneViewer;
}());

GelImageLaneViewer.viewers will add a property which can be accessed by anything with access to GelImageLaneViewer by asking for GelImageLaneViewer.viewers . Var viewers would create a property which can only be accessed inside the scope of GelImageLaneViewer itself, and wouldn't be visible to anything outside the scope.

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