【问题标题】:What makes these two functions 'public' and 'private' respectively, and what does that mean?是什么让这两个功能分别为“公共”和“私有”,这是什么意思?
【发布时间】:2016-05-26 17:51:50
【问题描述】:

这两个函数来自我正在学习的课程 (https://www.udacity.com/course/front-end-web-developer-nanodegree--nd001)。代码和 cmets 来自课程提供者:

/* This is the publicly accessible image loading function. It accepts
 * an array of strings pointing to image files or a string for a single
 * image. It will then call our private image loading function accordingly.
 */
function load(urlOrArr) {
    if(urlOrArr instanceof Array) {
        /* If the developer passed in an array of images
         * loop through each value and call our image
         * loader on that image file
         */
        urlOrArr.forEach(function(url) {
            _load(url);
        });
    } else {
        /* The developer did not pass an array to this function,
         * assume the value is a string and call our image loader
         * directly.
         */
        _load(urlOrArr);
    }
}

/* This is our private image loader function, it is
 * called by the public image loader function.
 */
function _load(url) {
    if(resourceCache[url]) {
        /* If this URL has been previously loaded it will exist within
         * our resourceCache array. Just return that image rather
         * re-loading the image.
         */
        return resourceCache[url];
    } else {
        /* This URL has not been previously loaded and is not present
         * within our cache; we'll need to load this image.
         */
        var img = new Image();
        img.onload = function() {
            /* Once our image has properly loaded, add it to our cache
             * so that we can simply return this image if the developer
             * attempts to load this file in the future.
             */
            resourceCache[url] = img;

            /* Once the image is actually loaded and properly cached,
             * call all of the onReady() callbacks we have defined.
             */
            if(isReady()) {
                readyCallbacks.forEach(function(func) { func(); });
            }
        };

        /* Set the initial cache value to false, this will change when
         * the image's onload event handler is called. Finally, point
         * the image's src attribute to the passed in URL.
         */
        resourceCache[url] = false;
        img.src = url;
    }
}

为什么load() 是“可公开访问”而_load() 是“私有”?在这种情况下,公共/私人意味着什么?

如果您需要,完整文件位于https://github.com/YolkFolkDizzy/frontend-nanodegree-arcade-game/blob/master/js/resources.js

【问题讨论】:

  • "Public" 表示可以从任何地方调用它。 “私有”意味着它只能从包含此函数的文件中访问。还要注意它说“Public”函数调用“Private”函数,所以当你运行“Public”函数时,它仍然运行相同的代码。

标签: javascript oop private public


【解决方案1】:

它是私有的,因为它不能被直接调用...参见第 105 行:

window.Resources = {
    load: load,
    get: get,
    onReady: onReady,
    isReady: isReady
};

由于该方法是在作用域中声明的,因此它在其他任何地方都不可用。

可以看到里面写了代码:

(function() {
...
})()

它强制将任何函数声明或变量声明附加到当前范围。如果没有这个,变量将附加到最近的当前对象通常是窗口。所以_load 永远不会被导出,调用它的唯一方法是调用在Resource 对象中的window 上导出的方法之一。

  • 公共是指可以从外部调用某些内容。
  • 私有是指某些东西只能从内部调用。

在 Javascript 中,私有属性通常隐藏在一个范围内,只有在该范围内创建的函数才能使用。

【讨论】:

  • 此外,请注意,许多人使用初始下划线(“_”)来表示私有数据。这只是一个约定,它本身实际上并不强制执行私有访问。但是有些人将其用作区分公共与私人的唯一机制。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-04-11
  • 1970-01-01
  • 1970-01-01
  • 2021-12-28
  • 2012-03-01
  • 2018-10-29
  • 1970-01-01
相关资源
最近更新 更多