解释Javascript中var和function.varName之间的区别

时间:2016-05-27 15:12:19

标签: javascript

我正在使用此代码,并希望了解它们之间的区别 var Point = Geometry.Point和 GelImageLaneViewer.viewers = []

在什么情况下我会选择在函数内定义var而不是function.varName?

代码片段来自工作代码:

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;
}());

1 个答案:

答案 0 :(得分:0)

GelImageLaneViewer.viewers会添加一个属性,可以通过询问GelImageLaneViewer来访问GelImageLaneViewer.viewers访问权限的任何内容。 Var viewers会创建一个只能在GelImageLaneViewer范围内访问的属性,并且对范围之外的任何内容都不会显示。